记录使用,参考ng代码,相关参考:http://blog.csdn.net/yangliuy/article/details/18504921
logistic_grad_ascent.m代码
% logistic_grad_ascent.m
function [theta,ll] = logistic_grad_ascent(X,y,alpha,max_iters)
% rows of X are training samples
% rows of Y are corresponding 0/1 values
% output ll: vector of log-likelihood values at each iteration
% ouptut theta: parameters
[m,n] = size(X);
X = [ones(size(X,1),1), X]; % append col of ones for intercept term
theta = zeros(n+1, 1); % initialize theta
for k = 1:max_iters
hx = sigmoid(X*theta);
theta = theta + alpha * X' * (y-hx);
ll(k) = sum( y .* log(hx) + (1 - y) .* log(1 - hx) );
end
end
function a = sigmoid(z)
a = 1.0 ./ (1.0 + exp(-z));
end
logistic_grad_ascent_test.m
function logistic_grad_ascent_test
load q1y.dat
load q1x.dat
alpha = 0.0001;
max_iters = 500;
[th,ll] = logistic_grad_ascent(q1x,q1y,alpha,max_iters);
plot(ll)
end
q1x.dat:http://cs229.stanford.edu/materials/q1x.dat
q1y.dat:http://cs229.stanford.edu/materials/q1y.dat