This blog explores data science and networking, combining theoretical concepts with practical implementations. Topics include routing protocols, network operations, and data-driven problem solving, presented with clarity and reproducibility in mind.
๐ Understanding the Cost Function (Mean Squared Error)
In machine learning, a cost function tells us how well a model is performing.
One of the most common cost functions used in regression problems is
Mean Squared Error (MSE).
๐ Step-by-Step Explanation
A model makes predictions (for example, predicting a student's test score),
and we compare these predictions with the actual results.
For each prediction, calculate the difference between the predicted value and the actual value.
This difference is known as the error.
Squaring the error ensures all values are positive and penalizes larger mistakes more heavily.
The squared errors are averaged to produce a single value — the cost.
๐ Cost Function Formula
Cost = (1 / n) × ฮฃ (yi − ลทi)²
n: number of data points yi: actual value ลทi: predicted value
Understanding Cost Functions with Mean Squared Error
Evaluating a Model with a Cost Function
Understanding Mean Squared Error using a house price prediction example
When building machine learning models, predictions are rarely perfect.
To understand how well a model performs, we use a cost function.
This page walks through a concrete example using house prices and
Mean Squared Error (MSE).
Scenario Overview
Imagine you have a model that predicts house prices, and you want to evaluate how accurate those predictions are.
๐ Actual vs Predicted Prices
House
Actual Price ($)
Predicted Price ($)
1
200,000
210,000
2
250,000
240,000
3
300,000
290,000
What Is a Cost Function?
A cost function measures how far the model’s predictions are from the actual values.
In regression problems, a commonly used cost function is Mean Squared Error (MSE).
Step-by-Step Cost Calculation
1️⃣ Calculate Errors
The error is the difference between the actual price and the predicted price.
House 1: 200,000 - 210,000 = -10,000
House 2: 250,000 - 240,000 = 10,000
House 3: 300,000 - 290,000 = 10,000
2️⃣ Square the Errors
Squaring the errors ensures that negative and positive errors do not cancel each other out.
It also penalizes larger mistakes more heavily.
An MSE of 100,000,000 means that, on average,
the model’s predictions deviate significantly from the actual prices.
The purpose of training a machine learning model is to
adjust its parameters so this cost function is minimized.
As the MSE decreases, predictions become closer to real-world values.