只贴添加的部分,时间关系有些不是最优雅的解法。。 看不懂请留言
plotData.m
pos = find(y==1); neg =find(y==0); plot(X(pos,1),X(pos,2),'k+','LineWidth',2,'MarkerSize',7); plot(X(neg,1),X(neg,2),'ko','MarkerFaceColor','y','MarkerSize',7);
sigmoid.m
g = 1./(1+exp(-z));
costFunction.m
J = sum(-y.*log(sigmoid(X*theta)) - (1-y).*log(1-sigmoid(X*theta)))/m; grad = (X'*(sigmoid(X*theta)-y))/m;
predict.m
p = sigmoid(X*theta); for i=1:m, if p(i) >= 0.5, p(i) = 1; else p(i) = 0; end; end;
costFunctionReg.m
J = ( log( sigmoid(theta'*X') ) * y + log( 1-sigmoid(theta'*X') ) * (1 - y) )/(-m) + (lambda / (2*m)) * ( ( theta( 2:length(theta) ) )' * theta(2:length(theta)) ); grad = ( X' * ( sigmoid(X*theta)-y ) )/m + ( lambda / m ) * ( [0; ones( length(theta) - 1 , 1 )].*theta );