Stanford机器学习课程 学习笔记1
(LMS(Least Mean Square),Batch Gradient Decent)
代码:
x = [2014, 1600, 2400, 1416, 3000, 3670, 4500]'; %feature1: area
Y = [400, 330, 369, 232, 540, 620, 800]'; %Outcome: price
error = Inf;
threshold = 4300;
alpha = 10^(-10);
theta = [0;0];
X = [zeros(size(x,1),1),x];
J = 1/2 * (X*theta - Y)' * (X*theta - Y);
%%learning algorithm
while error > threshold
%batch gradient descent:
tmp = Y - X*theta;
for j = 1:2
theta(j,1) = theta(j,1) + alpha * (X(:,j)' * tmp);
end
error = 1/2 * (X*theta - Y)' * (X*theta - Y);
end
plot(x,Y,'*');
grid on;
xlabel('Area');
ylabel('Price(1000$)');
hold on;
h = X*theta;
plot(x,h,'r-');
运行结果:
以上为单特征情况下的结果,下面是两个特征:面积,房间数的结果
我觉得多特征情况下几何表示并无什么实际意义,只是更直观一点。
关于程序代码,值得注意的点是threshold和alpha的取值。临界值(threshold)如果太高,拟合结果会过于粗糙,太低则可能导致运行时间过长而没有什么实质改变。学习速率(alpha)则和能否收敛相关,太大了会导致不收敛,太小会导致运行时间过长。