Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

MSE, RMSE, and R²: Regression Metrics with scikit-learn in Python

Learn how to compute and interpret MSE, RMSE, and R² for regression models using scikit-learn. Includes code examples and practical guidance for choosing the right metric.

~4 min readPython
#scikit-learn#regression metrics#MSE#RMSE
A visual representation of regression metrics MSE, RMSE, and R² in scikit-learn, showing predicted vs actual values.

Regression models need clear evaluation metrics. This article explains three common scikit-learn metrics: Mean Squared Error (MSE), Root Mean Squared Error (RMSE), and R². You will see how to compute them with sklearn.metrics, how to interpret the values, and how to choose the right metric for a given task.

What MSE, RMSE, and R² Measure

Mean Squared Error (MSE) is the average of the squared differences between predicted and actual values. Because the differences are squared, large errors contribute disproportionately, making MSE sensitive to outliers. Root Mean Squared Error (RMSE) is the square root of MSE, which restores the metric to the same unit as the target variable. R², also called the coefficient of determination, measures the proportion of variance in the target that the model explains. It can range from 1 for a perfect fit down to negative values when the model is worse than simply predicting the mean.

MSE and RMSE quantify the average prediction error, with RMSE expressed in the target unit. R² measures fit relative to a mean-prediction baseline. The three metrics therefore answer different questions about model performance.

Computing MSE and RMSE with scikit-learn

scikit-learn provides mean_squared_error in sklearn.metrics. The default result is the MSE. To get RMSE, take the square root of that value:

python
from sklearn.metrics import mean_squared_error y_true = [3, -0.5, 2, 7] y_pred = [2.5, 0.0, 2, 8] mse = mean_squared_error(y_true, y_pred) rmse = mse ** 0.5 print(f'MSE: {mse:.2f}') print(f'RMSE: {rmse:.2f}')

If your scikit-learn version provides root_mean_squared_error, it is the clearest direct way to get RMSE. Some versions also support mean_squared_error(..., squared=False), but that parameter is deprecated in newer releases.

mean_squared_error accepts arrays of the same shape and supports sample weights through sample_weight. For multioutput regression, multioutput controls how errors are aggregated across targets.

Computing R² with scikit-learn

Use r2_score from sklearn.metrics to compute R²:

python
from sklearn.metrics import r2_score y_true = [3, -0.5, 2, 7] y_pred = [2.5, 0.0, 2, 8] r2 = r2_score(y_true, y_pred) print(f'R²: {r2:.2f}')

Like mean_squared_error, r2_score supports sample_weight. In multioutput regression, its default multioutput='variance_weighted' weights each target by its variance. Set multioutput='raw_values' to get a separate R² for each target, or multioutput='uniform_average' to average them equally.

Interpreting the Metrics in Practice

MSE and RMSE are scale-dependent. An RMSE of 5 is very different for a target ranging from 0 to 10 than for a target ranging from 0 to 1000. These metrics are most useful for comparing models on the same dataset or whenever the target scale is fixed.

R² is unitless and expresses how much better the model is than a baseline that always predicts the mean. An R² of 0.8 indicates that, under the usual variance-based interpretation, the model explains 80% of the variance in the target. R² can be negative when the model is worse than that baseline.

If you are comparing two models on the same data, RMSE tells you which has a smaller typical error in the target's units. Adding R² helps show whether the difference is meaningful relative to the difficulty of the prediction task.

Choosing Between MSE, RMSE, and R²

The right metric depends on the question you need to answer.

MetricUnitScaleOutlier SensitivityInterpretation
MSESquared target unitDependentHighAverage squared error
RMSETarget unitDependentHighAverage error magnitude
R²NoneIndependentAffected by large residuals through squared errorsProportion of variance explained

Use RMSE when you need an error metric in the original target unit. Use MSE when you want to emphasize large errors or when you are evaluating a loss function that uses squared error. Use R² when you want a relative measure of fit against the mean-prediction baseline. Although R² is unitless, it is not automatically comparable across datasets with different target variance, because the baseline changes.

Common Pitfalls and Edge Cases

With mean_squared_error(..., squared=False), the function already returns RMSE; taking the square root again would be wrong. If you use root_mean_squared_error, no extra transformation is needed.

Do not compare R² values across datasets with different target variance. The mean-prediction baseline differs, so R² measures something different in each case. Also remember that R² can be negative, even though many people expect it to stay between 0 and 1.

For multioutput regression, check how multioutput affects the aggregated score. The default 'variance_weighted' can produce a different result than 'uniform_average'. Choose the aggregation that matches your reporting needs.

Performance and Numerical Considerations

The scikit-learn metric functions operate on NumPy arrays and vectorize the calculation internally. They do create intermediate arrays, so memory may matter for very large data; if the data cannot fit in memory, compute errors in chunks and combine them manually. In practice this is rarely necessary for typical regression tasks.

MSE and RMSE have the usual floating-point limitations if target values are extremely large and squared differences exceed the representable range. Scaling the target before computing metrics is the simplest mitigation.

The same error metric is often used as a training loss. For example, MSE-based loss penalizes large errors more than MAE-based loss. This article focuses on evaluation, but the choice of metric can also shape model training.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.