# Why transpose of independent feature matrix is necessary in case of linear regression?

I can follow classical linear regression steps:

$$Xw=y$$

$$X^{-1}Xw=X^{-1}y$$

$$Iw=X^{-1}y$$

$$w=X^{-1}y$$

However, on implementing in Python, I see that instead of simply using

w = inv(X).dot(y)


they apply

w = inv(X.T.dot(X)).dot(X.T).dot(y)


What is the explanation of the transpositions and the two times multiplication here? I'm confused...

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)$$

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.