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.
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:

We can see the regression line passing through the data points.
Linear Regression Equation
The formula is simple and goes as follows: Where:
- : expected output, prediction;
- : weight (the slope of the line);
- : input, the feature value;
- : model bias.
The and 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.

The two basic forms:
| Loss | Equation |
|---|---|
| loss (absolute error) | |
| loss (squared error) |
Where:
- : the actual (observed) value of the -th sample;
- : the value predicted by the model for the -th sample.
Cost Function
The cost function aggregates the loss over the entire training set of samples.

There are three main equations:
| Cost | Equation |
|---|---|
| MAE (Mean Absolute Error) | |
| MSE (Mean Squared Error) | |
| RMSE (Root Mean Squared Error) |
Where:
- : the actual (observed) value of the -th sample;
- : the value predicted by the model for the -th sample;
- : the total number of samples.
MAE is the mean of the loss, MSE the mean of the 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 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:
- Calculate the cost using the current weights and biases;
- Determine in which direction to adjust (and consequently change) the weights and biases to reduce the cost;
- Modify the weights and biases to actually reduce the cost;
- 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:
- : cost function (the cost to be minimized);
- , : gradient indicates the direction of maximum cost growth, so we move in the opposite direction;
- : learning rate controls the step size at each iteration.

The parameters must be updated simultaneously: calculate both gradients using the old values of and , 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, , and the model .
When we take the derivative, the exponent 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 ; it is produced by the derivative of the square.
Substituting into the update rule:
With the cost defined as , the factor remains in the gradients, exactly as stated above. Some, however, define the cost as : in that case, the and the in the derivative cancel out, and the gradients do not include the . These are two equivalent conventions (in practice, the can still be absorbed into the learning rate ).
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 good? It depends entirely on the data. To judge how well the model actually fits, we use the coefficient of determination, .
It measures the fraction of the variance in the target that the model explains, on a -to- scale:
- : the model predicts every point perfectly;
- : the model does no better than always predicting the mean ;
- : it does worse than that trivial baseline.
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 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 between the observed values and the model’s predictions :
Equivalently, : covariance of observed and predicted, normalized by their standard deviations.
With a single feature there is a nice shortcut: since the prediction is just a straight-line transform of , the correlation between and equals the correlation between and the feature . So in univariate regression “how well the line fits” and “how correlated and are” are the same question and . 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
Related
How I predicted California house prices with linear regression trained by gradient descent (SGDRegressor) — mutual-information feature selection, a scaling gotcha that breaks SGD, and an honest benchmark against KNN, a decision tree, and WEKA.
What is multivariate (multiple) linear regression? Learn how this ML algorithm fits a hyperplane with one weight per feature plus a bias to predict values from many inputs.
What is logistic regression? Learn how this classification algorithm uses the sigmoid function, log loss, and gradient descent to predict probabilities and class labels.
Get new posts by email
No hype, unsubscribe anytime. · Powered by Buttondown