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\nactual = [3.4, 5.8, 7.2, 9.8, 11.6]\npredicted = [2.1, 4.8, 6.1, 9.3, 12.2]\n \n# Calculate the number of values\nn = len(actual)\n \n# Initialize a variable to store the sum of squared differences\ntotal = 0\n \n# Loop through each value\nfor i in range(n): # Add the squared difference to the total\n total += (predicted[i] - actual[i])**2\n \n# Calculate the mean squared error\nmeanSquaredError = total / n\n \n# Raise the mean squared error to the power of 0.5\nrmse = (meanSquaredError) ** (1/2)\n \n# Print the RMSE\nrmse
Reset Code Python Output:
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\nimport math # Import math library\n \n# Define our actual and predicted values\nactual = [3.4, 5.8, 7.2, 9.8, 11.6]\npredicted = [2.1, 4.8, 6.1, 9.3, 12.2]\n \n# Calculate the mean squared error using mean_squared_error from sklearn.metrics\nmse = mean_squared_error(actual, predicted)\n \n# Raise the mean squared error to the power of 0.5\nrmse = (mse)**(1/2)\n# Print the RMSE\nrmse
Reset Code Python Output:
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\n \n# Create numpy arrays for the actual and predicted values\nactual = np.array([3.4, 5.8, 7.2, 9.8, 11.6])\npredicted = np.array([2.1, 4.8, 6.1, 9.3, 12.2])\n \n# Calculate the mean squared error (MSE) by taking the mean of the squared differences\nmeanSquaredError = ((predicted - actual) ** 2).mean()\n \n# Calculate the RMSE by taking the square root of the MSE\nrmse = np.sqrt(meanSquaredError)\n# Print the RMSE\nrmse
Reset Code Python Output:
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.