Linear regression Exercises
Follow Andrew Ng and do the exercises: http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc= Exercises/ex2/ex2.html
This section does a little exercise in linear regression, with data from the Web site above, where X is the height of the little boy,Y is the age of the little boy, and the dataset consists of 50 training data sets.
1, pretreatment
by x = Load (' Ex2x.dat ');
y = Load (' Ex2y.dat ');
Loading data;
Then generate the unit vector for X
m = Length (y); % store the number of training examples
x = [Ones (M, 1), X]; % Add a column of ones to X
2, Linear regression
The linear regression model is
The parameter batch update rule is
Where the learning rate is set to α=0.07, the parameters are initialized to 0;
The iteration is then started until the theta converges.
Since this thing is very simple, now the direct code is as follows
CLC Clear All;close all;x= Load ('Ex2x.dat'); y= Load ('Ex2y.dat');% Open aNewFigure Windowplot (x, Y,'o');%discrete point Ylabel ('Height in meters') Xlabel ('Age in years') M= Length (y); %Store the number of training Examplesx= [Ones (M,1) x]; % Add a column of ones to x----This is due to f (x) =w'*x+b can be converted to F (X) =[x,1]*[w'; B]a=0.07; Theta= Zeros (Size (x (1,:)))';% parameter includes two, K,,,, b forI=1: theTheta=theta-a./m.*x'* (x*theta-y);% Batch gradient descentEndhold on% PlotNewdata without clearing old Plotplot (x (:,2), X*theta,'-')% remember that X isNow a matrix with2Columns%And the second column contains the time Infolegend ('Training Data','Linear regression')
Deep learning Exercise 1 linear regression exercises