Key Concepts
Code cheat-sheet
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
lr = LinearRegression().fit(X_train, y_train)
y_pred = lr.predict(X_test)
print("MSE:", mean_squared_error(y_test, y_pred))
print("R²: ", r2_score(y_test, y_pred))Math
Sum of squared errors. Lower = closer fit. Sensitive to outliers because errors are squared.
SSE averaged over n — comparable across datasets of different sizes.
Fraction of variance explained. 1 = perfect; 0 = no better than the mean; negative = worse than the mean.
Self-check
Gotchas
Baseline first. Always compute the score of predicting the mean. If your model can't beat that, the features are weak or the model is mis-specified.
Interpretability ≠ accuracy. Linear coefficients are readable but often miss non-linear structure. Don't keep a linear model just because it's interpretable if the residuals are clearly curved.