Regression in Machine Learning
Linear and Polynomial Regression
1. Problem setting
We consider supervised regression with scalar input and scalar output . Given a dataset , we fit a function from a specified model class and evaluate its agreement with the observed targets.
A central point: a model can be nonlinear in the input while remaining linear in its parameters. Polynomial regression is the canonical example.
2. Data generation model
We generate inputs on an interval and synthesize targets from a function that is mostly linear but contains systematic curvature:
Interpretation:
- provides a dominant linear trend.
- adds oscillatory structure that cannot be captured by a polynomial of very low degree without approximation error.
- adds global curvature.
- injects irreducible noise; no deterministic regressor can interpolate the data without overfitting.
All models below are trained on the same dataset (same random seed and generation procedure) so differences in fit are attributable to the model class, not to resampling.
3. Linear regression (degree 1)
3.1 Model
The linear model is
3.2 Least-squares solution (closed form)
Minimizing yields the standard closed-form solution:
Expected behavior on this dataset- The fit captures the global trend.
- Residuals retain structure (systematic nonlinearity), indicating model misspecification rather than random noise.
- Training is limited by the inability of a line to represent curvature.
Linear regression
4. Quadratic regression (degree 2)
4.1 Model
A quadratic regressor is
This is nonlinear in but linear in parameters .
4.2 Design matrix formulation
Define the design matrix
Then and the least-squares problem is
Expected behavior on this dataset- Adds curvature and typically improves training fit relative to the linear model.
- Still cannot represent oscillations well, but approximates the global quadratic component.
- Training increases because the model class is strictly richer than degree 1.
Quadratic regression (least squares)
5. Cubic regression (degree 3)
5.1 Model
The cubic model is
5.2 Least squares (linear in parameters)
As with degree 2, we construct a polynomial design matrix with columns and solve a linear least-squares problem.
Expected behavior on this dataset- Greater flexibility enables closer tracking of systematic structure.
- Training error typically decreases again.
- Increased flexibility also increases sensitivity to noise, especially near domain boundaries.
Cubic regression (least squares)
6. Degree-5 polynomial regression (degree 5)
6.1 Model
A degree-5 polynomial is
Expected behavior on this dataset- Training fit typically improves further (lower residual norms, higher training ).
- However, improved training metrics do not imply improved test performance.
- High-degree polynomials can show undesirable oscillations and boundary artifacts, especially without regularization.
This section exists to make the bias–variance point concrete: capacity increases training fit monotonically, but generalization is not monotone.
Degree-5 polynomial regression (least squares)
7. Training metrics and interpretation
7.1 Coefficient of determination
For observed targets and predictions , the (training) coefficient of determination is
Key facts:
- measures the fraction of variance (around ) explained by the fitted model on the data evaluated.
- When computed on the training set, usually increases with model flexibility.
- A higher training does not certify better out-of-sample performance.
7.2 L2 error and RMSE (training set)
Two complementary training diagnostics are:
- residual norm:
- RMSE:
Both decrease as the model class becomes more expressive (on training data), but neither prevents overfitting.
Compare L2 error across models (same data)
8. k-Nearest Neighbors (k-NN): a nonparametric local model
Polynomial regression is a parametric approach: the hypothesis class is determined by a fixed set of parameters (the coefficients). In contrast, k-Nearest Neighbors (k-NN) is a nonparametric method: it stores the training set and predicts using local neighborhoods at query time.
8.1 Classification vs. regression
k-NN can be used for both tasks:
- k-NN classification: predict a class label by majority vote among the k nearest training points.
- k-NN regression: predict a real value by averaging the neighbors’ targets (often with distance weights).
In both cases, the inductive bias is the same: nearby points in input space should have similar outputs.
8.2 Decision boundary geometry
For classification, the k-NN decision boundary is determined by the local arrangement of labeled samples:
- Small k yields a high-variance boundary that can track noise.
- Large k yields a smoother, higher-bias boundary that can wash out fine structure.
A common refinement is distance-weighted voting, which reduces sensitivity to the arbitrary choice of k by giving more influence to closer neighbors. One typical weighting is
where is Euclidean distance and controls how quickly influence decays with distance.
k-NN classification (distance-weighted) and the effect of k
9. Logistic Regression
Logistic regression models
with
Equivalently,
The model is linear in the log-odds, but it produces calibrated probabilities rather than hard decisions.
10. The Logistic Loss
Let . Maximizing the conditional likelihood of labels is equivalent to minimizing the negative log-likelihood, which yields the logistic loss. For data :
This objective is convex in .
9. Demonstration: Logistic Regression Trained by Gradient Descent
The next cell trains logistic regression using NumPy gradient descent and visualizes the decision boundary.