Mean Squared Error (MSE): Formula and 3 Key Uses

The mean squared error (MSE) is the single most common way to measure how wrong a regression model is. It takes the gap between each prediction and the true value, squares it, and averages the result across every example. That one design choice — squaring — is what gives the MSE its personality: it is smooth, easy to optimise, and unforgiving of large mistakes. If you fit a straight line, train a neural network on a continuous target, or compare two forecasts, the MSE is almost always the number you are minimising, whether you realise it or not.

mean squared error regression loss chart

📌
In one line. The mean squared error is the average of the squared differences between predictions and true values — a smooth, heavily penalising loss that is the default for regression.

What is the MSE?

The mean squared error answers a simple question: on average, how far are my predictions from the truth, and how much should big misses count? For every data point you take the residual — the predicted value minus the actual value — square it so that positive and negative errors do not cancel out, and then average those squares over the whole dataset. Because the errors are squared, the MSE is always positive, it is measured in the square of the target’s units, and a prediction that is twice as far off contributes four times as much to the total. That last property is the heart of the metric: the MSE cares far more about a few large errors than about many small ones.

It shows up under many names. Statisticians call it the MSE; machine-learning engineers often call it the squared loss or L2 loss; and when you take its square root you get the root mean squared error, which is easier to interpret. They are all built from the same squared residuals.

The mean squared error formula

MSE = (1/n) Σ (yi − ŷi

Here n is the number of examples, yi is the true value, and ŷi is the model’s prediction. Read it left to right: take each error, square it, add them all up, and divide by how many there are. The mean squared error formula is deliberately plain — there is nothing to tune inside it, which is part of why it has been the default regression loss for over a century.

See it: how the MSE grows

The blue curve below is the loss contributed by a single prediction as its error moves away from zero; the orange dashed line is the gradient (how hard the loss pushes back). Notice the loss is a parabola — it rises slowly near zero and then steeply, which is the squaring at work.

MSE = error²gradient = 2×error

A worked example

Suppose a model predicts house prices (in $1,000s) for three homes. The true values are 200, 250 and 300; the predictions are 210, 230 and 340. The residuals are −10, +20 and −40, so the squared errors are 100, 400 and 1600. The mean squared error is (100 + 400 + 1600) / 3:

700MSE ($1,000s)²
26.5RMSE ($1,000s)
23.3MAE ($1,000s)
1600Worst term

Look at how much the single $40k miss dominates: its squared error of 1600 is larger than the other two combined. A mean squared error of 700 is hard to read directly because it is in squared thousands of dollars, which is exactly why people report the RMSE of about 26.5 instead. The mean absolute error of 23.3 treats that big miss much more gently.

Why square the errors?

There are two everyday reasons and one mathematical reason to square the residuals. First, squaring removes the sign, so an error of +5 and an error of −5 both count as real mistakes instead of cancelling to zero. Second, squaring makes the MSE punish large errors disproportionately, which is usually what you want — being off by 100 is often much worse than being off by 10 twice.

💡
The deeper reason. Minimising the MSE is equivalent to finding the maximum-likelihood estimate when the noise in your data is Gaussian (normally distributed). That statistical fact is why the MSE, and the least-squares fitting built on it, sits underneath ordinary linear regression.

The gradient of the MSE

∂MSE/∂ŷ = −(2/n) Σ (yi − ŷi)

Optimisers learn by following gradients, and the MSE has an especially friendly one: it is smooth everywhere and proportional to the error itself. A large error produces a large gradient and therefore a large corrective step, while a small error produces a gentle nudge. This clean, continuous gradient is a big reason gradient descent converges quickly on the MSE, and it is one advantage the metric holds over the mean absolute error, whose gradient has a constant magnitude and a kink at zero.

Mean squared error vs MAE vs RMSE

LossFormula coreUnitsReaction to outliers
MSEaverage of error²target²very sensitive
MAEaverage of |error|targetrobust
RMSE√MSEtargetvery sensitive

The practical difference is all about outliers. Because it squares residuals, the MSE (and its square root, RMSE) will chase a few extreme points hard, dragging the model toward them. The mean absolute error shrugs those points off. If your data has genuine, meaningful outliers you must not miss, the MSE is a feature; if it has noisy junk outliers you want to ignore, it is a bug, and MAE or Huber loss is the better choice.

When to use the MSE

Reach for the MSE when your target is continuous, your errors are roughly symmetric, and large mistakes really are proportionally worse than small ones — forecasting demand, predicting prices, or any physical measurement where a big deviation is genuinely costly. It is also the right default whenever you simply want a smooth, well-behaved objective for gradient descent and have no specific reason to prefer something more robust.

⚠️
When to avoid it. Skip the MSE when your dataset is full of noisy outliers you do not want to chase, when the target is skewed and a few huge values would dominate training, or when the problem is actually classification — there you want cross-entropy loss, not a squared error.

Common mistakes to avoid

🚫
Three traps. Do not compare the MSE across datasets with different target scales — the numbers are not comparable. Do not forget it is in squared units; report the RMSE when you need a human-readable figure. And never leave large outliers unexamined, because a single mislabelled point can dominate the entire mean squared error and quietly distort your model.

Another subtle mistake is forgetting to scale your features. The mean squared error itself does not require scaling, but the optimisation that minimises it converges far faster when inputs are standardised, so a slow-training model is often a scaling problem rather than a loss problem.

Mean squared error in Python

import numpy as np
from sklearn.metrics import mean_squared_error

y_true = np.array([200, 250, 300])
y_pred = np.array([210, 230, 340])

mse = mean_squared_error(y_true, y_pred)
print("MSE :", mse)            # 700.0
print("RMSE:", np.sqrt(mse))   # 26.46

# by hand
print(np.mean((y_true - y_pred) ** 2))   # 700.0

In deep learning frameworks it is built in: torch.nn.MSELoss() in PyTorch and loss="mse" in Keras. Both compute exactly the MSE described above and hand its clean gradient back to the optimiser automatically. For the mathematical background, see the Wikipedia mean squared error article.

Putting it into practice

In a real project the MSE is usually your training loss and your validation metric at the same time, but it pays to watch more than one number. A common workflow is to train against the MSE for its smooth gradient, then report the RMSE so stakeholders can read the result in the original units, and also glance at the MAE to see whether a handful of outliers are inflating the score. If the MSE is high but the MAE is low, you almost certainly have a few large misses rather than a generally poor fit, and that diagnosis points you straight at the rows worth investigating. Tracking the metric on a held-out set epoch by epoch also tells you when the model stops improving, which is the signal to stop training before it overfits.

Frequently asked questions

What is the MSE in simple terms?
It is the average of the squared gaps between your model's predictions and the true values. Squaring removes the sign and makes big mistakes count much more than small ones.
Why is the MSE squared instead of just the average error?
Squaring stops positive and negative errors from cancelling out, and it penalises large errors disproportionately, which produces a smooth loss with a clean gradient that gradient descent likes.
What is the difference between MSE and RMSE?
RMSE is simply the square root of the MSE. RMSE is in the same units as the target, so it is easier to interpret, while MSE is in squared units.
Is a lower mean squared error always better?
A lower mean squared error means predictions are closer to the truth on that dataset, but it is only comparable within the same target scale, and a very low training MSE with a high validation MSE signals overfitting.
When should I not use the MSE?
Avoid it when your data has noisy outliers you do not want to chase, when the target is heavily skewed, or when you are doing classification, where cross-entropy loss is the right choice.
Scroll to Top