当前位置:   article > 正文

逻辑回归模型实例(by Fminunc and Gradient descent) ...

逻辑回归作业 fminunc


本实例整理自斯坦福机器学习课程课后练习ex2

一、Binary classification

本例中是用机器学习通过已知的输入(两次考试成绩)和输出(是否被录取)来建立一个模型;然后根据输入的成绩来判断一个学生是否能被录取。

1.可视化(visualizing)

  1. data = load('ex2data1.txt');
  2. X = data(:,1:2);
  3. y = data(:,3);
  4. %% step 1.Visualizing
  5. pos = find(y==1);
  6. neg = find(y==0);
  7. plot(X(pos,1),X(pos,2),'k+','LineWidth',2,'MarkerSize',7);
  8. hold on;
  9. plot(X(neg,1),X(neg,2),'ko','MarkerSize',7,'MarkerFaceColor', 'y');
  10. xlabel('Exam 1 score');
  11. ylabel('Exam 2 score');
  12. legend('Admitted', 'Not admitted');
  13. hold off;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

这里写图片描述

2.Hypothesis function and Cost function

假设函数的形式如下:


然后先完成函数 在matlab中的定义:
  1. %sigmoid.m
  2. function g = sigmoid(z)
  3. g = zeros(size(z));
  4. Denominator = 1 + exp(-z);
  5. g = 1 ./ Denominator ;
  6. end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

代价函数的形式如下:

此时,我们建立一个计算的函数,名为costFunction(),其中返回值就是代价值。

  1. % costFunction.m
  2. function J= costFunction( X,y,theta )
  3. m = length(y);
  4. h_theta = sigmoid(X * theta);
  5. J = (1/m)*(sum(-y .* log(h_theta) - ( 1 - y) .* log(1-h_theta)));
  6. end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.确立优化参数的方法(fit parameter)

在确定好假设函数和代价之后,我们就需要选择一种方法求解我们的参数,即;使得在这种情况下,函数取得最小值(minimum)或者局部最小值(local minimum).

目前我们知道的方法有梯度下降(Gradient descent)算法和进阶优化算法(Advanced algorithm)。现在分别就进阶优化算和梯度下降算法来求解。


Advanced Optimization:

我们将使用Octave/Matlab中内置的一个函数fminunc来进行求解。此时我们将不需要手动来设置的值,只需写出cost function 以及gradient即可,所以我们在costFunction.m中加上对每个参数的求导计算公式。

  1. function [J,grad] = costFunction( X,y,theta )
  2. m = length(y);
  3. grad = zeros(size(theta));
  4. h_theta = sigmoid(X * theta);
  5. J = (1/m)*(sum(-y .* log(h_theta) - ( 1 - y) .* log(1-h_theta)));
  6. for i = 1:size(theta)
  7. grad(i) = (1/m) * sum((h_theta - y ) .* X(:,i));
  8. end
  9. end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

下面是利用fminuc进行求解:

  1. % Set options for fminunc
  2. options = optimset('GradObj', 'on', 'MaxIter', 400);
  3. % Run fminunc to obtain the optimal theta
  4. % This function will return theta and the cost
  5. [theta, cost] = ...
  6. fminunc(@(t)(costFunction(t, X, y)), initial theta, options);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

下面是关于fminuc的使用介绍:

In this code snippet, we first defined the options to be used with fminunc.
Specifically, we set the GradObj option to on, which tells fminunc that our
function returns both the cost and the gradient. This allows fminunc to
use the gradient when minimizing the function. Furthermore, we set the
MaxIter option to 400, so that fminunc will run for at most 400 steps before
it terminates.

To specify the actual function we are minimizing, we use a “short-hand”
for specifying functions with the @(t) ( costFunction(X, y,t) ) . This
creates a function, with argument t, which calls your costFunction. This
allows us to wrap the costFunction for use with fminunc.

If you have completed the costFunction correctly, fminunc will converge on the right optimization parameters and return the final values of the cost and . Notice that by using fminunc, you did not have to write any loops yourself, or set a learning rate like you did for gradient descent. This is all done by fminunc: you only needed to provide a function calculating the cost and the gradient.

  1. %% 以下是计算结果
  2. cost J = 0.203506
  3. theta -24.932770
  4. theta 0.204406
  5. theta 0.199616
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

画出Decision boundary

plotDecisionBoundary(theta,X,y);
  • 1

这里写图片描述

预测:

  1. chance = sigmoid([1,63,63]*theta);
  2. fprintf('chance = %f\n',chance);
  3. %% 预测结果
  4. chance = 0.627294
  • 1
  • 2
  • 3
  • 4
  • 5

Matlab源码下载



梯度下降算法:


此时,我们建立一个梯度下降的函数,名为gradientDescen(),其中theta为最终得到的参数值,J_history为每次梯度下降后的代价值。
  1. %gradientDescent.m
  2. function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
  3. m = length(y); % number of training examples
  4. J_history = zeros(num_iters, 1);
  5. temp_matrix = zeros(size(X,2),1);
  6. for iter = 1:num_iters
  7. prediction = sigmoid(X*theta) - y;
  8. for i = 1:size(X,2);
  9. temp_matrix(i) = sum(prediction .* X(:,i));
  10. end
  11. theta = theta - alpha * (1/m) * temp_matrix;
  12. % Save the cost J in every iteration
  13. J_history(iter) = costFunction(X, y, theta);
  14. %fprintf('j=%f\n',J_history);
  15. end
  16. end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  1. % test.m
  2. data = load('ex2data1.txt');
  3. X = data(:,1:2);
  4. y = data(:,3);
  5. %% step 1.Visualizing
  6. plotData(X,y);
  7. xlabel('Exam 1 score');
  8. ylabel('Exam 2 score');
  9. legend('Admitted', 'Not admitted');
  10. %% step 2. Hypothesis and Cost function
  11. % sigmoid.m costFunction.m
  12. m = length(y);
  13. X = [ones(m,1),X];
  14. theta =-5*ones(size(X,2),1)+1E-10;
  15. %theta =zeros(size(X,2),1)-1E-6;
  16. %% step 3. Fit parameter
  17. [theta, J_history] = gradientDescent(X, y, theta, 0.0013, 100)
  18. figure;
  19. plot(J_history,'-r','LineWidth',2);
  20. axis([87 100 0.385 0.42]);
  21. hold on;
  22. figure;
  23. plotDecisionBoundary(theta,X,y);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

这里写图片描述

我们可以看到,代价值接近0.38,但与之前的0.2相比还是略微的偏大。所以自然而然其最终的拟合程度肯定不如之前的高,而事实也是如此。

这里写图片描述

Matlab源码下载

转载于:https://www.cnblogs.com/tolic/p/7142208.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/674867
推荐阅读
相关标签
  

闽ICP备14008679号