Skip to main content

Fixed: "TypeError: got an unexpected keyword argument 'squared'" in scikit-learn mean_squared_error

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

mean_squared_error(y_true, y_pred, squared=False) is how many tutorials calculate RMSE. In scikit-learn 1.9.1 it fails with TypeError: got an unexpected keyword argument 'squared': the argument was deprecated in scikit-learn 1.4 and has since been removed. root_mean_squared_error() does the same calculation.

What I Was Doing​

I was calculating a regression model's RMSE, root mean squared error, for How to Calculate MAE, RMSE and R² in scikit-learn. Many tutorials compute it by asking mean_squared_error for the square root with squared=False. I ran that line with three small sample values, in a uv project with scikit-learn 1.9.1 on Python 3.15.2:

rmse_squared.py
from sklearn.metrics import mean_squared_error

y_true = [2.0, 3.5, 1.2]
y_pred = [2.4, 3.1, 1.0]
print("RMSE:", mean_squared_error(y_true, y_pred, squared=False))
Terminal
uv run python rmse_squared.py

What I Expected​

One number: the RMSE of the three predictions.

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/mlflow-demo/rmse_squared.py", line 5, in <module>
print("RMSE:", mean_squared_error(y_true, y_pred, squared=False))
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/you/source/mlflow-demo/.venv/lib/python3.15/site-packages/sklearn/utils/_param_validation.py", line 196, in wrapper
params = func_sig.bind(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.15/lib/python3.15/inspect.py", line 3256, in bind
return self._bind(args, kwargs)
~~~~~~~~~~^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.15/lib/python3.15/inspect.py", line 3245, in _bind
raise TypeError(
'got an unexpected keyword argument {arg!r}'.format(
arg=next(iter(kwargs))))
TypeError: got an unexpected keyword argument 'squared'

A terminal running uv run python rmse_squared.py, with a traceback through sklearn's _param_validation.py and Python's inspect.py ending in TypeError: got an unexpected keyword argument 'squared'.

What I Checked​

Which scikit-learn was installed.

Terminal
uv pip show scikit-learn
Output
Name: scikit-learn
Version: 1.9.1
Location: /Users/you/source/mlflow-demo/.venv/lib/python3.15/site-packages
Requires: joblib, narwhals, numpy, scipy, threadpoolctl
Required-by: mlflow, skops

Where the error is raised. The traceback goes through scikit-learn's argument checking, _param_validation.py, into Python's own inspect.py, which raised the TypeError while matching the call's arguments to the function's parameters. So mean_squared_error in 1.9.1 has no parameter called squared; the function never ran.

scikit-learn's documentation. The scikit-learn 1.5 page for mean_squared_error still lists the parameter, with a notice: "Deprecated since version 1.4: squared is deprecated in 1.4 and will be removed in 1.6. Use root_mean_squared_error instead to calculate the root mean squared error." The root_mean_squared_error page says "Added in version 1.4."

Why It Happens​

squared=False was scikit-learn's old way to get RMSE out of mean_squared_error. Version 1.4 added a function just for it, root_mean_squared_error, and deprecated the argument; the deprecation said it would go in 1.6, and in 1.9.1 it's gone. Code written for an older scikit-learn, including many tutorials, still passes it, and Python refuses an argument the function doesn't have.

The Fix​

Use root_mean_squared_error:

rmse_fixed.py
from sklearn.metrics import root_mean_squared_error

y_true = [2.0, 3.5, 1.2]
y_pred = [2.4, 3.1, 1.0]
print("RMSE:", root_mean_squared_error(y_true, y_pred))

It takes the same y_true and y_pred, plus the same optional sample_weight and multioutput, and returns the square root of the mean squared error.

After the Fix​

Terminal
uv run python rmse_fixed.py
Output
RMSE: 0.34641016151377535

That's the right answer: the misses are 0.4, 0.4 and 0.2, their squares average 0.12, and √0.12 ≈ 0.3464.

Common Mistakes​

MistakeWhat happensDo this instead
Pinning an old scikit-learn to keep squared=FalseYou miss every fix and feature sinceSwitch to root_mean_squared_error
Writing mean_squared_error(...) ** 0.5 everywhereIt works, but repeats what scikit-learn already providesUse root_mean_squared_error
Using mean_squared_error and calling it RMSEThe number is the squared error, in squared unitsUse root_mean_squared_error for RMSE

Troubleshooting​

SymptomLikely causeFix
TypeError: got an unexpected keyword argument 'squared'scikit-learn without the squared argumentroot_mean_squared_error(y_true, y_pred)

FAQ​

Did mean_squared_error change too?​

Not in what it returns: called without squared, it still gives the mean squared error.

Does the same apply to mean_squared_log_error?​

Yes. In scikit-learn 1.9.1, mean_squared_log_error has no squared parameter either: its parameters are y_true, y_pred, sample_weight and multioutput. For the root of it, use root_mean_squared_log_error.

Conclusion​

TypeError: got an unexpected keyword argument 'squared' means the code was written for a scikit-learn that still had mean_squared_error(..., squared=False). The argument was deprecated in 1.4 and removed; in 1.9.1, root_mean_squared_error(y_true, y_pred) gave the RMSE, 0.34641016151377535, in one call.

Additional Resources​