Exercise: Linear Regression 斯坦福作业二 (转)

题目给出的数据是一组2-8岁男童的身高。x是年龄,y是身高。样本数m=50.

使用gradient descent来做线性回归。

step1:数据准备。

加载数据:

>> x=load(‘ex2x.dat‘);

>> y=load(‘ex2y.dat‘);

可视化数据:

figure % open a new figure window
plot(x, y, ‘o‘);
ylabel(‘Height in meters‘)
xlabel(‘Age in years‘)

效果如下:

我们将模型设定为:

Hθ(x) = θ0 + θ1x

因此,还需要在数据集x的坐标统一加上1,相当于x0=1

m = length(y); % store the number of training examples
x = [ones(m, 1), x]; % Add a column of ones to x

增加了这一列以后,要特别注意,现在年龄这一变量已经移动到了第二列上。

step2:线性回归

先来回忆一下我们的计算模型:

批量梯度下降(注意不是online梯度下降)的规则是:

题目要求:

1、使用步长为0.07的学习率来进行梯度下降。权重向量θ={θ01}初始化为0。计算一次迭代后的权重向量θ的值。

首先初始化权重向量theta

>> theta=zeros(1,2)
theta =     0     0

计算迭代一次时的权重向量:

delta=(x*theta‘)-y;
sum=delta‘*x;
delta_theta=sum*0.07/m;
theta1=theta - delta_theta;

求得的theta1的结果为:

theta1 =    0.0745    0.3800

2、迭代进行gradient descent,直到收敛到一个点上。

测试代码(每100步输出一条theta直线,直到迭代结束):

function [ result ] = gradientDescent( x,y,alpha,accuracy )
%GRADIENTDESCENT Summary of this function goes here
%   Detailed explanation goes here
orgX=x;
plot(orgX, y, ‘o‘);
ylabel(‘Height in meters‘)
xlabel(‘Age in years‘)
m=length(y);
x=[ones(m,1),x];
theta = zeros(1,2);
hold on;
times = 0;
while(1)
    times = times + 1;
    delta=(x*theta‘)-y;
    sum=delta‘*x;
    delta_theta=sum*alpha/m;
    theta1=theta - delta_theta;
    if(all(abs(theta1(:) - theta(:)) < accuracy))
        result = theta;
        break;
    end
    theta = theta1;
    if(mod(times,100) == 0)
        plot(x(:,2), x*theta‘, ‘-‘,‘color‘,[(mod(times/100,256))/256 128/256 2/256]); % remember that x is now a matrix with 2 columns
        % and the second column contains the time info
        legend(‘Training data‘, ‘Linear regression‘);
    end
end

end

效果如下:

可以看到,theta所确定的直线慢慢逼近数据集。

洁净版的代码(只输出最终的theta结果,不输出中间步骤):

function [ result ] = gradientDescent( x,y,alpha,accuracy )
%GRADIENTDESCENT Summary of this function goes here
%   Detailed explanation goes here
orgX=x;
plot(orgX, y, ‘o‘);
ylabel(‘Height in meters‘)
xlabel(‘Age in years‘)
m=length(y);
x=[ones(m,1),x];
theta = zeros(1,2);
hold on;
while(1)
    delta=(x*theta‘)-y;
    sum=delta‘*x;
    delta_theta=sum*alpha/m;
    theta1=theta - delta_theta;
    if(all(abs(theta1(:) - theta(:)) < accuracy))
        result = theta;
        plot(x(:,2), x*theta‘, ‘-‘,‘color‘,[200/256 128/256 2/256]); % remember that x is now a matrix with 2 columns
        % and the second column contains the time info
        legend(‘Training data‘, ‘Linear regression‘);
        break;
    end
    theta = theta1;
end

end

结果:

执行代码

result=gradientDescent(x,y,0.07,0.00000001)

其中0.07为步长(即学习率),0.00000001为两个浮点矩阵的相近程度(即在这个程度内,视为这两个浮点矩阵相等)

收敛时的theta值为:

theta =    0.7502    0.0639

3、现在,我们已经训练出了权重向量theta的值,我们可以用它来预测一下别的数据。

predict the height for a two boys of age 3.5 and age 7

代码如下:

>> boys=[3.5;7];
>> boys=[ones(2,1),boys];
>> heights=boys*theta‘;

执行结果:

heights =

0.9737
    1.1973

4、理解J(θ)

J(θ)是与权重向量相关的cost function。权重向量的选取,会影响cost的大小。我们希望cost可以尽量小,这样相当于寻找cost function的最小值。查找一个函数的最小值有一个简单的方法,就是对该函数求导(假设该函数可导),然后取导数为0时的点,该点即为函数的最小值(也可能是极小值)。梯度下降就是对J(θ)求偏导后,一步一步逼近导数为0的点的方法。

因为我们在此练习中使用的θ向量只有两个维度,因此可以使用matlab的plot函数进行可视化。在一般的应用中,θ向量的维度很高,会形成超平面,因此无法简单的用plot的方法进行可视化。

代码:

function [] = showJTheta( x,y )
%SHOW Summary of this function goes here
%   Detailed explanation goes here
J_vals = zeros(100, 100);   % initialize Jvals to 100x100 matrix of 0‘s
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);
for i = 1:length(theta0_vals)
      for j = 1:length(theta1_vals)
      t = [theta0_vals(i); theta1_vals(j)];
      J_vals(i,j) = sum(sum((x*t - y).^2,2),1)/(2*length(y));
    end
end

% 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
J_vals = J_vals‘
figure;
surf(theta0_vals, theta1_vals, J_vals);
axis([-3, 3, -1, 1,0,40]);
xlabel(‘\theta_0‘); ylabel(‘\theta_1‘)

end

执行:

x=load(‘ex2x.dat‘);
y=load(‘ex2y.dat‘);
x=[ones(length(y),1),x];
showJTheta(x,y);

结果

加上等高线的绘制语句

figure;
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15));
xlabel(‘\theta_0‘); ylabel(‘\theta_1‘);

效果

转自: http://www.cnblogs.com/elaron/archive/2013/05/21/3090724.html

时间: 2024-07-30 03:55:12

Exercise: Linear Regression 斯坦福作业二 (转)的相关文章

Linear Regression ----- Stanford Machine Learning(by Andrew NG)Course Notes

Andrew NG的Machine learning课程地址为:https://www.coursera.org/course/ml 在Linear Regression部分出现了一些新的名词,这些名词在后续课程中会频繁出现: Cost Function Linear Regression Gradient Descent Normal Equation Feature Scaling Mean normalization 损失函数 线性回归 梯度下降 正规方程 特征归一化 均值标准化 Mode

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression 1  Linear regression with one variable In thispart of this exercise, you will implement linear regression with one variableto predict profits for a food truck. Suppose you are the CEO of a rest

机器学习 1 linear regression 作业(二)

机器学习 1 linear regression 作业(二) 这个线性回归的作业需要上传到https://inclass.kaggle.com/c/ml2016-pm2-5-prediction 上面,这是一个kaggle比赛的网站.第一次接触听说这个东西,恰好在京东上有一本刚出来的关于这个的书<Python机器学习及实践:从零开始通往Kaggle竞赛之路>.把我自己写的代码运行保存的结果提交上去后发现,损失函数值很大,baseline是6,而我的却是8,于是很不心甘,尝试了其他方法无果后,准

二、Linear Regression 练习(转载)

转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2961660.html 前言 本文是多元线性回归的练习,这里练习的是最简单的二元线性回归,参考斯坦福大学的教学网http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex2/ex2.html.本题给出的是50个数据样本点,其中x为这50个小朋

转载 Deep learning:二(linear regression练习)

前言 本文是多元线性回归的练习,这里练习的是最简单的二元线性回归,参考斯坦福大学的教学网http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex2/ex2.html.本题给出的是50个数据样本点,其中x为这50个小朋友到的年龄,年龄为2岁到8岁,年龄可有小数形式呈现.Y为这50个小朋友对应的身高,当然也是小数形式表示的.现在的问题是要根据这50个训练样本,估

ufldl学习笔记与编程作业:Linear Regression(线性回归)

ufldl出了新教程,感觉比之前的好.从基础讲起.系统清晰,又有编程实践. 在deep learning高质量群里面听一些前辈说.不必深究其它机器学习的算法.能够直接来学dl. 于是近期就開始搞这个了,教程加上matlab编程,就是完美啊. 新教程的地址是:http://ufldl.stanford.edu/tutorial/ 本节学习链接:http://ufldl.stanford.edu/tutorial/supervised/LinearRegression/ 从一个最简单的线性回归,能够

机器学习 1 linear regression 作业

机器学习 1 linear regression 作业 话说学机器学习,不写代码就太扯淡了.好了,接着上一次的线性回归作业. hw1作业的链接在这: http://speech.ee.ntu.edu.tw/~tlkagk/courses/ML_2016/Lecture/hw1.pdf 作业是预测台湾的PM2.5的指数,既然是回归问题,肯定是用的是上一节课的线性回归了. 以上数据我传到https://pan.baidu.com/s/1dFhwT13 上面了,供有兴趣的人做做. 实际上上述中分为训练

Coursera machine learning 第二周 编程作业 Linear Regression

必做: [*] warmUpExercise.m - Simple example function in Octave/MATLAB[*] plotData.m - Function to display the dataset[*] computeCost.m - Function to compute the cost of linear regression[*] gradientDescent.m - Function to run gradient descent 1.warmUpE

Andrew Ng Machine Learning 专题【Linear Regression】

此文是斯坦福大学,机器学习界 superstar - Andrew Ng 所开设的 Coursera 课程:Machine Learning 的课程笔记. 力求简洁,仅代表本人观点,不足之处希望大家探讨. 课程网址:https://www.coursera.org/learn/machine-learning/home/welcome Week 3: Logistic Regression & Regularization 笔记:http://blog.csdn.net/ironyoung/ar