Fit_intercept parameter of sklearn.linear_model.LinearRegression

In a mission: Linear Regression for Machine Learning - Ordinary Least Squares,
it is said “scikit-learn uses OLS under the hood when you call fit() on a LinearRegression instance”.

As i understand, the formula a=(X^TX)^{-1} X^Ty can only calculate slope coefficient, which is equal to LinearRegression(fit_intercept=False).

Could you please explain how the above formula changes when I want to calculate both slope and y-intercept?

Also, in what situation do we need to set fit_intercept to True or False?

The slope isn’t equal to LinearRegression(fit_intercept=False), it’s not a slope.

It doesn’t change, it’s already accounted for. The intercept is the first entry of a. Please review this screen for a more thorough explanation.

Straight from the documentation:

If set to False, no intercept will be used in calculations (i.e. data is expected to be centered).

So if the data is centered, you can pass False to this parameter and it might help with how fast it runs. Otherwise True should be passed, and is in fact the default value.