devnotes.

Linear Regression

Linear regression predicts a continuous numeric value as a weighted sum of the input features — the simplest, most interpretable regression model, and a strong baseline to compare more complex models against.

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = LinearRegression()
model.fit(X_train, y_train)             # learns the best-fit weights (coefficients)

predictions = model.predict(X_test)

print(model.coef_)          # the learned weight for each feature
print(model.intercept_)      # the baseline value when all features are 0

import pandas as pd
coefficients = pd.Series(model.coef_, index=X_train.columns)
print(coefficients.sort_values())   # which features push the prediction up or down, and by how much

Linear regression finds the weights (model.coef_) that minimize the total squared difference between predicted and actual values across the training data — each coefficient represents how much the prediction changes for a one-unit increase in that feature, holding every other feature constant, which is exactly what makes linear regression's predictions so directly interpretable compared to more complex models.

Its core assumption — that the relationship between each feature and the target is a straight line — is also its main limitation: real relationships are frequently non-linear (diminishing returns, thresholds, interactions between features), which linear regression simply can't represent no matter how the data is fed to it. Despite this, it remains an excellent first model to fit on any new regression problem: fast to train, easy to explain to a non-technical audience, and a useful baseline that more sophisticated models (covered in later lessons) should meaningfully beat before being preferred.

Linear Regression in scikit-learn | DevNotes