3 Ways to Calculate the RMSE in Python


What is the RMSE?

The RMSE, or Root Mean Square Error, is a commonly used metric to measure the standard deviation of the errors. It provides a method for quantifying the difference between values predicted and observed by a model. Overall, the RMSE helps us evaluate the accuracy of a prediction model.

Calculating the RMSE in Python

While the RMSE is commonly calculated by hand, if you're looking for an easier method, we will be going over three methods to calculate the RMSE using Python.

Method #1: For Loops & Exponents

The brute force way to calculate the RMSE in Python is to use Python's in built tools, such as for loops and exponents.

# Define the actual and predicted values
actual = [3.4, 5.8, 7.2, 9.8, 11.6]
predicted = [2.1, 4.8, 6.1, 9.3, 12.2]

# Calculate the number of values
n = len(actual)

# Initialize a variable to store the sum of squared differences
total = 0

# Loop through each value
for i in range(n): # Add the squared difference to the total
total += (predicted[i] - actual[i])**2

# Calculate the mean squared error
meanSquaredError = total / n

# Raise the mean squared error to the power of 0.5 
rmse = (meanSquaredError) ** (1/2)

# Print the RMSE
print("The calculated Root Mean Square Error (RMSE) is: " + str(rmse))

The calculated Root Mean Square Error (RMSE) is: 0.95.

Method #2: sklearn & math

The RMSE can also be calculated in Python using sklearn.metrics.mean_squared_error, which makes it much simpler than our previous example.

from sklearn.metrics import mean_squared_error # Import mean_squared_error from sklearn.metrics
import math # Import math library

# Define our actual and predicted values
actual = [3.4, 5.8, 7.2, 9.8, 11.6]
predicted = [2.1, 4.8, 6.1, 9.3, 12.2]

# Calculate the mean squared error using mean_squared_error from sklearn.metrics
mse = mean_squared_error(actual, predicted)

# Raise the mean squared error to the power of 0.5 
rmse = (mse)**(1/2)
# Print the RMSE
print("The calculated Root Mean Square Error (RMSE) is: " + str(rmse))

The calculated Root Mean Square Error (RMSE) is: 0.95.

Method #3: numpy

Our final method involves using Python's numpy library to calculate the RMSE without the use of for loops.

import numpy as np # Import the numpy library

# Create numpy arrays for the actual and predicted values
actual = np.array([3.4, 5.8, 7.2, 9.8, 11.6])
predicted = np.array([2.1, 4.8, 6.1, 9.3, 12.2])

# Calculate the mean squared error (MSE) by taking the mean of the squared differences
meanSquaredError = ((predicted - actual) ** 2).mean()

# Calculate the RMSE by taking the square root of the MSE
rmse = np.sqrt(meanSquaredError)
# Print the RMSE
print("The calculated Root Mean Square Error (RMSE) is: " + str(rmse))

The calculated Root Mean Square Error (RMSE) is: 0.95.


Overall, no matter which method you choose, you will be able to calculate the RMSE in no time with the help of Python.