The Mathematics Behind Supervised Machine Learning

The scikit-learn guide teaches a workflow: split the data, fit a model, evaluate it, and improve it. Underneath that uniform fit and predict interface, each model is solving a specific mathematical problem, and each metric is a specific formula. This article works through both. You will see why linear regression has a closed-form solution, why lasso performs feature selection and ridge does not, why logistic regression uses log-loss rather than squared error, and exactly how precision, recall, and the ROC curve are computed.

The supervised learning problem

Supervised learning has one goal: find a function $f$ that maps inputs to outputs, using labelled examples. Given NN training pairs of features 𝐱i\mathbf{x}_i and labels yiy_i, we search for the f that minimises the average loss between predictions and truth.

f∗=arg⁡minf⁡;1N∑i=1NL(f(𝐱i),,yi)f^* = \arg\min_{f} ; \frac{1}{N} \sum_{i=1}^{N} L\big(f(\mathbf{x}_i),, y_i\big)

Where L is a loss function measuring the gap between a prediction and its label.

Every model in the article is a different choice of the family for f and the loss L. Regression uses a continuous loss like squared error; classification uses a loss suited to probabilities. The fit method is this minimisation, and predict evaluates the learned f on new inputs.

K-nearest neighbours: distance and vote

KNN has no training in the usual sense; it stores the data and classifies a new point by the labels of its nearest neighbours. Nearness is measured by the Euclidean distance between two feature vectors.

d(𝐱,𝐱′)=∑j=1p(xj−xj′)2d(\mathbf{x}, \mathbf{x}’) = \sqrt{\sum_{j=1}^{p} (x_j – x’_j)^2}

Where the sum runs over the p features.

To classify a new point, KNN finds the k training points with the smallest distance and takes a majority vote. Suppose k = 3 and a new point’s three nearest neighbours have labels 1, 1, and 0. The prediction is the majority.

y^=mode(1,1,0)=1\hat{y} = \text{mode}(1, 1, 0) = 1

Because distance sums over features directly, a feature measured in thousands will dominate one measured in fractions, which is exactly why KNN requires the feature scaling of a later section.

Linear regression: least squares

Linear regression models the target as a weighted sum of the features plus an intercept.

y^=β0+∑j=1pβjxj\hat{y} = \beta_0 + \sum_{j=1}^{p} \beta_j x_j

The coefficients β\beta are chosen to minimise the residual sum of squares, the total squared gap between predictions and targets.

RSS=∑i=1N(yi−y^i)2\text{RSS} = \sum_{i=1}^{N} \big(y_i – \hat{y}_i\big)^2

Writing the features as a matrix X (with a column of ones for the intercept) and the targets as a vector 𝐲\mathbf{y}, this minimisation has a closed-form solution, the normal equation.

𝜷=(X⊤X)−1X⊤𝐲\boldsymbol{\beta} = (X^\top X)^{-1} X^\top \mathbf{y}

This is why linear regression needs no iterative training: the optimal coefficients are computed directly in one matrix expression.

As a worked example, fit a line y^=β0+β1x\hat{y} = \beta_0 + \beta_1 x to the three points (1,2)(1, 2), (2, 2), and (3, 4)

. The least-squares slope is the covariance of x and y over the variance of x.

β1=∑(xi−x‾)(yi−y‾)∑(xi−x‾)2=(−1)(−0.67)+0+(1)(1.33)2=1\beta_1 = \frac{\sum (x_i – \bar{x})(y_i – \bar{y})}{\sum (x_i – \bar{x})^2} = \frac{(-1)(-0.67) + 0 + (1)(1.33)}{2} = 1

The intercept then places the line through the means.

β0=y‾−β1x‾=2.67−(1)(2)=0.67\beta_0 = \bar{y} – \beta_1 \bar{x} = 2.67 – (1)(2) = 0.67

So the fitted line is y^=0.67+x\hat{y} = 0.67 + x, the line that makes the total squared vertical error as small as possible.

Ridge and lasso: regularisation

Regularisation adds a penalty on the size of the coefficients, discouraging the model from fitting noise. Ridge regression adds the squared size of the coefficients, the L2 penalty, controlled by a strength α\alpha.

RSS+α∑j=1pβj2\text{RSS} + \alpha \sum_{j=1}^{p} \beta_j^2

The larger α\alpha is, the more the coefficients are shrunk toward zero, which reduces variance at the cost of a little bias.

Lasso regression instead penalises the absolute size of the coefficients, the L1 penalty.

RSS+α∑j=1p|βj|\text{RSS} + \alpha \sum_{j=1}^{p} |\beta_j|

The two penalties behave very differently near zero. The squared L2 penalty has a vanishing gradient as a coefficient approaches zero, so ridge shrinks coefficients but rarely sets them exactly to zero. The absolute-value L1 penalty has a constant gradient right up to zero, so lasso can drive coefficients exactly to zero, removing those features entirely. That is the mathematical reason lasso performs feature selection and ridge does not.

Logistic regression: probabilities and log-loss

For binary classification, logistic regression first computes a linear score, then passes it through the sigmoid to produce a probability between 0 and 1.

p^=σ(β0+∑jβjxj)=11+e−(β0+∑jβjxj)\hat{p} = \sigma\big(\beta_0 + \textstyle\sum_j \beta_j x_j\big) = \frac{1}{1 + e^{-(\beta_0 + \sum_j \beta_j x_j)}}

The class boundary is where the probability crosses one half, which is where the linear score is zero.

The model is trained not with squared error but with log-loss, also called binary cross-entropy, which measures the distance between the predicted probability and the true label.

L=−1N∑i=1N[,yilog⁡p^i+(1−yi)log⁡(1−p^i),]L = -\frac{1}{N} \sum_{i=1}^{N} \big[, y_i \log \hat{p}_i + (1 – y_i) \log(1 – \hat{p}_i) ,\big]

Squared error is avoided here because, combined with the sigmoid, it produces a non-convex loss surface full of local minima. Log-loss combined with the sigmoid is convex, so gradient descent reliably finds the single best solution. Unlike linear regression, there is no closed form, so the coefficients are found by iterative optimisation.

Feature scaling

Any method that measures distance or penalises coefficient size is sensitive to the scale of the features. Standardisation rescales each feature to have mean zero and standard deviation one.

x′=x−μσx’ = \frac{x – \mu}{\sigma}

Where μ\mu and σ\sigma are the feature’s mean and standard deviation on the training data.

After this transform, a feature in kilograms and a feature in millimetres contribute comparably to a distance or a penalty. This is why KNN, ridge, and lasso all need scaling, and why it belongs inside a pipeline so the statistics are computed only on the training fold.

Regression metrics: RMSE and R-squared

Once a regression model is fit, two numbers describe its quality. Root mean squared error is the typical size of a prediction error, in the units of the target.

RMSE=1N∑i=1N(yi−y^i)2\text{RMSE} = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (y_i – \hat{y}_i)^2}

The coefficient of determination, R2R^2, expresses how much of the target’s variance the model explains, comparing its error to the error of simply predicting the mean.

R2=1−∑i(yi−y^i)2∑i(yi−y‾)2R^2 = 1 – \frac{\sum_i (y_i – \hat{y}_i)^2}{\sum_i (y_i – \bar{y})^2}

An R2R^2 of 1 is a perfect fit, 0 means the model does no better than predicting the average, and negative values mean it does worse.

Classification metrics: precision, recall, F1

Accuracy alone misleads on imbalanced classes, so classification is judged from the confusion matrix of true positives, false positives, true negatives, and false negatives. Precision asks: of the cases predicted positive, how many really were.

Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}

Recall asks the complementary question: of the actual positives, how many did the model find.

Recall=TPTP+FN \text{Recall} = \frac{TP}{TP + FN}

The F1 score is their harmonic mean, high only when both are high.

F1=2⋅Precision⋅RecallPrecision+RecallF_1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}

As a worked example, suppose a model produces TP=40TP = 40, FP=10FP = 10, FN=20FN = 20, and TN=130TN = 130. Precision is 40/50=0.8040 / 50 = 0.80 and recall is 40/60=0.6740 / 60 = 0.67, so the F1 score is

F1=2⋅(0.80)(0.67)0.80+0.67≈0.73F_1 = 2 \cdot \frac{(0.80)(0.67)}{0.80 + 0.67} \approx 0.73

Accuracy here would be 170/200=0.85170 / 200 = 0.85, comfortably higher than F1, which is exactly how accuracy flatters a model that misses a third of the positives.

The ROC curve and AUC

A probabilistic classifier does not commit to a threshold. The ROC curve traces performance across all thresholds, plotting the true positive rate against the false positive rate.

TPR=TPTP+FN,FPR=FPFP+TN\text{TPR} = \frac{TP}{TP + FN}, \qquad \text{FPR} = \frac{FP}{FP + TN}

As the threshold sweeps from 1 down to 0, each setting produces one point, and the curve connects them.

The area under this curve, the AUC, summarises the whole curve as one number. It has a clean interpretation: the probability that the model ranks a random positive example above a random negative one. An AUC of 1 is perfect ranking, and 0.5 is no better than a coin flip, which is why AUC is the metric of choice when classes are imbalanced and a single threshold would hide the model’s behaviour.

Overfitting as a bias-variance trade-off

The reason regularisation and cross-validation exist is captured in one decomposition. The expected error of a model splits into three parts.

Error=Bias2+Variance+Irreducible noise\text{Error} = \text{Bias}^2 + \text{Variance} + \text{Irreducible noise}

Bias is error from a model too simple to capture the pattern, and variance is error from a model so flexible it fits the noise.

A high-degree model or a KNN with k=1k = 1 has low bias but high variance, memorising the training data and generalising poorly. A heavily regularised model has higher bias but lower variance. Tuning α\alpha, or kk, or the depth of a tree is the search for the point where their sum is smallest, and cross-validation is how you estimate that sum honestly without touching the test set.

The whole picture

Every model in the article is the same template with different choices. KNN chooses a distance and votes. Linear regression chooses a linear function and squared-error loss, solved in closed form. Ridge and lasso add an L2 or L1 penalty, and the shape of that penalty near zero decides whether features are shrunk or dropped. Logistic regression swaps in the sigmoid and log-loss to output convex, well-behaved probabilities. The metrics then measure what the loss could not fully capture, with precision, recall, F1, and AUC guarding against the imbalance that accuracy hides. Seen this way, scikit-learn’s uniform fit and predict interface is a clean surface over a handful of precise optimisation problems, and knowing the mathematics tells you which model, penalty, and metric each situation actually calls for.

See you soon.

View Comments (2)

Leave a Reply

Subscribe to My Newsletter

Subscribe to my email newsletter to get the latest posts delivered right to your email. Pure inspiration, zero spam.