Mean Square Error | Loss Functions
Mean Squared Error is the most commonly used in the Regression problems. The name itself suggests that function returns the value by taking a mean of the square of the errors.
M = mean(T) | T = sum(S) | S = square(E) | E = Error
Here E is the difference between actual output and predicted output. Let’s understand this deeply using the sample data.
Here I have the small dataset from online. We using single linear regression for the analysis as dataset contains only one input(x). We are going to use pandas & matplotlib python libraries for plotting. Let’s see how the x and y connect in the plots.
As we know MSE is for Regression Problems, understand this better using Linear Regression. LR
Linear Regression is the statistical modelling technique to investigate the relationship between dependent variable and independent variable(s) with continuous variables.
Simply Linear regression is “Fitting the line for all the points with minimum error and predict future”
Consider the blue line over the points as Regression line drawn using the Linear Regression algorithm. Here the job of MSE is to find the error and give it back by which algorithm decides its internal parameters. Below sample image gives a better understanding of how the error is calculated.
The regression line indicates “Every X value in the data and Its predicted Y value”. But as we can see that the predicted Y value does not equal the actual Y which is considered as Error. Now we can calculate the Total loss by the simple formula.
Once this error is given to the Regression algorithm, It will do the optimization tasks, changes its internal parameter and run the algorithm continuously using loss function until it feels the line fits well to the data.
Digging Deeper:
Now we approximately understand how MSE calculates error. But how it works with data? What is the effect of the outliers if you use MSE? I wrote a small python script to calculate the MSE manually for our data.
8.84 is the error I got using MSE method as shown. Here I aim to find how much this function responds to the outliers.
I have added x=10, Y=99 which is an outlier when we see other pairs. Now I got “67.06” as output from MSE which is very larger than “8.44”. So with this, we can conclude that outliers influence MSE output largely.
When to use MSE?
We clarify here is MSE is returning large value if outliers/large errors present. Use MSE when your data accepts the following.
- Give more important to large values than smaller once.
- Data is distributed randomly and not necessary to mind about the large values.
Example: House price prediction. When we use house price data, depends on the place of the houses the price will get high/low. In these kinda times, we know that this is correct and not gonna do any issues. we can use MSE here.
That’s all for now about MSE. I will update eventually whenever I am getting new on MSE. Thanks, Bye. Happy ML.
Read about my post on Mean Absolute Error(MAE) if needed.