How Wrong Is the Line? Cost and MSE
A line needs a score
In the last lesson, a line estimated the rent of a 750 square foot flat. The estimate was ₹25,900. That tells us what the line predicts, but not whether the line is good.
Suppose two people draw two different lines for the same four Pune flats. Both lines look close to the dots. One is usually off by about ₹1,000. The other is usually off by about ₹3,000. You would choose the first line, but a picture alone makes that comparison difficult.
We need one number that describes how far the predictions are from the real rents. Then we can compare two lines, or later ask a learning algorithm to search for a line with a smaller score.
Tip:A model is not "good" just because it produces a prediction. It is useful when its predictions are close enough for the job it needs to do.
First measure one mistake
Start with one flat. Its actual rent is ₹18,000. The model predicts ₹17,000. The prediction is ₹1,000 too low.
Key Points
- Actual value, y: ₹18,000, the rent that really happened.
- Predicted value, ŷ: ₹17,000, the rent produced by the model.
- Error, or residual: y − ŷ = ₹18,000 − ₹17,000 = ₹1,000.
The error is also called a residual. It is the vertical gap between the real data point and the line. A positive error means the model predicted too low. A negative error means it predicted too high.
If the same flat gets a prediction of ₹19,000, the error becomes ₹18,000 − ₹19,000 = −₹1,000. The minus sign tells you the direction. The size of the mistake is still ₹1,000.
Do not confuse the error with the actual rent. The error is not another target. It is a measurement of how far this particular prediction missed.
Why raw errors cannot be averaged
Imagine four predictions with errors of ₹1,000, −₹1,000, ₹1,000, and −₹1,000. Every prediction is wrong by ₹1,000, but the average error is zero.
The positive and negative signs cancel. The zero would falsely suggest that the model has no overall error. This is why we need a rule that stops opposite-direction mistakes from hiding each other.
1errors = [1000, -1000, 1000, -1000]
2
3average_error = sum(errors) / len(errors)
4average_absolute_error = sum(abs(error) for error in errors) / len(errors)
5
6print(f"Average signed error: ₹{average_error:,.0f}")
7print(f"Average distance from reality: ₹{average_absolute_error:,.0f}")Average signed error: ₹0 Average distance from reality: ₹1,000
The second number uses absolute value. Absolute value keeps the distance from zero and removes the direction. It is a useful idea, and you will study it alongside other regression measures later. This lesson focuses on a different choice: square the errors before averaging them.
Mean squared error, step by step
Mean squared error, usually shortened to MSE, has two operations. First, square every error. Second, take the mean, which means add the squared errors and divide by the number of examples.
Squaring does two things. A negative error becomes positive, so opposite errors cannot cancel. A large error becomes much more influential than a small error.
Use four errors from our flat example: ₹1,000, −₹1,000, ₹2,000, and −₹500.
Key Points
- Errors: 1,000, −1,000, 2,000, −500.
- Squared errors: 1,000,000, 1,000,000, 4,000,000, 250,000.
- Add them: 6,250,000.
- Divide by four examples: MSE = 1,562,500.
The formula is MSE = (1 ÷ n) × Σ(y − ŷ)². Here, n is the number of examples. The symbol Σ means add the result for every example. y is the actual value. ŷ is the prediction. The small ² means square the error.
1import numpy as np
2
3errors = np.array([1000, -1000, 2000, -500])
4squared_errors = errors ** 2
5mse = squared_errors.mean()
6
7print("Errors:", errors)
8print("Squared errors:", squared_errors)
9print(f"MSE: ₹{mse:,.0f}")Errors: [ 1000 -1000 2000 -500] Squared errors: [1000000 1000000 4000000 250000] MSE: ₹1,562,500
Why a large mistake matters more
A ₹1,000 error becomes 1,000,000 after squaring. A ₹2,000 error becomes 4,000,000. The ₹2,000 mistake is twice as large in rupees, but it contributes four times as much to MSE.
1for error in [1000, 2000, 5000]:
2 print(f"₹{error:,} error becomes {error ** 2:,} after squaring")₹1,000 error becomes 1,000,000 after squaring ₹2,000 error becomes 4,000,000 after squaring ₹5,000 error becomes 25,000,000 after squaring
This is useful when a very wrong prediction deserves extra attention. It is also a choice with a cost. One unusual flat can dominate the final MSE, especially in a small dataset. MSE does not simply report reality. It says that large mistakes should matter more.
That is why you should say "lower MSE according to this scoring rule" rather than "MSE proves this model is best". The scoring rule reflects what we decided to care about.
Compare two possible lines
Now compare two lines for the same four flats. Line A predicts values that are ₹1,000 away from every actual rent. Line B is ₹2,000 away from every actual rent. MSE should prefer Line A because its typical mistakes are smaller.
1import numpy as np
2
3actual = np.array([18000, 24000, 31000, 37000])
4line_a = np.array([17000, 25000, 30000, 38000])
5line_b = np.array([16000, 26000, 29000, 39000])
6
7for name, predictions in [("Line A", line_a), ("Line B", line_b)]:
8 errors = actual - predictions
9 mse = np.mean(errors ** 2)
10 print(f"{name} MSE: ₹{mse:,.0f}")Line A MSE: ₹1,000,000 Line B MSE: ₹4,000,000
Line A wins under MSE. Notice the phrase "under MSE". If one line is safer for a particular neighbourhood or protects a particular group of users, you may need another analysis too. A metric helps you compare models. It does not understand the whole business problem for you.
Calculate MSE with scikit-learn
Once you understand the calculation, a library can do it for you. scikit-learn provides mean_squared_error. Give it the actual values first and the predictions second.
1import numpy as np
2from sklearn.metrics import mean_squared_error
3
4actual = np.array([18000, 24000, 31000, 37000])
5predicted = np.array([17000, 25000, 30000, 38000])
6
7mse = mean_squared_error(actual, predicted)
8print(f"MSE: ₹{mse:,.0f}")MSE: ₹1,000,000
This MSE is 1,000,000, but that does not mean the model is wrong by ₹1,000,000. The units are squared rupees. MSE is useful for comparing and training models, but it is not always easy to explain in the original units.
Taking the square root brings the number back to rupees. That measure is called root mean squared error, or RMSE. You will compare RMSE with other regression measures later in this chapter. For now, remember that MSE is the squared measure and RMSE returns to the target units.
Where people get confused
Key Points
- "MSE is the average mistake in rupees." No. MSE is measured in squared units, such as rupees squared. RMSE returns to the original units.
- "A negative error means the model is rewarded." No. It only says the prediction was too high. Squaring prevents opposite directions from cancelling.
- "The lowest MSE is always the best model." It is the best according to MSE on that dataset. A useful choice may need checks for groups, costs, or other kinds of error.
- "Squaring is only a mathematical trick." It is also a modelling choice that gives large mistakes more influence.
- "Cost, loss, and MSE always mean exactly the same thing." People often use cost and loss broadly. MSE is one specific squared-error calculation. Always check the definition being used.
Quick check
Why can the average of raw errors be zero even when every prediction is wrong?
Errors in opposite directions cancel. A prediction that is too low can hide another prediction that is too high, even though both predictions are wrong.
What are the two main steps in mean squared error?
Calculate each actual-minus-predicted error, square every error, then take the mean by adding the squared errors and dividing by the number of examples.
Why does MSE pay extra attention to a very large mistake?
The error is squared before it is averaged. A ₹2,000 error becomes four times the squared contribution of a ₹1,000 error.
What does an MSE of ₹1,000,000 mean in the flat-rent example?
It is the average squared error, measured in squared rupees. It does not mean the typical rent mistake is ₹1,000,000. Taking the square root would return to rupees.
Recap
Key Points
- An error, or residual, is the actual value minus the prediction for one example.
- Raw errors cannot simply be averaged because positive and negative errors cancel.
- MSE squares each error and then averages the squared values.
- Squaring removes the sign and makes large mistakes count more.
- MSE is useful for comparing models, but its squared units and preference for large errors are choices with consequences.
We can now score a line and compare it with another line. The next question is how a model changes w and b to find a line with a lower cost. That is where Gradient Descent begins.
Theia
Can you explain this concept?
Practise How Wrong Is the Line? Cost and MSE with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.