matlab上輸入代碼如下:
data = load('ex1data2.txt');
xd = data(:,1:2);y = data(:,3);
m = length(y);
m1=mean(data(:,1));
m2=mean(data(:,2));
m3=mean(data(:,3));
s1=std(data(:,1));
s2=std(data(:,2));
s3=std(data(:,3));
x = [ones(m,1),xd];
x(:,2)=(x(:,2)-m1)/s1;
x(:,3)=(x(:,3)-m2)/s2;
y(:,1)=(y(:,1)-m3)/s3;
theta1 = 0;theta2 = 0;theta3 = 0;
theta = [theta1;theta2;theta3];
iteration = 500;
alpha = 0.01;
z=zeros(m,1);
for j=1:1:500
z(j)=costfunctionJ(x,y,theta,m);
predictions=x*theta;
errors=(predictions-y);
t1=theta1-alpha*(1/m)*sum(errors);
t2=theta2-alpha*(1/m)*sum(errors.*x(:,2));
t3=theta2-alpha*(1/m)*sum(errors.*x(:,3));
theta1=t1;
theta2=t2;
theta3=t3;
theta=[theta1;theta2;theta3];
j=j+1;
end
iter=1:1:500;
plot(iter,z,'b-');資料:機器學習課程ex1data2 截取前30
2104,3,399900
1600,3,329900
2400,3,369000
1416,2,232000
3000,4,539900
1985,4,299900
1534,3,314900
1427,3,198999
1380,3,212000
1494,3,242500
1940,4,239999
2000,3,347000
1890,3,329999
4478,5,699900
1268,3,259900
2300,4,449900
1320,2,299900
1236,3,199900
2609,4,499998
3031,4,599000
1767,3,252900
1888,2,255000
1604,3,242900
1962,4,259900
3890,3,573900
1100,3,249900
1458,3,464500
2526,3,469000
2200,3,475000
2637,3,299900

怎么上升了?實在不知道怎么辦。。
uj5u.com熱心網友回復:
函式的代碼:function J = costfunctionJ(x,y,theta,m)
predictions=x*theta;
sqrerrors=(predictions-y).^2;
J=1/(2*m)*sum(sqrerrors);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/31584.html
上一篇:請教一個問題,bert啟動后不報錯連不上服務,卡主不輸出all set,ready to serve request
