Machine Learning MultiLinear Regression Andrew Ng 课程练习 Matlab Script 详细解析
来源:互联网 发布:windows sudo rm -rf 编辑:程序博客网 时间:2024/04/30 13:54
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
%}
0 0
- Machine Learning MultiLinear Regression Andrew Ng 课程练习 Matlab Script 详细解析
- Machine Learning 线性回归( Linear Regression) Andrew Ng 课程练习 Matlab Script 详细解析
- Machine Learning Logistic Regression and Newton's Method Andrew Ng 课程练习 Matlab Script 详细解析
- Andrew Ng Machine Learning 专题【Linear Regression】
- Machine Learning课程 by Andrew Ng
- Machine Learning by Andrew Ng ---Linear Regression with one variable
- Machine Learning by Andrew Ng---Linear Regression with multiple variables
- Machine Learning by Andrew Ng --- Logistic Regression with two classes
- Machine Learning by Andrew Ng --- Logistic Regression by using Regularization
- Andrew Ng Machine Learning 专题【Logistic Regression & Regularization】
- Linear Regression代码-Andrew NG Machine Learning Ex2
- Multivariate Linear Regression代码-Andrew NG Machine Learning Ex3
- Machine Learning -- Linear Regression with Multiple Variables(Andrew Ng)
- Machine Learning -- Linear Regression with Multiple Variables(Andrew Ng)
- Machine Learning- overfitting (Andrew Ng 课程总结-过拟合)
- Andrew Ng-Machine learning (1)
- coursera Machine Learning, Andrew Ng
- Machine Learning-Andrew Ng-week3
- Linux下查看某程序的端口占用情况
- OpenCV学习笔记之八(保存视频,录制视频,cvLoadImage的路径)
- Android手势源码浅析------手势的形成(Gesture)
- Picasso源码解析之Lrucache算法源码解析
- 取石子
- Machine Learning MultiLinear Regression Andrew Ng 课程练习 Matlab Script 详细解析
- java实现条件编译
- 《Java程序设计基础》 第8章手记Part 1
- C++ 单位整型计算器,仅支持带括号的四则运算。初学c++第一个个人作品
- 第二季:在UEFI+GPT模式下用U安装系统
- EditText限制输入的小数点只能一个
- 第三季:UEFI SecureBoot ESP MSR等相关名词解释
- 机器学习笔记01:线性回归(Linear Regression)和梯度下降(Gradient Decent)
- GCC与Makefile入门