# Python Program for Root Mean Square Error (RMSE)

Root Mean Square Error (RMSE):

The Root Mean Square Error (RMSE) is a method of calculating the difference between a model’s predicted and actual values.

Prior to actually delving into the concept of RMSE, let us first understand Python error metrics.

Error metrics allow us to track efficiency and accuracy using various of metrics.

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

One such error metric for judging the accuracy and error rate of any machine learning algorithm for a regression problem is Mean Square Error(MSE).

As such, MSE is a risk function that allows us to calculate the average squared difference between a feature’s or variable’s predicted and actual value.

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

We can easily plot a difference between the estimated and actual values of a model parameter using RMSE.

This allows us to clearly assess the model’s efficiency.

An RMSE score of less than 180 is usually considered a good score for a moderately or well-functioning algorithm. If the RMSE value exceeds 180, we must perform feature selection and hyper parameter tuning on the model’s parameters.

#### RMSE using Numpy Library

Formula:

As previously stated, Root Mean Square Error is defined as the square root of the average of the squared differences between the estimated and actual value of the variable or feature.

Example:

Approach:

• Import numpy module as np using the import keyword.
• Import math module using the import keyword.
• Give the list of actual values as static input and store it in a variable.
• Give the list of predicted values as static input and store it in another variable.
• Pass the above actual and predicted lists as the arguments to the np.subtract() function to get the difference between the predicted and the actual values.
• Store it in another variable.
• Square the above-obtained difference using the np.square() function.
• Store it in another variable.
• Apply the mean() function to the above obtained squared value to get the mean of the squared value(MSE).
• Store it in another variable.
•  Pass the above obtained mean square error as an argument to the math.sqrt() function to get the Root mean square error.
• Store it in another variable.
• Print the Root mean square error for the given predicted and actual values.
• The Exit of the Program.

Below is the implementation:

# Import numpy module as np using the import keyword
import numpy as np
# Import math module using the import keyword
import math
# Give the list of actual values as static input and store it in a variable.
gvn_actul_vals = [5, 6, 1, 7, 3]
# Give the list of predicted values as static input and store it in another
# variable.
gvn_predictd_vals = [2.4, 1.5, 1.4, 2.7, 3.1]
# Pass the above actual and predicted lists as the arguments to the np.subtract()
# function to get the difference between the predicted and the actual values.
# store it in another variable.
diffrnce = np.subtract(gvn_actul_vals, gvn_predictd_vals)
# Square the above obtained difference using the np.square() function
# store it in another variable.
sqre_err = np.square(diffrnce)
# Apply mean() function to the above obtained squared value to
# get the mean of the squared value(MSE).
# store it in another variable.
rslt_meansqre_err = sqre_err.mean()
# Pass the above obtained mean square error as an argument to the math.sqrt()
# function to get the Root mean square error
# store it in another variable.
root_meansqre_err = math.sqrt(rslt_meansqre_err)
# Print the Root mean square error for the given predicted and actual values
print("The RMSE for the given predicted and actual values = ")
print(root_meansqre_err)


Output:

The RMSE for the given predicted and actual values =
3.0222508168581905

#### RMSE using Scikit learn Library

Example:

Approach:

• Import mean_squared_error function from sklearn.metrics module using the import keyword.
• Import math module using the import keyword.
• Give the list of actual values as static input and store it in a variable.
• Give the list of predicted values as static input and store it in another variable.
• Pass the above actual and predicted lists as the arguments to the mean_squared_error() function to get the Mean Square Error(MSE).
• Store it in another variable.
•  Pass the above obtained mean square error as an argument to the math.sqrt() function to get the Root mean square error.
• Store it in another variable.
• Print the Root mean square error for the given predicted and actual values.
• The Exit of the Program.

Below is the implementation:

# Import mean_squared_error function from sklearn.metrics module using the
# import keyword
from sklearn.metrics import mean_squared_error
# Import math module using the import keyword
import math
# Give the list of actual values as static input and store it in a variable.
gvn_actul_vals = [5, 6, 1, 7, 3]
# Give the list of predicted values as static input and store it in another
# variable.
gvn_predictd_vals = [2.4, 1.5, 1.4, 2.7, 3.1]
# Pass the above actual and predicted lists as the arguments to the
# mean_squared_error() function to get the Mean Square Error(MSE).
# store it in another variable.
rslt_meansqre_err = mean_squared_error(gvn_actul_vals, gvn_predictd_vals)
# Pass the above obtained mean square error as an argument to the math.sqrt()
# function to get the Root mean square error
# store it in another variable.
root_meansqre_err = math.sqrt(rslt_meansqre_err)
# Print the RMSE for the given predicted and actual values
print("The RMSE for the given predicted and actual values = ")
print(root_meansqre_err)


Output:

The RMSE for the given predicted and actual values =
3.0222508168581905