The logistic regression cost function is convex, so gradient descent will always find the global minimum.
问题一:采用逻辑回归
Suppose that you are the administrator of a university department and you want to determine each applicant's chance of admission based on their results on two exams. You have historical data from previous applicants that you can use as a training set for logistic regression. For each training example, you have the applicant's scores on two exams and the admissions decision.
Your task is to build a classi cation model that estimates an applicant's probability of admission based the scores from those two exams. This outline and the framework code in ex2.m will guide you through the exercise.
1. 最主要的步骤是计算cost fucnction和 gradient
function [J, grad] = costFunction(theta, X, y)
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
Jtmp=0;
h= zeros(m,1);
%step1:compute hx
hx = X*theta;
%step2:compute h(hx)
h = sigmoid(hx);
%step3:compute cost function's sum part
for i=1:m,
Jtmp=Jtmp+(-y(i)*log(h(i))-(1-y(i))*log(1-h(i)));
end;
J=(1/m)*Jtmp;
%step4:compute gradient's sum part
sum1 =zeros(size(X,2),1);%#features row
for i=1:m
sum1 = sum1+(h(i)-y(i)).*X(i,:)';
end;
grad= (1/m)*sum1;
2. 其中调用的sigmoid(S型函数)如下:
function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g = zeros(size(z));
for i =1:size(z,1)
for j =1:size(z,2)
g(i,j)=1/(1+e^(-z(i,j)));
end;
end;
3.调用octave的内建函数fminunc();来获得最优的theta和最小的cost。
<