scikit-learn: A Guide to Calculating Root-Mean-Square Error (RMSE) as a Percentage

Posted by

Calculating Root Mean Square Error (RMSE) in Percentage using scikit-learn library

Calculating Root Mean Square Error (RMSE) in Percentage using scikit-learn library

Root Mean Square Error (RMSE) is a popular metric for evaluating the accuracy of a model’s predictions. It measures the average distance between the predicted values and the actual values in a dataset. However, sometimes it is helpful to express RMSE as a percentage to better understand the magnitude of the errors.

In order to calculate RMSE in percentage using scikit-learn library, you can follow the steps below:

  1. First, import necessary libraries:
  2. <code>
    import numpy as np
    from sklearn.metrics import mean_squared_error
    </code>

  3. Next, calculate the RMSE using the mean_squared_error function:
  4. <code>
    y_true = np.array([3, -0.5, 2, 7])
    y_pred = np.array([2.5, 0.0, 2, 8])
    rmse = np.sqrt(mean_squared_error(y_true, y_pred))
    </code>

  5. Then, calculate the RMSE in percentage:
  6. <code>
    rmse_percentage = (rmse/np.mean(y_true))*100
    </code>

  7. Finally, print out the RMSE in percentage:
  8. <code>
    print('RMSE in percentage:', rmse_percentage)
    </code>

By following these steps, you can now calculate the RMSE in percentage using the scikit-learn library. This can be helpful in understanding the accuracy of your model’s predictions in a more intuitive way.