It’s been a tough week, I needed some time to comprehend the formulas and get used to it. I am not alone in this, surprisingly phd student also had a hard time with this course.
Last week, we learnt how to find a best fit line for graph with one variable, this week, we find best fit line for graph with multiple variables. For problems with multiple variables, it doesn’t produce a straight line, but an equation that best fit the features, with .
The goal is to find an equation in the form:
are features, are parameters, is the value.
The equation can be condensed into vector form (meaning matrix) as follows, in the equation means transpose:
We have to find the values of theta that gives the least difference between and to produce the linear regression line. We have two methods to do this, first, cost function mentioned last week, second, normal equations.
or in vectorized form:
For the cost function to work, we have to change the values of theta for each iteration until it reaches the minimum value of . This is done by gradient descent, introduced last week:
The parameters are updated using gradient descent, remember the parameters must be updated simultaneously and use temporary variable to store the updated value before changing the theta itself. is the learning rate, low learning rate needs more iteration for the cost function to converge, high learning rate is faster, but risk not converging at all.
After getting the theta values that give the lowest , the “errors”, we can substitute back into the equation to get the linear regression line.
One point to note, the features, or may have different scales, for example, we have and number of bedrooms as our features, the former may range from 0 to 2000, while the later may range from 0 to 5. It will be wise to normalize the features into approximately a range. This can be done by subtracting the mean of the feature from the data and divide by the range or standard deviation.
Normal equation is a better way than cost function to find the values of theta.
Normal equation doesn’t require us to do feature scaling and there is no need to find sum via loop as in gradient descent.
This week, we used octave to plot the linear regression line that maps the relationship between house price and features such as size of the house and number of bedrooms. This is done through gradient descent and normal equation. Using the linear regression line, we predict the house price given the size of the house and number of bedrooms.
Octave is optimized to deal with matrix, and operations involving matrix such as basic operation, inverse, transpose, etc.