What Is Multivariate Linear Regression? How It Works in ML
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.
We talked about univariate linear regression and saw that it can handle only one feature. Multivariate (or multiple) linear regression can handle many features at once.
Where univariate fits a line, multivariate fits a hyperplane: one weight per feature, plus a single shared bias.
Multivariate Linear Regression Equation
With features, the prediction becomes:
Each feature gets its own weight , and is the bias. In compact vector form:
Where:
- : the feature vector;
- : the weight vector (one weight per feature);
- : the model bias.
Univariate is just the special case where , which collapses back to .
Loss and Cost Function
The loss and cost functions do not change. They only compare a predicted value to the actual value , so they don’t care how many features produced that prediction. The MSE (most used for multivariate) cost is still:
The same goes for R², the evaluation metric we covered in the univariate post: it still works here, but only in its general form, as the square of the Pearson correlation between the observed values and the predictions, not the shortcut between a single feature and the target.
Gradient Descent
The only difference is that now we have one partial derivative per weight, plus one for the bias. Each weight is updated using its own feature :
Where is the value of feature in sample .
As before, all parameters must be updated simultaneously, using the old values:
In pseudocode, the only change from the univariate version is that we loop over the weights, computing every gradient before applying any update:
repeat until convergence {
for j = 1 to n {
grad_w[j] = ∂J/∂w[j]
}
grad_b = ∂J/∂b
for j = 1 to n {
w[j] = w[j] - α * grad_w[j]
}
b = b - α * grad_b
}
Feature Scaling
With univariate regression there is only one feature, so its scale is not a problem. With multiple features it becomes one: real datasets mix features on completely different ranges. Predicting a house price, the number of rooms might span – while the surface area spans –. The model treats those raw numbers as comparable when they are not.
This hurts gradient descent. A feature with a large range produces large gradients, so its weight wants big steps; a feature with a small range wants tiny ones. With a single learning rate shared by all weights, you cannot satisfy both: the cost surface becomes a stretched, narrow valley, and the algorithm zig-zags slowly toward the minimum instead of heading straight for it.
For example, this is without scaling, and produces slower convergence:
This is with scaling and produces faster convergence:

Feature scaling brings every feature onto a similar range, which makes the cost surface more symmetric and convergence faster and more stable.
There are two common methods:
| Method | Formula | Result |
|---|---|---|
| Min-max normalization | range | |
| Standardization (z-score) | mean , std |
Where:
- : feature mean;
- : standard deviation.
Scale every feature before training, and apply the same transformation (the same , , , computed on the training set) to any new data at prediction time.
Choosing the Learning Rate
The learning rate controls how big each step is during gradient descent, and it is the parameter you tune the most:
- too small: model converges, but very slowly, wasting many iterations.
- too large: steps overshoot the minimum; the cost oscillates or even grows, and the model can diverge.
The practical way to pick it is to plot the cost against the number of iterations (the learning curve) for a few values, for example , , , . A good makes the cost decrease smoothly on every iteration. If the curve goes up or oscillates, is too large; if it crawls down, it is too small.
This is also where feature scaling pays off: once all features share a similar range, a single works well for every weight, so finding a good learning rate becomes much easier.
Features and Polynomial Regression
So far we have used features exactly as they come in the dataset, but we are free to build better ones. Predicting a house price from its frontage and depth, instead of feeding both as separate features you could multiply them into a single, more meaningful one, the area:
Choosing and transforming features like this, using what you know about the problem, is called feature engineering, and it often matters more than the model itself.
The same trick lets a linear model fit curves. When the data does not follow a straight line, you can add powers of a feature as new features. A cubic model of one feature is:
If we set , and , this is exactly the multivariate equation from before. The key point is that the model is still linear in the weights , which is what “linear regression” really means, it does not have to be linear in . Other transforms work the same way, for example a square root:
This is also where feature scaling becomes essential. If ranges from to , then ranges up to and up to , so without scaling these derived features sit on wildly different ranges and gradient descent struggles.
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 univariate linear regression? Learn how this machine learning algorithm uses weights and bias to fit a line and predict values from one feature.
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