theta matlab,梯度下降搜索在matlab theta1中的实现

我学习了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号

数据集在链接中提供。有人能帮我解决这个问题吗?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值