Normal Equation

Analytic solution for Linear Regression

Assuming you have these training data points:

x1x_1x2x_2x3x_3x4x_4yy
21045145460
14163240232
15343230315
8522136178

Add an additional column x0x_0 with value 1 and turn the examples into a matrix, and the results into a vector

X=[12104514511406324011534323018522136]y=[460232315178]X = \begin{bmatrix} 1 & 2104 & 5 & 1 & 45 \\ 1 & 1406 & 3 & 2 & 40 \\ 1 & 1534 & 3 & 2 & 30 \\ 1 & 852 & 2 & 1 & 36 \end{bmatrix} y = \begin{bmatrix} 460 \\ 232 \\ 315 \\ 178 \end{bmatrix}

Then you can calculate θ\theta

θ=(XTX)−1XTy\theta = (X^TX)^{-1}X^Ty

In Octave

pinv(X'*X)*X'*y

(use pseudo-inverse to avoid non-invertibility problems with XTXX^TX)

  • Could happen because of redundant (linear dependent) features or too many features for too few data points