Linear regression with multiple variables

原创 2016年06月02日 12:57:06

1.Promble:

Suppose you are selling your house and you want to know what a good market price would be. One way to do this is to first collect information on recent houses sold and make a model of housing prices.

The file ex1data2.txt contains a training set of housing prices in Portland, Oregon. The first column is the size of the house (in square feet), the second column is the number of bedrooms, and the third column is the price of the house.

 

此部分采用了2种求解方法,一种方法就是采用的是梯度下降的方法,另一种采用的是正规方程的方法。

 

2.采用梯度下降的方法求解:

    1)step 1: Feature Normalization

By looking at the values(ex1data2.txt), note that house sizes are about 1000 times the number of bedrooms. When features differ by orders of magnitude, first performing feature scaling can make gradient descent converge much more quickly

采用的特征标准化公式:

z-score标准化方法适用于属性A的最大值和最小值未知的情况,或有超出取值范围的离群数据的情况。

    新数据=(原数据-均值)/标准差

MATLAB代码如下:

<span style="font-size:18px;">function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X 
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.
 
% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));      % mean value 均值   size(X,2)  列数
sigma = zeros(1, size(X, 2));   % standard deviation  标准差
 
% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
%               of the feature and subtract it from the dataset,
%               storing the mean value in mu. Next, compute the 
%               standard deviation of each feature and divide
%               each feature by it's standard deviation, storing
%               the standard deviation in sigma. 
%
%               Note that X is a matrix where each column is a 
%               feature and each row is an example. You need 
%               to perform the normalization separately for 
%               each feature. 
%
% Hint: You might find the 'mean' and 'std' functions useful.
%       
  mu = mean(X);       %  mean value 
  sigma = std(X);     %  standard deviation
  X_norm  = (X - repmat(mu,size(X,1),1)) ./  repmat(sigma,size(X,1),1);%新数据=(原数据-均值)/标准差
 
end</span>


2)step 2:Gradient Descent


梯度下降的更新公式:

 theta = theta - alpha / m * X' * (X * theta - y);


此部分Matlab 代码如下:

function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
%   theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
%   taking num_iters gradient steps with learning rate alpha
 
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
 
for iter = 1:num_iters
    
    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta.
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCostMulti) and gradient here.
    %
    theta = theta - alpha / m * X' * (X * theta - y);
    
    % ============================================================
    
    % Save the cost J in every iteration
    J_history(iter) = computeCostMulti(X, y, theta);
    
end
 
end


3.采用正规方程求解

Normal Equations:

the closed-form solution to linear regression is:



Using this formula does not require any feature scaling, and you will get an exact solution in one calculation: there is noloop until convergencelike in gradient descent


Matlab代码如下:

function [theta] = normalEqn(X, y)
%NORMALEQN Computes the closed-form solution to linear regression 
%   NORMALEQN(X,y) computes the closed-form solution to linear 
%   regression using the normal equations.
 
theta = zeros(size(X, 2), 1);
 
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
%               to linear regression and put the result in theta.
%
 
% ---------------------- Sample Solution ----------------------
 
theta = pinv( X' * X ) * X' * y;
 
end


最终运行结果:


可以看出运行到500次左右,采用梯度下降的损失函数的值就收敛了


此练习的完整代码请参见:点击打开链接




版权声明:本文为博主原创文章,未经博主允许不得转载。

机器学习作业之 Linear Regression with Multiple Variables (Week 2)

function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linear regression % J = COMPUT...
  • susser43
  • susser43
  • 2014年11月19日 22:55
  • 716

Linear regression with one variable

1.Promble:        In this part of this exercise, you will implement linear regression with one varia...
  • ZHE123ZHE123ZHE123
  • ZHE123ZHE123ZHE123
  • 2016年05月30日 23:49
  • 425

Coursera机器学习-Week 2-测验:Linear Regression with Multiple Variables

其实做这些题挺烦的,因为吴恩达的这个视频配套的测验全英,还没有汉化参考,我英语差的也是没啥可说,一直无法通晓题意,总是再三出错,这不,折腾了三次才全部做对。这个题每次重做数据和题面都会有所改变,但是思...
  • f_zyj
  • f_zyj
  • 2017年12月27日 19:12
  • 97

Stanford公开课机器学习---week2-1.多变量线性回归 (Linear Regression with multiple variable)

3.多变量线性回归 (Linear Regression with multiple variable)3.1 多维特征(Multiple Features) n 代表特征的数量 x(i)x^{(i)...
  • muzilanlan
  • muzilanlan
  • 2015年05月27日 12:39
  • 1371

Coursera公开课笔记: 斯坦福大学机器学习第四课“多变量线性回归(Linear Regression with Multiple Variables)”

来自:http://www.52nlp.cn/coursera%E5%85%AC%E5%BC%80%E8%AF%BE%E7%AC%94%E8%AE%B0-%E6%96%AF%E5%9D%A6%E7%A...
  • fennvde007
  • fennvde007
  • 2014年07月08日 19:22
  • 1020

Linear Regression with Multiple Variables

Linear Regression with Multiple Variables =====================================multiple-features===...
  • qq_31600497
  • qq_31600497
  • 2015年12月18日 09:59
  • 241

(2)Linear Regression with Multiple Variables

以下内容源自coursera上的machine learning,同时参考了Rachel-Zhang的博客(http://blog.csdn.net/abcjennifer) 上一节...
  • xuexiang0704
  • xuexiang0704
  • 2013年04月29日 21:40
  • 1131

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

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression 1  Linear regression with one variable ...
  • E_pen
  • E_pen
  • 2015年02月03日 23:10
  • 3286

Regression(3)-------Linear Regression with multiple variables

(一)、Multiple Features: 多变量假设:输出由多维输入决定,即输入为多维特征。如下图所示:Price为输出,前面四维为输入: 假设h(x)=θ0+θ1x1+……所谓多...
  • u012796064
  • u012796064
  • 2013年12月01日 10:29
  • 512

[Coursera机器学习]Linear Regression WEEK2编程作业

1 InitIn the fi le warmUpExercise.m, you will find the outline of an Octave/MATLAB function. Modify ...
  • wangjianyu0115
  • wangjianyu0115
  • 2016年09月05日 15:55
  • 2475
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Linear regression with multiple variables
举报原因:
原因补充:

(最多只允许输入30个字)