This post provides a birdsâ€™ eye view of how to calculate linear regression using the numerical programming used by machine-learning people.Â It is, of course, easier to do the linear regression in a statistics program but it is good to know and the overall structure probably provide a foundation for other machine-learning programs.

The algorithm works with Octave which is like a free version of MatLab.

Iâ€™ve also only given the birdsâ€™eye view because the code is part of the Machine Learning course at Stanford and writing it is part of the homework â€“ if we release it, studentsâ€™ wonâ€™t have to write it themselves and they wonâ€™t learn how anything.

## Example of linear regression

Imagine you want to buy a second hand car and you collect information on prices for a particularly model with the age and mileage of the vehicle.

The following can compute an equation to predict the price of the car from* either* the age of the vehicle *or* the mileage.Â Using both age and mileage is a multivariate problem which has different algorithms.

## #1 Prepare data

~1 Normally, we would input the data into a table in Excel with the first column being age (or mileage) of the vehicle and the second column being price.

~2 Then we would save the file as a csv (comma separated values) text file which we will call for now mydata.txt

## #2 Load the data into Octave

~1 Start Octave from your list of Start/Programs

~2 Tell Octave where your data is stored.Â For example, cd â€˜c:usersmedataâ€™

~3 Confirm you are in the correct directory by typing â€˜pwdâ€™

~4 Read in the data using these three commands

data = csvread(â€˜mydata.txtâ€™); Â % reads the data file

X= data(:,1); % reads all the rows of the first column of the data (age in our example) into matrix X

y = data(:, 2); % reads all the rows of the second column of the data (prices in our example) into vector y

m=length(y); % counts the number of training examples, or rows (age, price pairs in our example)

## #3 Visualize the data using plot

~1 Find out what the data looks like (inspect the data) byÂ using the plot function

~2 This was part of the homework so letâ€™s say that the commands for a plot were put in separate file which is called with a simple command plot(X, y)

~3 Calls to a function can be made from the command line within Octave or another function.

## #4 Pick out some data to act as a test you have everything correct

~1 Look at the graph and pick two values of X (age of the vehicle in our example)

~2 Estimate by sight the predicted value of Y (the price of the vehicle in our example)

Hang on to these. You will need them at the end!

## Â #5 Set the â€˜settingsâ€™ for the gradient descent

~1 Set the number of iterations and type these in at the command line or put them in another function.Â For example, iterations=1500

~2 Set the learning rate.Â For example, alpha = 0.01

~3 Set up the matrix of theta values (that is, the y intercept and the gradient of the graph. If price= a +b(age), the two theta values are a and b).Â Type in theta= [0;0]. That sets the initial values of both parameters as 0.Â A line like this predicts every price as 0 no matter the age of the vehicle.Â But it is just our starting point!

## #6 Calculate the initial cost function

~1 Calculate the errors in prediction if we treat theta as [0;0], or that is if we treat price as a straight line and always 0.Â The formula is in essence the sum of the square of the prediction errors divided by twice the number of cases.Â I canâ€™t derive this and I am not going to type it in because finding the right formula was probably part of the homework.

~2 Put the code into a function costCompute (X, y, theta) and save as a text file with extension .m (costCompute.m)

~3 This function is called repeatedly later because every time we improve our guess of the parameters (theta or a & b in the regression line), then our prediction errors will decrease.Â Basically, we will stop trying to improve our parameters when we canâ€™t reduce our prediction errors any further.

## #7 Repeatedly calculate new parameters

~1 The goal now is to iteratively improve our guesses of the parameters (theta â€“ i.e., a & b in the regression line). The machine learning specialists call this â€˜learningâ€™ the parameters.Â So, we are starting with [0,0] and we will slowly improve them.

~2Â The formulas for changing the parametesr amount to calculating a minute change and taking it away from the last value.

~3 There is a different formula for the two parameters (a & b) largely because they start off as differently – as aÂ constant, a, and as bx. (It’s mathsâ€¦)

~4 The constant, a, decreases by the alpha (set as low as 0.01 â€“ see above) times the average error in prediction.Â Again the formula is part of the homework so I won’t write it down here.

~5 The slope, b, decreases by the alpha times the average error in the prediction itself multiplied by the original x value.Â I vaguely intuit this.Â Again there is a formula.

~6Â A new cost is calculated with the two new parameters and of course, the cost (think of it as the average prediction error) should have gone down.

## #8 Iterate lots!

~1 The iteration in this example was set at 1500!

~2 I donâ€™t see how else the program ended. Presumably it could also be coded to end when the improvement in the cost (improvement in prediction errors) falls below an pre-stated, acceptable level.

## #9 Print out a linear fit

~1 Overlay a straight line graph on the original plot of the data

~2 Use Octaveâ€™s ‘hold on’ command to keep the old plot as the base

~3 To draw our prediction line, either calculate predicted values or simply calculate the predicted values within the plot command.Â Plot (X, X*theta, â€˜-â€˜)

## #10 Check the answer is reasonable

~1 Find the test data you set up in step 4.

~2 Calculate predicted values using the parameters we have calculated for the two test levels of X (i.e., what prices do we predict for our two ages of vehicle?).

~3 Do they make sense?Â My primary school maths teacher told me ALWAYS to write the answer in plain English!

## #11 Visualize theta values in a 3D plot

~1 Use Octaveâ€™s surf command to visualize the cost (average prediction errors) of each combination of theta values (a & b).

~2 The 3d plot is bowl-shaped and the best combination of (a & b) is at the putative point where the bowl would balance (my rough understanding).

## #12 Visualize the contour plot

~1 Use Octaveâ€™s contour command to visualize the theta-theta graph (all those aâ€™s plotted against all those bâ€™s).

~2 Our final version of a and b should sit in the inner circle as if it was the highest point on the contour map of a mountain.

This is my record of the 12 steps in using gradient descent to do linear regression for a problem such as predicting of the price of a car from its age.Â We need a large data set of recent data and we repeatedly put in values for the y intercept (value of a car when it is brand new) and the slope (the rate the value decreases). (Yeah, I knowâ€¦ a straight line is not necessarily the best model unless we start after the car is a year old).

The program nippily calculates how bad the model is and when it stops getting better, it stops and delivers the parameters for the line.Â We can check the parameters graphically (because like all good data scientists we inspected our data at the beginning).

We can also use the contour and surf functions of Octave to plot the improvements in our estimations so that we can see what the program actually did.

Iâ€™ve written this to make sure I can do it again. I hope it is useful but the code itself is embargoed because otherwise future students of Stanford will not have to do any work and will not learn anything!

CHECK OUT SIMILAR POSTS

Thanks a lot. I am enrolled on the public online version of this course, and got stuck in one homeword. Now I get how can I solve it.

Thank you! Your explanation is clear and concise. You added several facets to my understanding. I am grateful.

Well explained and has bolstered my understanding, thank you loads

Here is a tutorial of Univariate Linear Regression using Octave.

http://www.adeveloperdiary.com/data-science/machine-learning/univariate-linear-regression-using-octave-machine-learning-step-by-step/