Question - Write code to calculate the root mean square error (RMSE) given the lists of values as actual and predicted.
Answer -
To calculate the root mean square error (RMSE), we have to:
- Calculate the errors, i.e., the differences between the actual and the predicted values
- Square each of these errors
- Calculate the mean of these squared errors
- Return the square root of the mean
The code in Python for calculating RMSE is given below:
def rmse(actual, predicted):
errors = [abs(actual[i] - predicted[i]) for i in range(0, len(actual))]
squared_errors = [x ** 2 for x in errors]
mean = sum(squared_errors) / len(squared_errors)
return mean ** .5