Trusted answers to developer questions

Educative Answers Team

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:

- $n$ - the total number of terms for which the error is to be calculated
- $y_i$ - the observed value of the variable
- $\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.

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

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

RELATED TAGS

error

python

mean square error

mse

Copyright ©2022 Educative, Inc. All rights reserved

RELATED COURSES

View all Courses

Keep Exploring

Related Courses