已经学习吴恩达的机器学习四周,但对编程还是不够熟练,所以想重新总结一下自己的编程作业,加强巩固。
在写代码之前一定要搞清楚X、y、theta是几乘几的矩阵。
一元线性回归,步骤:
1、设置代价函数
2、梯度下降,对代价函数求θ的偏导,更新θ的值,迭代更新。
% 代价函数
function J = computeCost(X, y, theta)
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
% ------ my first try ----------
% 使用迭代的方法计算
% for i = 1 : m
% J = J + ((theta(1) + X(i, 2) * theta(2)) - y(i))^2;
% endfor
% J = J / 2 / m;
% ------------------------------
% ------ my second try ---------
% 使用向量化计算
J = sum((X * theta - y) .^ 2) / 2 / m;
% ------------------------------
% =========================================================================
end
% 梯度下降
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_