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:
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.
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
View all Courses