sklearn rmse in python

To compute Root Mean Squared Error (RMSE) using scikit-learn in Python, you can use the mean_squared_error function from the sklearn.metrics module, and then take the square root of the returned value.

Here's an example of how to do it:

main.py
from sklearn.metrics import mean_squared_error
import numpy as np

# Sample inputs
y_true = np.array([3, -0.5, 2, 7])
y_pred = np.array([2.5, 0.0, 2, 8])

# Compute the RMSE
mse = mean_squared_error(y_true, y_pred)
rmse = np.sqrt(mse)

print("RMSE:", rmse)
257 chars
13 lines

In this example, we're using mean_squared_error to compute the mean squared error between the true values y_true and predicted values y_pred. We then take the square root to obtain the RMSE.

The output of this code will be:

main.py
RMSE: 0.6123724356957945
25 chars
2 lines

Note that mean_squared_error expects inputs as 1D arrays or lists, so you might need to reshape or flatten your data if it's in a different format.

related categories

gistlibby LogSnag