- Find the equation for the regression line.
(1)
- Insert X values in the equation found in step 1 in order to get the respective Y values i.e.
(2)
- 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)
- Square the errors found in step 3.
(4)
- Sum up all the squares.
(5)
- Divide the value found in step 5 by the total number of observations.
(6)
X | Y | |
---|---|---|
1 | 1 | 0.6 |
2 | 1 | 1.29 |
3 | 2 | 1.99 |
4 | 2 | 2.69 |
5 | 4 | 3.4 |
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.21606MSE 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
Recommended Articles