top of page
Inaugurated by IN-SPACe
ISRO Registered Space Tutor

S3-SA3-0465

What is a Cost Function in ML?

Grade Level:

Class 9

AI/ML, Data Science, Physics, Economics, Cryptography, Computer Science, Engineering

Definition
What is it?

A Cost Function in Machine Learning is like a 'scorecard' that tells us how well our predictions match the actual correct answers. It measures the 'error' or 'cost' of our model's wrong guesses. The goal is always to find a model that has the lowest possible cost.

Simple Example
Quick Example

Imagine you predict the price of a chai cup will be 15 rupees, but the actual price is 12 rupees. Your 'error' is 3 rupees. If you predict another chai cup at 10 rupees, and it's actually 11 rupees, your error is 1 rupee. A cost function adds up these errors to give a total 'badness' score for all your predictions.

Worked Example
Step-by-Step

Let's say a model predicts the marks of 3 students. We want to calculate the 'Squared Error' cost.

Student 1: Predicted Marks = 80, Actual Marks = 75
Student 2: Predicted Marks = 60, Actual Marks = 62
Student 3: Predicted Marks = 95, Actual Marks = 90

Step 1: Calculate error for Student 1. Error = Predicted - Actual = 80 - 75 = 5
---Step 2: Square the error for Student 1. Squared Error = 5 * 5 = 25
---Step 3: Calculate error for Student 2. Error = Predicted - Actual = 60 - 62 = -2
---Step 4: Square the error for Student 2. Squared Error = (-2) * (-2) = 4
---Step 5: Calculate error for Student 3. Error = Predicted - Actual = 95 - 90 = 5
---Step 6: Square the error for Student 3. Squared Error = 5 * 5 = 25
---Step 7: Sum all squared errors to get the total Cost. Total Cost = 25 + 4 + 25 = 54

Answer: The total Cost Function value (Squared Error) for these predictions is 54.

Why It Matters

Cost functions are crucial in AI/ML because they guide models to learn and improve. From predicting stock prices in economics to designing efficient engineering systems or even securing online transactions in cryptography, understanding cost helps build smarter technology. It's key for careers in Data Science and AI.

Common Mistakes

MISTAKE: Thinking a low cost function means the model is perfect. | CORRECTION: A low cost function means the model is performing well, but it might still have small errors or could be 'overfitting' (too good on seen data, bad on new data). The goal is to minimize, not necessarily zero out, the cost.

MISTAKE: Only considering the difference (error) without squaring or taking absolute value. | CORRECTION: Simply adding up (Predicted - Actual) errors can make positive and negative errors cancel out, giving a misleading total of zero even if many predictions are wrong. Squaring errors (like in Mean Squared Error) or taking absolute values (like in Mean Absolute Error) ensures all errors contribute positively to the total cost.

MISTAKE: Confusing a cost function with an objective function. | CORRECTION: A cost function is a specific type of objective function that we want to minimize. An objective function can also be something we want to maximize (like profit). For ML models, we usually aim to minimize cost.

Practice Questions
Try It Yourself

QUESTION: If your model predicts a cricket score of 180, but the actual score was 175, what is the simple error? | ANSWER: 5

QUESTION: A delivery app predicts a 20-minute delivery time, but it actually takes 25 minutes. If another delivery is predicted for 30 minutes but takes 28 minutes. Calculate the total Squared Error for these two predictions. | ANSWER: Step 1: Error 1 = 20 - 25 = -5. Squared Error 1 = (-5)*(-5) = 25. --- Step 2: Error 2 = 30 - 28 = 2. Squared Error 2 = 2*2 = 4. --- Step 3: Total Squared Error = 25 + 4 = 29.

QUESTION: For three students, your model predicts marks (P) and actual marks (A) are: (P=70, A=72), (P=85, A=80), (P=60, A=65). Calculate the Mean Absolute Error (MAE), which is the average of the absolute differences. | ANSWER: Step 1: |70-72| = 2. --- Step 2: |85-80| = 5. --- Step 3: |60-65| = 5. --- Step 4: Sum of absolute errors = 2 + 5 + 5 = 12. --- Step 5: MAE = Sum of errors / Number of students = 12 / 3 = 4.

MCQ
Quick Quiz

What is the main purpose of a Cost Function in Machine Learning?

To make the model run faster

To measure how accurately the model predicts

To choose the input data for the model

To display the final results to the user

The Correct Answer Is:

B

A Cost Function's primary role is to quantify the error or 'cost' of a model's predictions, thereby measuring its accuracy. Options A, C, and D describe other aspects of machine learning or model usage, not the core function of a cost function.

Real World Connection
In the Real World

When you use a food delivery app like Zomato or Swiggy, the app's estimated delivery time is a prediction. Behind the scenes, a machine learning model constantly tries to minimize its 'cost function' – the difference between its predicted delivery time and the actual delivery time. By doing this, the app learns to give you more accurate estimates, making your experience better!

Key Vocabulary
Key Terms

ERROR: The difference between a predicted value and the actual value. | PREDICTION: A guess or estimate made by a machine learning model. | OPTIMIZATION: The process of finding the best set of parameters for a model by minimizing the cost function. | MODEL: A program or system that learns from data to make predictions or decisions.

What's Next
What to Learn Next

Now that you understand what a Cost Function is, you should learn about 'Gradient Descent'. Gradient Descent is the main method used to minimize these cost functions, helping the machine learning model learn and improve its predictions over time. It's like finding the lowest point in a valley!

bottom of page