data=https://bbs.csdn.net/topics/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 and theta2; theta3];
The iteration=500;
Alpha=0.01;
Z=zeros (m, 1);
For j=1:1:50 0
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 (1/m)=theta2 - alpha * * sum (errors. * x (:, 3));
Theta1=t1;
Theta2=t2;
Theta3=t3;
Theta=[theta1 and theta2; theta3];
j=j+1;
End
Iter 1:1:50=0;
The plot (iter, z, b '-');
Data: machine learning course ex1data2 intercepted before 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
What up? Really don't know what to do,,
CodePudding user response:
Function code:function J=costfunctionJ (x, y, theta, m)
Predictions=x * theta;
Sqrerrors=(predictions - y). ^ 2.
J=1/(2 * m) * sum (sqrerrors);