Linear regression with multiple variables

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


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.





    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





<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);%新数据=(原数据-均值)/标准差

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);


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


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;





斯坦福 机器学习Andrew NG 第二讲 Linear Regression with multiple variables

Linear Regression with multiple variables 多参数线性回归 1·Multiple features 引例:同第一讲中一样,依然用房屋售价问题为例。不同的是,...
  • lvpyuan
  • lvpyuan
  • 2014年11月25日 16:46
  • 579

Machine Learning:Linear Regression With Multiple Variables

Machine Learning:Linear Regression With Multiple Variables 接着上次预测房子售价的例子,引出多变量的线性回归。 接着上次预测房子售...

【Stanford机器学习笔记】2-Linear Regression with Multiple Variables

【Stanford机器学习笔记】2-Linear Regression with Multiple Variables

Cousera-stanford-机器学习练习-第二周-Linear Regression with Multiple Variables

Linear Regression with Multiple Variables 5 试题 1。 Suppose m=4 students have taken some class, and...

机器学习之2-多变量线性回归(Linear Regression with Multiple Variables)

1.多维特征多个变量的模型: 特征的数量:n训练集实例:代表第 i 个训练实例,是特征矩阵中的第 i 行,是一个向量(vector)。 代表特征矩阵中第 i 行的第 j 个特征,也就是第 i 个训...

机器学习之多变量线性回归(Linear Regression with multiple variables)

本文引至: 1. Multiple features(多维特征) 在机器学习之...

Machine Learning - IV. Linear Regression with Multiple Variables多变量线性规划 (Week 2)

机器学习Machine Learning - Andrew NG courses学习笔记 linear regression works with multiple variables or wi...

Coursera Machine Learning 第二周 quiz Linear Regression with Multiple Variables 习题答案

1.Suppose m=4 students have taken some class, and the class had a midterm exam and a final exam. You...

机器学习之多变量线性回归(Linear Regression with multiple variables)

本文引至: 1. Multiple features(多维特征) 在机器学习之单变...

Andrew NG机器学习课程笔记系列之——机器学习之多变量线性回归(Linear Regression with multiple variables)

1. Multiple features(多维特征) 在机器学习之单变量线性回归(Linear Regression with One Variable)我们提到过的线性回归中,我们只有一个单一特征量...
您举报文章:Linear regression with multiple variables