Mean Squared Error (MSE) is one of the most widely used metrics for evaluating the performance of regression models. It evaluates the prediction accuracy by measuring the average squared difference between predicted and actual values.
Mean Squared Error (MSE) equation
The formula for the mean squared error is-
$$ MSE=\frac{1}{n}\sum _{i=1}^n(y_i-\hat {y}_i)^2 $$
Here n is the total number of observations
yi is the actual value
\(\hat {y}_i \) is the predicted value
The MSE measures the average of the squared differences between predicted values and actual target values, which leads to the following characteristics-
- It’s in squared units of the target variable, so it’s not directly intuitive.
- MSE Penalizes Large Errors. If your dataset has extreme values, MSE will reflect them strongly. Which means it is sensitive to outliers. For example, an error of 10 when squared- 102=100. At the same time an error of 100 when squared- 1002=10,000
- Smaller the MSE, better the model’s prediction accuracy.
- Values of MSE may be used for comparing two or more statistical models, in case multiple models are considered for the dataset. Model with lowest MSE is deemed better
MSE calculation example using Numpy in Python
import numpy as np y_test = [6295, 10698, 13860, 13499, 15750] y_pred = [5691, 12380, 18371, 15935, 22500] mse = np.mean(np.square(np.subtract(y_test, y_pred))) print(mse)
Output
15007931.4
MSE calculation example using sklearn in Python
from sklearn.metrics import mean_squared_error y_test = [6295, 10698, 13860, 13499, 15750] y_pred = [5691, 12380, 18371, 15935, 22500] mse = mean_squared_error(y_test, y_pred) print(mse)
Output
15007931.4
That's all for this topic Mean Squared Error (MSE) With Python Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment