RMSE – Root Mean Square Error in Python

RMSE

Hello readers. In this article, we will be focusing on Implementing RMSE – Root Mean Square Error as a metric in Python. So, let us get started!!


What is Root Mean Square Error (RMSE) in Python?

Before diving deep into the concept of RMSE, let us first understand the error metrics in Python.

Error metrics enable us to track the efficiency and accuracy through various metrics as shown below–

  • Mean Square Error(MSE)
  • Root Mean Square Error(RMSE)
  • R-square
  • Accuracy
  • MAPE, etc.

Mean Square error is one such error metric for judging the accuracy and error rate of any machine learning algorithm for a regression problem.

So, MSE is a risk function that helps us determine the average squared difference between the predicted and the actual value of a feature or variable.

RMSE is an acronym for Root Mean Square Error, which is the square root of value obtained from Mean Square Error function.

Using RMSE, we can easily plot a difference between the estimated and actual values of a parameter of the model.

By this, we can clearly judge the efficiency of the model.

Usually, a RMSE score of less than 180 is considered a good score for a moderately or well working algorithm. In case, the RMSE value exceeds 180, we need to perform feature selection and hyper parameter tuning on the parameters of the model.

Let us now focus on the implementation of the same in the upcoming section.


Root Mean Square Error with NumPy module

Let us have a look at the below formula–

RMSE Root Mean Square Error
RMSE

So, as seen above, Root Mean Square Error is the square root of the average of the squared differences between the estimated and the actual value of the variable/feature.

In the below example, we have implemented the concept of RMSE using the functions of NumPy module as mentioned below–

  • Calculate the difference between the estimated and the actual value using numpy.subtract() function.
  • Further, calculate the square of the above results using numpy.square() function.
  • Finally, calculate the mean of the squared value using numpy.mean() function. The output is the MSE score.
  • At the end, calculate the square root of MSE using math.sqrt() function to get the RMSE value.

Example:

import math
y_actual = [1,2,3,4,5]
y_predicted = [1.6,2.5,2.9,3,4.1]

MSE = np.square(np.subtract(y_actual,y_predicted)).mean() 

RMSE = math.sqrt(MSE)
print("Root Mean Square Error:\n")
print(RMSE)

Output:

Root Mean Square Error:

0.6971370023173351

RMSE with Python scikit learn library

In this example, we have calculated the MSE score using mean_square_error() function from sklearn.metrics library.

Further, have calculated the RMSE score through the square root of MSE as shown below:

Example:

from sklearn.metrics import mean_squared_error
import math
y_actual = [1,2,3,4,5]
y_predicted = [1.6,2.5,2.9,3,4.1]

MSE = mean_squared_error(y_actual, y_predicted)

RMSE = math.sqrt(MSE)
print("Root Mean Square Error:\n")
print(RMSE)

Output:

Root Mean Square Error:

0.6971370023173351

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, Stay tuned and till then, Happy Learning!! 🙂