What Is Univariate Linear Regression? How It Works in ML

What is univariate linear regression? Learn how this machine learning algorithm uses weights and bias to fit a line and predict values from one feature.

Simone Negro, Backend & AI Engineer
6 min read

Linear regression is a statistical method used to model the relationship between variables. In ML (Machine Learning), it is used to predict a target value from one or more input features.

Let’s take the following graph of housing costs as an example:

univariate-linear-regression-table

We can see the regression line passing through the data points.

Linear Regression Equation

The formula is simple and goes as follows: y=wx+by'=wx+b Where:

  • yy': expected output, prediction;
  • ww: weight (the slope of the line);
  • xx: input, the feature value;
  • bb: model bias.

The ww and bb are both calculated during the training session.

Loss and Cost Function

To evaluate this model, we must distinguish between:

  • loss function: computes the error for a single training example;
  • cost function: overall error cost of the entire training set.

Loss Function

The loss function measures the error on a single training example.

loss-linear-regression-table

The two basic forms:

LossEquation
L1L_1 loss (absolute error)yiy^i\lvert y_i - \hat{y}_i \rvert
L2L_2 loss (squared error)(yiy^i)2(y_i - \hat{y}_i)^2

Where:

  • yiy_i: the actual (observed) value of the ii-th sample;
  • y^i\hat{y}_i: the value predicted by the model for the ii-th sample.

Cost Function

The cost function aggregates the loss over the entire training set of NN samples.

cost-function-table

There are three main equations:

CostEquation
MAE (Mean Absolute Error)1Ni=1Nyiy^i\frac{1}{N}\sum_{i=1}^{N} \lvert y_i - \hat{y}_i \rvert
MSE (Mean Squared Error)1Ni=1N(yiy^i)2\frac{1}{N}\sum_{i=1}^{N} (y_i - \hat{y}_i)^2
RMSE (Root Mean Squared Error)1Ni=1N(yiy^i)2\sqrt{\frac{1}{N}\sum_{i=1}^{N} (y_i - \hat{y}_i)^2}

Where:

  • yiy_i: the actual (observed) value of the ii-th sample;
  • y^i\hat{y}_i: the value predicted by the model for the ii-th sample;
  • NN: the total number of samples.

MAE is the mean of the L1L_1 loss, MSE the mean of the L2L_2 loss, and RMSE the square root of MSE.

The most used are MAE and RMSE because they are more human-interpretable: they keep the error on the same scale as the data, instead of inflating it like MSE or the L2L_2 loss.

Gradient Descent

It is an algorithm that iterates over the model’s weights and biases to find the model with the lowest cost. To better understand this, consider the following comparison: the cost function is used to calculate how much the model is off the mark based on all the loss functions; gradient descent, on the other hand, is the algorithm that optimizes the model by adjusting its parameters to minimize that cost.

The process works as follows:

  1. Calculate the cost using the current weights and biases;
  2. Determine in which direction to adjust (and consequently change) the weights and biases to reduce the cost;
  3. Modify the weights and biases to actually reduce the cost;
  4. Return to step 1 and repeat the process until the model reaches the desired low cost level.
repeat until convergence {
	grad_w = ∂J/∂w
	grad_b = ∂J/∂b

	w = w - α * grad_w
	b = b - α * grad_b
}

Where:

  • JJ: cost function (the cost to be minimized);
  • Jw\frac{\partial J}{\partial w}, Jb\frac{\partial J}{\partial b}: gradient indicates the direction of maximum cost growth, so we move in the opposite direction;
  • α\alpha: learning rate controls the step size at each iteration.

gradient-descent

The parameters must be updated simultaneously: calculate both gradients using the old values of ww and bb, then update them.

The above is the general form of gradient descent: it applies to any model, because the gradients remain symbolic. It becomes specific to linear regression when we expand those derivatives using the MSE cost function, J=1Ni=1N(y^iyi)2J = \frac{1}{N}\sum_{i=1}^{N}(\hat{y}_i - y_i)^2, and the model y^i=wxi+b\hat{y}_i = w x_i + b.

When we take the derivative, the exponent 22 of the squared term comes down by the power rule and appears as a factor in the gradients. The cost function itself does not contain any 22; it is produced by the derivative of the square.

Jw=2Ni=1N(y^iyi)xiJb=2Ni=1N(y^iyi)\frac{\partial J}{\partial w} = \frac{2}{N}\sum_{i=1}^{N}(\hat{y}_i - y_i)\,x_i \qquad \frac{\partial J}{\partial b} = \frac{2}{N}\sum_{i=1}^{N}(\hat{y}_i - y_i)

Substituting into the update rule:

w:=wα2Ni=1N(y^iyi)xib:=bα2Ni=1N(y^iyi)w := w - \alpha \, \frac{2}{N}\sum_{i=1}^{N}(\hat{y}_i - y_i)\,x_i \qquad b := b - \alpha \, \frac{2}{N}\sum_{i=1}^{N}(\hat{y}_i - y_i)

With the cost defined as 1N(y^iyi)2\frac{1}{N}\sum(\hat{y}_i - y_i)^2, the factor 22 remains in the gradients, exactly as stated above. Some, however, define the cost as 12N\frac{1}{2N}: in that case, the 12\frac{1}{2} and the 22 in the derivative cancel out, and the gradients do not include the 22. These are two equivalent conventions (in practice, the 22 can still be absorbed into the learning rate α\alpha).

Evaluating the Fit: R²

Loss and cost tell the optimizer how to improve the model, but they are in squared units and hard to read on their own: is an MSE of 1212 good? It depends entirely on the data. To judge how well the model actually fits, we use the coefficient of determination, R2R^2.

It measures the fraction of the variance in the target that the model explains, on a 00-to-11 scale:

  • R2=1R^2 = 1: the model predicts every point perfectly;
  • R2=0R^2 = 0: the model does no better than always predicting the mean yˉ\bar{y};
  • R2<0R^2 < 0: it does worse than that trivial baseline.

R2=1i=1N(yiy^i)2i=1N(yiyˉ)2R^2 = 1 - \frac{\sum_{i=1}^{N}(y_i - \hat{y}_i)^2}{\sum_{i=1}^{N}(y_i - \bar{y})^2}

The numerator is the squared error the model still makes; the denominator is the error of the trivial model that always predicts the mean. So R2R^2 is the share of that baseline error the model removes.

It is called the coefficient of determination because it is the square of the Pearson correlation coefficient rr between the observed values yiy_i and the model’s predictions y^i\hat{y}_i:

r=i=1N(yiyˉ)(y^iy^ˉ)i=1N(yiyˉ)2  i=1N(y^iy^ˉ)2R2=r2r = \frac{\sum_{i=1}^{N}(y_i - \bar{y})(\hat{y}_i - \bar{\hat{y}})}{\sqrt{\sum_{i=1}^{N}(y_i - \bar{y})^2}\;\sqrt{\sum_{i=1}^{N}(\hat{y}_i - \bar{\hat{y}})^2}} \qquad R^2 = r^2

Equivalently, r=cov(y,y^)σyσy^r = \dfrac{\operatorname{cov}(y, \hat{y})}{\sigma_y\,\sigma_{\hat{y}}}: covariance of observed and predicted, normalized by their standard deviations.

With a single feature there is a nice shortcut: since the prediction y^=wx+b\hat{y} = wx + b is just a straight-line transform of xx, the correlation between yy and y^\hat{y} equals the correlation between yy and the feature xx. So in univariate regression “how well the line fits” and “how correlated xx and yy are” are the same question and R2=rxy2R^2 = r_{xy}^2. With more than one feature that shortcut breaks: only the observed-vs-predicted form survives.

Where this goes next

That last caveat is the whole reason for the next step: multivariate linear regression is this same machinery with one weight per feature, fitting a hyperplane instead of a line.

If you’d rather see the math run on real data than on a clean example, the house-price regressor I built trains exactly this with gradient descent on 20,640 California districts — including the scaling gotcha that sent the gradient to NaN. And when the target is a class rather than a number, the line gets squashed through a sigmoid: that’s logistic regression.

References

Get new posts by email

No hype, unsubscribe anytime. · Powered by Buttondown

Or follow along

Shorter takes, half-finished ideas, and whatever I'm building or breaking this week.