Deep learning Exercise 1 linear regression exercises

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.