Machine Learning 线性回归( Linear Regression) Andrew Ng 课程练习 Matlab Script 详细解析
2016-04-09 10:57
537 查看
close all;
clear;
clc;
clf;
%该脚本尾部有原数据
% /media/heliheng/Resource/deep_learning/matMLLRex
X = load('ex2x.dat');
Y = load('ex2y.dat');
plot(X, Y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
% store the number of training examples 训练样列的数目
m = length(Y);
% Add a column of ones to X 考虑到偏置项,加上一列ones(m,1),即m行1列的单位矩阵
X = [ones(m, 1), X];
%X m行2列 [mx2] the dimensions of the matrix X
theta = zeros(size(X(1,:)));
% theta <= theta 的转置
theta = theta';
%theta 2行1列 [2X1] the dimensions of the matrix theta
MAX_ITR = 1500;
alpha = 0.07;
for num_iterations = 1:MAX_ITR
grand = (1/m)*X'*(X*theta-Y);
theta = theta-alpha*grand;
end
theta
%%
% Plot the linear fit
% keep previous plot visible
hold on;
%X*theta = X0*theta0+X1*theta1+...+Xj*thetaj+...+Xm*thetam
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
% don't overlay any more plots on this figure
hold off
%%
% the predicted height
%the predicted height for age 3.5 predict1 : 年龄3.5的预测身高值
predict1= [1,3.5]*theta
%the predicted height for age 7 predict2 : 年龄7的预测身高值
predict2=[1,7]*theta
%%
% Calculate J matrix
% Grid over which we will calculate J
%-3到3之间的数值被100等分
theta0_vals = linspace(-3, 3, 100);
%-1到1之间的数值被100等分
theta1_vals = linspace(-1, 1, 100);
% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
%T:[2X1] T=[-3;-1] or [-3;-0.979798] or ......[3;1] 等有100x100种取值;
T = [theta0_vals(i); theta1_vals(j)];
% J_vals:[100X100]
J_vals(i,j) = (0.5/m) * (X *T- Y)'* (X * T - Y);
end
end
J_vals = J_vals';
% Plot the surface plot
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');
figure;
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
%x=logspace(a,b,n)生成有n个元素的对数等分行向量x,且x(1)=10的a次方,x(n)=10的b次方;
%生成从10的a次方到10的b次方之间按对数等分的n个元素的行向量。n如果省略,则默认值为50
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1')
%{
X : [50x1] double
2.3684
2.5400
2.5421
2.5491
2.7867
2.9117
3.0356
3.1147
3.1582
3.3276
3.3793
3.4122
3.4216
3.5316
3.6393
3.6733
3.9256
4.0499
4.2483
4.3440
4.3827
4.4231
4.6102
4.6881
4.9777
5.0360
5.0685
5.4161
5.4396
5.4563
5.5698
5.6016
5.6878
5.7216
5.8539
6.1978
6.3511
6.4797
6.7384
6.8638
7.0223
7.0782
7.1514
7.4664
7.5974
7.7441
7.7730
7.8265
7.9306
Y : [50x1] double
0.7792
0.9160
0.9054
0.9057
0.9390
0.9668
0.9644
0.9145
0.9393
0.9607
0.8984
0.9121
0.9424
0.9662
1.0527
1.0144
0.9597
0.9685
1.0766
1.1455
1.0341
1.0070
0.9668
1.0896
1.0634
1.1237
1.0323
1.0874
1.0703
1.1606
1.0778
1.1070
1.0972
1.1649
1.1412
1.0844
1.1252
1.1168
1.1971
1.2069
1.1251
1.1236
1.2133
1.2523
1.2497
1.1800
1.1897
1.3030
1.2601
1.2562
%}
clear;
clc;
clf;
%该脚本尾部有原数据
% /media/heliheng/Resource/deep_learning/matMLLRex
X = load('ex2x.dat');
Y = load('ex2y.dat');
plot(X, Y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
% store the number of training examples 训练样列的数目
m = length(Y);
% Add a column of ones to X 考虑到偏置项,加上一列ones(m,1),即m行1列的单位矩阵
X = [ones(m, 1), X];
%X m行2列 [mx2] the dimensions of the matrix X
theta = zeros(size(X(1,:)));
% theta <= theta 的转置
theta = theta';
%theta 2行1列 [2X1] the dimensions of the matrix theta
MAX_ITR = 1500;
alpha = 0.07;
for num_iterations = 1:MAX_ITR
grand = (1/m)*X'*(X*theta-Y);
theta = theta-alpha*grand;
end
theta
%%
% Plot the linear fit
% keep previous plot visible
hold on;
%X*theta = X0*theta0+X1*theta1+...+Xj*thetaj+...+Xm*thetam
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
% don't overlay any more plots on this figure
hold off
%%
% the predicted height
%the predicted height for age 3.5 predict1 : 年龄3.5的预测身高值
predict1= [1,3.5]*theta
%the predicted height for age 7 predict2 : 年龄7的预测身高值
predict2=[1,7]*theta
%%
% Calculate J matrix
% Grid over which we will calculate J
%-3到3之间的数值被100等分
theta0_vals = linspace(-3, 3, 100);
%-1到1之间的数值被100等分
theta1_vals = linspace(-1, 1, 100);
% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
%T:[2X1] T=[-3;-1] or [-3;-0.979798] or ......[3;1] 等有100x100种取值;
T = [theta0_vals(i); theta1_vals(j)];
% J_vals:[100X100]
J_vals(i,j) = (0.5/m) * (X *T- Y)'* (X * T - Y);
end
end
J_vals = J_vals';
% Plot the surface plot
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');
figure;
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
%x=logspace(a,b,n)生成有n个元素的对数等分行向量x,且x(1)=10的a次方,x(n)=10的b次方;
%生成从10的a次方到10的b次方之间按对数等分的n个元素的行向量。n如果省略,则默认值为50
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1')
%{
X : [50x1] double
2.3684
2.5400
2.5421
2.5491
2.7867
2.9117
3.0356
3.1147
3.1582
3.3276
3.3793
3.4122
3.4216
3.5316
3.6393
3.6733
3.9256
4.0499
4.2483
4.3440
4.3827
4.4231
4.6102
4.6881
4.9777
5.0360
5.0685
5.4161
5.4396
5.4563
5.5698
5.6016
5.6878
5.7216
5.8539
6.1978
6.3511
6.4797
6.7384
6.8638
7.0223
7.0782
7.1514
7.4664
7.5974
7.7441
7.7730
7.8265
7.9306
Y : [50x1] double
0.7792
0.9160
0.9054
0.9057
0.9390
0.9668
0.9644
0.9145
0.9393
0.9607
0.8984
0.9121
0.9424
0.9662
1.0527
1.0144
0.9597
0.9685
1.0766
1.1455
1.0341
1.0070
0.9668
1.0896
1.0634
1.1237
1.0323
1.0874
1.0703
1.1606
1.0778
1.1070
1.0972
1.1649
1.1412
1.0844
1.1252
1.1168
1.1971
1.2069
1.1251
1.1236
1.2133
1.2523
1.2497
1.1800
1.1897
1.3030
1.2601
1.2562
%}
相关文章推荐
- matlab正则表达式
- 关于matlab程序打包成exe可执行文件或者jar包的常见问题
- 关于matlab程序打包成的EXE文件或者jar包中访问MySQL数据库的问题
- MATLAB中数值拟合的种种办法
- De Bruijn序列的matlab暴力生成
- [转载]基于Matlab的标记分水岭分割算法
- MATLAB 串口通信的实现。。
- imadjust,imcomplement
- 自定义功能函数-matlab
- parfor, callback, real-time data reading
- Matlab中设置条件断点不会停
- MATLAB中如何读取同一路径下所有txt或mat文件小程序
- matlab 求解线性方程组之LU分解
- crossvalind Matlab 交叉验证
- Matlab 实例分析
- 遗传算法Matlab源程序
- 关于matlab用export_fig去掉白边、保存图像等问题
- Deep Learning for Computer Vision with MATLAB and cuDNN(译文)
- PCA (主成分分析)详解 (写给初学者) 结合matlab
- Matlab设置Legend横排、分块