Linear & Polynomial Regression
Fit linear models and polynomial features for continuous targets with clear assumptions.
In plain words
Linear regression draws the best straight line through your data to predict a number; polynomial regression lets that line curve.
Think of it like…
Fitting a straight ruler through scattered points so that the total squared vertical gap is as small as possible; polynomial regression bends a flexible wire instead, which fits better until it starts chasing every wobble.
Where you’ve seen it
Five observations: x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1. The means are x̄ = 3 and ȳ = 6.02. Sxy = Σ(x − 3)(y − 6.02) = 7.84 + 2.12 + 0 + 1.78 + 8.16 = 19.9 and Sxx = 10, so the slope is 19.9 / 10 = 1.99 and the intercept is 6.02 − 1.99 × 3 = 0.05, giving ŷ = 0.05 + 1.99x. Residuals are 0.06, −0.13, 0.18, −0.21, 0.10 (they sum to 0), so SSE = 0.107, SST = 39.708 and R² = 1 − 0.107 / 39.708 = 0.997. In a real house-price model the same method gives a coefficient of $142 per extra square foot, holding bedrooms and age fixed; a residual plot shows a funnel shape (variance grows with price), so the analyst models log(price), which stabilises the variance and makes coefficients approximate percentage effects. A quadratic term for age captures that prices fall for old houses but rise again for historic ones.
Words you’ll hear
- OLS
- Least-squares estimation: minimise Σ(y − ŷ)² over the coefficients.
- Residual
- The difference between the observed and fitted value, y − ŷ.
- R² and adjusted R²
- Share of variance explained; adjusted R² penalises useless extra features.
- Heteroscedasticity
- Residual variance that changes with the fitted value or a feature.
Visual diagram
Theory & deep dive
OLS, interpretation and a worked fit
OLS minimises SSE = Σ(yᵢ − ŷᵢ)². For one feature the solution is slope = Sxy / Sxx and intercept = ȳ − slope × x̄; with several features it's the normal equations β = (XᵀX)⁻¹Xᵀy (solved numerically by QR or SVD, not by explicit inversion). The fitted values are the projection of y onto the column space of X, so residuals are orthogonal to every feature and, with an intercept, sum to zero. R² = 1 − SSE/SST is the share of variance explained (0.997 in the five-point example); adjusted R² and out-of-sample error guard against adding useless features. In statsmodels, OLS(y, sm.add_constant(X)).fit().summary() reports coefficients, standard errors, t-statistics, p-values, confidence intervals and diagnostics; scikit-learn's LinearRegression gives predictions without inference.
Interpret each coefficient as the expected change in y for a one-unit increase in that feature holding the others fixed, which only makes sense when the model is correctly specified and features aren't hopelessly collinear. Standardise features to compare effect sizes, and be careful with causal language: regression coefficients are associations unless the design justifies more. With a log-transformed target, coefficients approximate percentage changes (a coefficient of 0.04 means about +4%); with a log feature, they relate to elasticities.
x = 1..5, y = 2.1, 3.9, 6.2, 7.8, 10.1. Sxy = 19.9, Sxx = 10 → slope 1.99, intercept 0.05. Fitted values 2.04, 4.03, 6.02, 8.01, 10.00; residuals 0.06, −0.13, 0.18, −0.21, 0.10 sum to 0; SSE = 0.107; R² = 0.997.
Assumptions, diagnostics and polynomial terms
The classical assumptions: the mean of y is linear in the parameters, errors are independent (watch time series and clustered data), have constant variance (homoscedasticity) and are roughly normal (needed mainly for small-sample inference), and features are not perfectly collinear. Diagnostics: a residuals-versus-fitted plot should look like a shapeless band (a curve means missing nonlinearity, a funnel means heteroscedasticity); a Q-Q plot checks normality; Cook's distance and leverage reveal influential points; variance inflation factor (VIF) above about 5 to 10 flags multicollinearity that inflates standard errors. Remedies include transforming the target (log), adding terms, robust standard errors, weighted least squares, removing or investigating influential points and regularisation.
Polynomial regression uses PolynomialFeatures(degree=d) followed by linear regression, so a quadratic model is ŷ = β₀ + β₁x + β₂x². Scale x first (powers of large numbers cause numerical trouble and correlated columns), choose the degree by cross-validation (the bias-variance tradeoff: high degrees fit noise and oscillate wildly at the edges), and never extrapolate far beyond the observed range, where polynomials explode. Alternatives for smooth nonlinearity include splines, generalised additive models and tree ensembles. Report prediction intervals (which include noise) as well as confidence intervals for the mean, since users often want the range of a single outcome.
House-price residuals fan out as fitted value grows. Fitting log(price) makes residual spread roughly constant, and a coefficient of 0.0006 per square foot becomes about +0.06% per extra square foot. A quadratic age term captures the U-shaped effect without hand-made bins.
How it works, step by step
Step 1 of 41. Specify and scale
Choose features, add polynomial or interaction terms deliberately and standardise before powers.
- Log y
- if skewed
When to use it & trade-offs
What it is
Linear regression models a numeric target as a weighted sum of features, ŷ = β₀ + β₁x₁ + ... + βₚxₚ, and ordinary least squares (OLS) chooses the coefficients that minimise the sum of squared residuals, with the closed form β = (XᵀX)⁻¹Xᵀy. Polynomial regression is still linear in the coefficients: you add features such as x² and x³ (and interactions) so the fitted curve can bend. The method is interpretable (each coefficient is the expected change in y per unit change in a feature, holding others fixed) and a strong baseline, but it rests on assumptions (linearity, independent errors, constant variance, roughly normal residuals, no severe multicollinearity) that you check with residual diagnostics.
Why it matters: Regression is the foundation for forecasting, pricing, causal-style estimates and almost every other model family. Understanding OLS lets you read coefficients, standard errors and R² correctly, spot when predictions are unreliable (extrapolation, heteroscedasticity, influential points) and choose between simple and flexible fits. In interviews, explaining assumptions and diagnostics distinguishes users of fit() from analysts who trust their results.
When to reach for it
- Baselines and interpretable pricing or demand models
- Estimating effects with confidence intervals
- Modelling smooth curved trends
When to avoid or be careful
- Judging a model by R² alone
- Extrapolating polynomials far beyond the data
- Interpreting coefficients when features are highly collinear
Regression options
Strengths: Simple, fast, coefficients explain effects.
Watch out: Assumes linearity and well-behaved errors.
Strengths: Captures curvature with a linear fit.
Watch out: High degrees overfit and explode outside the data range.
Strengths: Handles complexity and collinearity.
Watch out: Less direct coefficient interpretation.
Interview signals & numbers to know
When this topic comes up
- “What does the coefficient mean?”
- “Residuals fan out”
- Multicollinearity and VIF
- Polynomial features
- Prediction vs confidence intervals
Benchmarks & rules of thumb
- Sxy / Sxx
- OLS slope for one feature
- 19.9 / 10 = 1.99
- slope in the worked example
- 5 – 10
- VIF levels that signal multicollinearity
Common mistakes & follow-ups
Common interview mistakes
- Fitting a degree-8 polynomial to 20 points and trusting the edges.
- Reading p-values from a model with strongly violated assumptions.
- Ignoring that residuals should be checked for patterns, not just summarised.
Likely interviewer follow-ups
- What are the assumptions of linear regression?
- How do you detect and handle heteroscedasticity?
- How do you choose the polynomial degree?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Linear & Polynomial Regression with data science interview Q&A and hands-on practice.