Calculating Mean Squared Error in Python

The mean squared error (MSE) is largely used as a metric to determine the performance of an algorithm.

The formula to calculate the MSE is as follows:

svg viewer

Defining the variables

  1. nn - the total number of terms for which the error is to be calculated
  2. yiy_i - the observed value of the variable
  3. yˉi\bar y_i - the predicted value of the variable

The mean square error is the average of the square of the difference between the observed and predicted values of a variable.

In Python, the MSE can be calculated rather easily, especially with the use of lists.

How to calculate MSE

  1. Calculate the difference between each pair of the observed and predicted value
  2. Take the square of the difference value
  3. Add each of the squared differences to find the cumulative values
  4. In order to obtain the average value, divide the cumulative value by the total number of items in the list

Example

Suppose you wish to calculate the MSE and are provided with the observed and predicted values. The steps mentioned above will be implemented as follows:

y = [11,20,19,17,10]
y_bar = [12,18,19.5,18,9]
summation = 0 #variable to store the summation of differences
n = len(y) #finding total number of items in list
for i in range (0,n): #looping through each element of the list
difference = y[i] - y_bar[i] #finding the difference between observed and predicted value
squared_difference = difference**2 #taking square of the differene
summation = summation + squared_difference #taking a sum of all the differences
MSE = summation/n #dividing summation by total values to obtain average
print "The Mean Square Error is: " , MSE
Copyright ©2024 Educative, Inc. All rights reserved