Machine Learning 线性回归( Linear Regression) Andrew Ng 课程练习 Matlab Script 详细解析

6 篇文章 0 订阅
5 篇文章 0 订阅
这篇博客详细解析了Andrew Ng的机器学习课程中的线性回归部分,通过Matlab脚本来实现。文章提供了一段清理屏幕并清除图形窗口的代码,并指出在脚本的末尾包含原始数据。
摘要由CSDN通过智能技术生成
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


%}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值