close all;
clear;
%X:[mX2]Dimensions
X= load('ex3x.dat');
%Y:[mX1]Dimensions
Y = load('ex3y.dat');
m = length(Y);
%% add an intercept term,then X:[mX3]Dimensions
X = [ones(m,1),X];
% Save a copy of the unscaled features for later
X_unscaled =X;
mu = mean(X);
X(:,2) = (X(:,2) - mu(2))./ sigma(2);
X(:,3) = (X(:,3) - mu(3))./ sigma(3);
%%
plotstyle = {'b', 'r', 'g', 'k', 'b--', 'r--'};
%initialize learning rate
%alpha = 0.0100 0.0251 0.0631 0.1585 0.3981 1.0000
alpha = logspace(-2, 0,6);
theta_ultimate = zeros(length(alpha),3);
%% compute J(theta) and plot
figure;
for i=1: length(alpha)
% initialize fitting parameters theta:[3X1]Dimensions
theta = zeros(size(X(1,:)))';
J = zeros(50, 1);
for num_iterations = 1:50
% Calculate cost function
J(num_iterations) = (0.5/m).*(X*theta-Y)'*(X*theta-Y);
% Result of gradient descent update
%X'*(X*theta-Y) :[3X1]Dimensions
theta = theta- alpha(i).*(1/m).*(X'*(X*theta-Y));
end
theta_ultimate(i,:) = theta;
plot(0:49, J(1:50), char(plotstyle(i)), 'LineWidth', 2);
hold on
end
%alpha = logspace(-2, 0,6)
%alpha = 0.0100 0.0251 0.0631 0.1585 0.3981 1.0000
legend('0.0100',' 0.0251','0.0631','0.1585','0.3981','1.0000')
xlabel('Number of iterations')
ylabel('Cost J')
theta_ultimate;
%the theta for alpha= 1
theta_special = theta_ultimate(6,:);
% force Matlab to display more than 4 decimal places
% formatting persists for rest of this session
format long
% Estimate the price of a 1650 sq-ft, 3 br house
price_grad_desc = dot(theta_special, [1, (1650 - mu(2))/sigma(2),...
(3 - mu(3))/sigma(3)])
% force Matlab to display more than 4 decimal places
% formatting persists for rest of this session
format long
%Estimate the house price again
price_normal = dot(theta_normal, [1, 1650, 3])
%{
X : [47X2] double
X =
2104 3
1600 3
2400 3
1416 2
3000 4
1985 4
1534 3
1427 3
1380 3
1494 3
1940 4
2000 3
1890 3
4478 5
1268 3
2300 4
1320 2
1236 3
2609 4
3031 4
1767 3
1888 2
1604 3
1962 4
3890 3
1100 3
1458 3
2526 3
2200 3
2637 3
1839 2
1000 1
2040 4
3137 3
1811 4
1437 3
1239 3
2132 4
4215 4
2162 4
1664 2
2238 3
2567 4
1200 3
852 2
1852 4
1203 3
Y : [47X1] double
Y =
399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
239999
347000
329999
699900
259900
449900
299900
199900
499998
599000
252900
255000
242900
259900
573900
249900
464500
469000
475000
299900
349900
169900
314900
579900
285900
249900
229900
345000
549000
287000
368500
329900
314000
299000
179900
299900
239500
%}
clear;
clc;
%%说明;矩阵规模这样表示 e.g. A:[mxn ]
%该脚本的尾部保留有原始数据;
%% load the original data X:[mX2]Dimensions Y:[mX1]Dimensions%X:[mX2]Dimensions
X= load('ex3x.dat');
%Y:[mX1]Dimensions
Y = load('ex3y.dat');
m = length(Y);
%% add an intercept term,then X:[mX3]Dimensions
X = [ones(m,1),X];
% Save a copy of the unscaled features for later
X_unscaled =X;
%% appling feature scaling to the Matrix X
%%这里所谓特征缩放用到了 特征标准化 详情请查看我的另外一篇blog 数据预处理之数据归一化点击打开链接
sigma = std(X);
mu = mean(X);
X(:,2) = (X(:,2) - mu(2))./ sigma(2);
X(:,3) = (X(:,3) - mu(3))./ sigma(3);
%%
plotstyle = {'b', 'r', 'g', 'k', 'b--', 'r--'};
%initialize learning rate
%alpha = 0.0100 0.0251 0.0631 0.1585 0.3981 1.0000
alpha = logspace(-2, 0,6);
theta_ultimate = zeros(length(alpha),3);
%% compute J(theta) and plot
figure;
for i=1: length(alpha)
% initialize fitting parameters theta:[3X1]Dimensions
theta = zeros(size(X(1,:)))';
J = zeros(50, 1);
for num_iterations = 1:50
% Calculate cost function
J(num_iterations) = (0.5/m).*(X*theta-Y)'*(X*theta-Y);
% Result of gradient descent update
%X'*(X*theta-Y) :[3X1]Dimensions
theta = theta- alpha(i).*(1/m).*(X'*(X*theta-Y));
end
theta_ultimate(i,:) = theta;
plot(0:49, J(1:50), char(plotstyle(i)), 'LineWidth', 2);
hold on
end
%alpha = logspace(-2, 0,6)
%alpha = 0.0100 0.0251 0.0631 0.1585 0.3981 1.0000
legend('0.0100',' 0.0251','0.0631','0.1585','0.3981','1.0000')
xlabel('Number of iterations')
ylabel('Cost J')
theta_ultimate;
%the theta for alpha= 1
theta_special = theta_ultimate(6,:);
% force Matlab to display more than 4 decimal places
% formatting persists for rest of this session
format long
% Estimate the price of a 1650 sq-ft, 3 br house
price_grad_desc = dot(theta_special, [1, (1650 - mu(2))/sigma(2),...
(3 - mu(3))/sigma(3)])
% Calculate the parameters from the normal equation
%这里左除法是对左值X_unscaled' * X_unscaled做逆运算再与右值相乘
theta_normal = (X_unscaled' * X_unscaled)\X_unscaled' *Y;% force Matlab to display more than 4 decimal places
% formatting persists for rest of this session
format long
%Estimate the house price again
price_normal = dot(theta_normal, [1, 1650, 3])
%{
X : [47X2] double
X =
2104 3
1600 3
2400 3
1416 2
3000 4
1985 4
1534 3
1427 3
1380 3
1494 3
1940 4
2000 3
1890 3
4478 5
1268 3
2300 4
1320 2
1236 3
2609 4
3031 4
1767 3
1888 2
1604 3
1962 4
3890 3
1100 3
1458 3
2526 3
2200 3
2637 3
1839 2
1000 1
2040 4
3137 3
1811 4
1437 3
1239 3
2132 4
4215 4
2162 4
1664 2
2238 3
2567 4
1200 3
852 2
1852 4
1203 3
Y : [47X1] double
Y =
399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
239999
347000
329999
699900
259900
449900
299900
199900
499998
599000
252900
255000
242900
259900
573900
249900
464500
469000
475000
299900
349900
169900
314900
579900
285900
249900
229900
345000
549000
287000
368500
329900
314000
299000
179900
299900
239500
%}