Introduction to Linear Regression
Linear regression models a continuous target as a weighted combination of input features plus an intercept.
Introduction
Linear regression models a continuous target as a weighted combination of input features plus an intercept. It is often the first supervised model teams try because it is fast, interpretable, and sets a baseline for harder algorithms.
Understanding the topic
When to use it Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.
- When to use it — Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.
- Fit and predict — Call fit on training features and targets, then predict on new rows.
| Term | Description |
|---|---|
| Coefficient | Weight applied to each feature. |
| Intercept | Baseline prediction when features are zero. |
| Residual | Difference between actual and predicted value. |
Step-by-step explanation
- When to use it — Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.
- Fit and predict — Call fit on training features and targets, then predict on new rows.
Syntax reference
Notation / API sketch:
y ≈ w1*x1 + w2*x2 + ... + b
Informative example
Python starter:
from sklearn.linear_model import LinearRegressionimport numpy as npX = np.array([[1], [2], [3], [4], [5]])y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])model = LinearRegression().fit(X, y)print(round(model.predict([[6]])[0], 2))
Output
12.04
Execution workflow
When to use it
Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.
Worked examples
Fit and predict
Call fit on training features and targets, then predict on new rows.
pred = model.predict(X_test)print(pred[:3])
Output
[...]
Best practices
- Hold out a test set before hyperparameter tuning.
- Scale numeric columns for distance-based models.
- Track multiple metrics — not accuracy alone on skewed labels.
Common mistakes
- Leaking test statistics into preprocessing fit on full data.
- Training on the same rows you report as test performance.
- Chasing complex models before a simple baseline.
Hands-on exercise
Practice:
- Plot residuals vs predictions
- Compare R² against a dummy mean predictor
Summary
Introduction to Linear Regression — Weighted sum of features for continuous targets.