 # Python – Mean Squared Error

## Python | Mean Squared Error

The Mean Squared Error (MSE) or Mean Squared Deviation (MSD) of an estimator measures the average of error squares i.e. the average squared difference between the estimated values and true value. It is a risk function, corresponding to the expected value of the squared error loss. It is always non – negative and values close to zero are better. The MSE is the second moment of the error (about the origin) and thus incorporates both the variance of the estimator and its bias.

Steps to find the MSE

1. Find the equation for the regression line.

(1) 2. Insert X values in the equation found in step 1 in order to get the respective Y values i.e.

(2) 3. Now subtract the new Y values (i.e. ) from the original Y values. Thus, found values are the error terms. It is also known as the vertical distance of the given point from the regression line.

(3) 4. Square the errors found in step 3.

(4) 5. Sum up all the squares.

(5) 6. Divide the value found in step 5 by the total number of observations.

(6) Example:
Consider the given data points: (1,1), (2,1), (3,2), (4,2), (5,4)
You can use this online calculator to find the regression equation / line. Regression line equation: Y = 0.7X – 0.1

X Y 1 1 0.6
2 1 1.29
3 2 1.99
4 2 2.69
5 4 3.4

Now, using formula found for MSE in step 6 above, we can get MSE = 0.21606

MSE using scikit – learn:

 from sklearn.metrics import mean_squared_error   # Given values Y_true = [1,1,2,2,4]  # Y_true = Y (original values)   # calculated values Y_pred = [0.6,1.29,1.99,2.69,3.4]  # Y_pred = Y'   # Calculation of Mean Squared Error (MSE) mean_squared_error(Y_true,Y_pred)
Output: 0.21606

MSE using Numpy module:

 import numpy as np   # Given values Y_true = [1,1,2,2,4]  # Y_true = Y (original values)   # Calculated values Y_pred = [0.6,1.29,1.99,2.69,3.4]  # Y_pred = Y'   # Mean Squared Error MSE = np.square(np.subtract(Y_true,Y_pred)).mean()
Output: 0.21606

Last Updated on November 1, 2021 by admin

## Break a list into chunks of size N in PythonBreak a list into chunks of size N in Python

Break a list into chunks of size N in Python Method 1: Using yield The yield keyword enables a function to comeback where it left off when it is called again. This is the critical difference from a regular function. A regular function

## File Handling in PythonFile Handling in Python

File Handling in Python Python too supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. The concept of file handling has stretched over

## Min Heap in PythonMin Heap in Python

Min Heap in Python A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node. Mapping the elements of a heap into an array

## Render HTML Forms (GET & POST) in DjangoRender HTML Forms (GET & POST) in Django

Render HTML Forms (GET & POST) in Django Django is often called “Batteries Included Framework” because it has a default setting for everything and has features that can help anyone develop a website rapidly. Talking about forms, In HTML, a

## Get() method for dictionaries in PythonGet() method for dictionaries in Python

Get() method for dictionaries in Python Python get() method return the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument). Syntax : Dict.get(key, default=None) Parameters:  key: The

## Combobox Widget in tkinter – PythonCombobox Widget in tkinter – Python

Combobox Widget in tkinter | Python Python provides a variety of GUI (Graphic User Interface) types such as PyQT, Tkinter, Kivy, WxPython, and PySide. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy

## Python 3 basicsPython 3 basics

Python 3 basics Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.7.1, we can simply call it as Python3. Python 3.0 was released in 2008. and is interpreted language i.e it’s not compiled

## Python Program to convert String to a ListPython Program to convert String to a List

Python Program to convert String to a List   In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this