Understanding the math behind regression from .fit method Just to preface, I am very new to machine learning and linear regression.
I have this simple data set with attributes: area, bedrooms, and age...trying to predict the price of a house.
Here is a screenshot of my Jupyter notebook

I am having trouble understanding how these coefficients were calculated (137.25, -26025, -6825). How were these values calculated? I have tried the least-squares solution. This was my work:

As you can see I used the formula $(A^T  A)^{-1} A^T  b$.
The matrix I calculated does not match the coefficients calculated in reg.coef_. What is the math behind these calculations?
 A: Whenever I want to learn how software works, I read the documentation.
In this case, when reading sklearn.linear_model.LinearRegression, I notice that the class includes an intercept by default.

fit_intercept - bool, default=True
Whether to calculate the intercept for this model. If set to False, no intercept will be used in calculations (i.e. data is expected to be centered).

The design matrix you used to compute $\text{coef}=(A^\top A)^{-1}A^\top b$ does not include an intercept term. We would not expect these two models to match in general because you're comparing two different models.
To make the results match, you'll have to do either of two things

*

*Add an intercept to your computations by hand. This means appending a column of 1s to $A$ and then compute $\text{coef}=(A^\top A)^{-1}A^\top b$.

*Remove the intercept from the sklearn model (use fit_intercept=False). (But this is generally inadvisable. See: When is it ok to remove the intercept in a linear regression model?)

Notes that floating point arithmetic is susceptible to roundoff error, so your hand-cranked solutions may not match to a large number of digits (or, in the worst case, you may lose all digits of precision). QR factorization is generally used to compute regression estimates because it is less susceptible to roundoff error (see: Understanding QR Decomposition).
