前言:最近二刷了吴恩达的机器学习视频,通过对视频中logistic算法知识点的整理,对该算法的应用有了更深的认识。同时发现logistic算法的应用和线性回归有几分相似,因此重新做了下练习加深自己的理解。在文章最后,附上自己用梯度下降算法实现的logistic算法。
这里列出线性回归和logistic回归算法的重要公式,更直观的认识到二者之间的异同。
未正则化的 logistic算法
plotData.m
这个函数的作用是将所有样本中结果为0和1的样本区分出来。I = find(X)函数会返回X中结果为真的索引号。
function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure
% PLOTDATA(x,y) plots the data points with + for the positive examples
% and o for the negative examples. X is assumed to be a Mx2 matrix.
% Create New Figure
figure; hold on;
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
% 2D plot, using the option 'k+' for the positive
% examples and 'ko' for the negative examples.
%
positive = find(y == 1);
negative = find(y == 0);
plot(X(positive,1),X(positive,2),'k+','MarkerSize',7,'LineWidth',2);
hold on;
plot(X(negative,1),X(negative,2),'ko','MarkerSize',7,'MarkerFaceColor','y');
legend();
% =========================================================================
hold off;
end
sigmoid.m
这个函数实现的就是sigmoid函数ÿ