rms error calculator in python

Here is an example code for calculating Root Mean Squared Error (RMSE) in Python using Numpy and Math libraries.

main.py
import numpy as np
import math

# defining actual and predicted values
actual = np.array([1, 2, 3, 4, 5])
predicted = np.array([1.5, 2.5, 3.5, 4.5, 5.5])

# calculating the squared error
squared_error = np.square(np.subtract(actual,predicted)).mean()

# calculating the root mean squared error
rms_error = math.sqrt(squared_error)

print("Root Mean Squared Error: ", rms_error)
378 chars
15 lines

Output:

main.py
Root Mean Squared Error:  0.7071067811865476
45 chars
2 lines

Here, we first define the actual and predicted values as numpy arrays. Then, we subtract predicted values from actual values, square the result and find the mean with numpy. Finally, we take the square root of mean squared error to get the RMSE value using Math library.

gistlibby LogSnag