That answer comes from the set of weights $w$ (or $\theta$) that analytically solves the cost function which is defined to be
$J(\theta) = (X\theta - y)^T (X\theta - y)$
(See here for more info)
Expanding the cost function we get
$J(\theta) = \theta^TX^TX\theta - 2 y^TX\theta + y^Ty$
(Note that all three terms come out to be scalers)
Before we take the next step, we need to brush up on derivatives of matrices
Some common matrix derivative formulas for reference:
$\frac{\partial (AX)}{\partial X} = A^T \ ;\ \frac{\partial (X^TA)}{\partial X} = A \ ;\ \frac{\partial (X^TX)}{\partial X} = 2X \ ;\ \frac{\partial (X^TAX)}{\partial X} = AX+A^TX$
Using those rules we can take the derivative of the cost function with respect to $\theta$
$\frac{\partial J(\theta)}{\partial \theta} = 2 X^TX \theta - 2 X^Ty$
Setting this to 0 we get
$2 X^TX\theta - 2 X^T y = 0$
solving for $\theta$ we get
$\theta = (X^TX)^{-1} X^T y$
Written in code that's
w = inv(X.T.dot(X)).dot(X.T).dot(y)
Hope this helps.