我学习了Andrew Ng教授教的机器学习课程。
This is the link
我试着完成这门课的第一个作业。
练习2:线性回归
基于
监督学习问题
1.使用α=0.07的学习率实现梯度下降。由于Matlab/Octave和Octave索引向量从1开始而不是从0开始,您可能会使用Matlab/Octave中的θ(1)和θ(2)来表示θ0和θ1。
我写了一个Matlab代码来解决这个问题:
clc
clear
close all
x = load('ex2x.dat');
y = load('ex2y.dat');
figure % open a new figure window
plot(x, y, '*');
ylabel('Height in meters')
xlabel('Age in years')
m = length(y); % store the number of training examples
x = [ones(m, 1), x]; % Add a column of ones to x
theta = [0 0];
temp=0,temp2=0;
h=[];
alpha=0.07;n=2; %alpha=learning rate
for i=1:m
temp1=0;
for j=1:n
h(j)=theta(j)*x(i,j);
temp1=temp1+h(j);
end
temp=temp+(temp1-y(i));
temp2=temp2+((temp1-y(i))*(x(i,1)+x(i,2)));
end
theta(1)=theta(1)-(alpha*(1/m)*temp);
theta(2)=theta(2)-(alpha*(1/m)*temp2);
我得到答案:
>> theta
theta =
0.0745 0.4545
Here, 0.0745 is exact answer but 2nd one is not accurate.
实际答案
西塔=
0.0745 0.3800号
数据集在链接中提供。有人能帮我解决这个问题吗?