MATHEMATICS FOR AI • LESSON 5

Median

The median is the middle value in a group of numbers. It is especially useful when the data contains very large or very small values that could distort the average.

CORE IDEA

The median is the middle value after the numbers are arranged in order.

Unlike the mean, the median is much less affected by extreme values. This makes it useful when the data is uneven or contains outliers.

01

What Is the Median?

Imagine several numbers standing in a line from the smallest to the largest.

The median is the number standing in the middle.

10, 20, 30, 40, 50

             ↑
           Middle

Median = 30

There are five numbers, and 30 is exactly in the middle.

02

Step 1: Arrange the Numbers

The first thing we must do is arrange the numbers from the smallest to the largest.

For example:

Original numbers:

40, 10, 30, 50, 20

Arrange them:

10, 20, 30, 40, 50

Now we can identify the middle value.

10, 20, 30, 40, 50
         ↑
       Middle

Median = 30
03

What If There Is an Even Number of Values?

Sometimes there is no single middle number. This happens when we have an even number of values.

For example:

10, 20, 30, 40

There are four numbers, so there are two middle values:

10, 20, 30, 40
     ↑   ↑
   Middle values

The two middle values are 20 and 30. We calculate their average:

(20 + 30) / 2

= 50 / 2

= 25

Therefore:

Median = 25
04

Why Do We Need the Median?

The median is useful because it is not easily affected by extremely large or extremely small values.

Consider these salaries:

₹20,000
₹22,000
₹24,000
₹25,000
₹2,00,000

The last salary is much larger than the others. This is an example of an outlier.

The median is simply the middle value:

₹20,000
₹22,000
₹24,000  ← Median
₹25,000
₹2,00,000

Therefore:

Median = ₹24,000

The large ₹2,00,000 value does not pull the median upward the way it pulls the mean.

05

Mean vs Median

This is one of the most important differences to understand.

Using the same salary data:

₹20,000
₹22,000
₹24,000
₹25,000
₹2,00,000

The mean is:

(20,000 + 22,000 + 24,000 + 25,000 + 2,00,000) / 5

= ₹58,200

The median is:

₹24,000

So we get:

Mean   = ₹58,200
Median = ₹24,000

The mean is much higher because of the extreme salary. The median gives us a better idea of the typical value in this particular dataset.

06

Real-World Example

Imagine an online store wants to understand the typical amount customers spend.

Five customers spend:

₹500
₹600
₹700
₹800
₹10,000

The ₹10,000 purchase is much larger than the other purchases.

The median is:

₹500
₹600
₹700  ← Median
₹800
₹10,000

Therefore:

Median = ₹700

The median tells us that the middle customer's spending was ₹700, without allowing the unusually large order to dominate the result.

07

Median in AI and Machine Learning

AI systems often work with real-world data, and real-world data can contain unusual values.

For example, imagine a dataset containing house prices:

₹40 lakh
₹45 lakh
₹48 lakh
₹50 lakh
₹5 crore

The ₹5 crore house is very different from the other houses.

The median can give us a more stable description of the typical house price in this small dataset.

This is useful when analyzing data before training a machine learning model.

08

Median in Data Preprocessing

Median can also be used when preparing data for machine learning.

For example, suppose a dataset has a missing age:

20
25
?
30
35
40

We can calculate the median from the available values:

20, 25, 30, 35, 40

Median = 30

We could then use 30 as a replacement for the missing value.

20
25
30  ← Missing value replaced
30
35
40

This technique is called median imputation. It can be useful when the data contains outliers because the median is less affected by them than the mean.

09

Median in Python

Python's statistics module provides a simple way to calculate the median.

from statistics import median

numbers = [10, 20, 30, 40, 50]

result = median(numbers)

print(result)

Output:

30

Python handles the process of finding the middle value for us.

But you should understand the mathematical process first. The library is only automating the calculation.

10

When Should We Use Median?

Median is especially useful when the data is not evenly distributed or contains extreme values.

Example 1 — Salaries

₹20,000
₹22,000
₹24,000
₹25,000
₹2,00,000

→ Median is useful
Example 2 — House Prices

₹40 lakh
₹45 lakh
₹50 lakh
₹55 lakh
₹5 crore

→ Median is useful

In both examples, one unusually large value can distort the mean.

11

Mean or Median?

A simple way to think about the difference is:

Mean

Add everything
     ↓
Divide by count
     ↓
Average
Median

Sort the values
     ↓
Find the middle
     ↓
Median

Neither one is always better. The correct choice depends on the data.

12

The Main Idea

The median is the middle value after the data has been arranged from smallest to largest.

5, 10, 20, 30, 100

          ↓

5, 10, 20, 30, 100
        ↑
      Middle

Median = 20

If there are two middle values, take their average.

10, 20, 30, 40

Middle values = 20 and 30

Median = (20 + 30) / 2

Median = 25
WHAT TO REMEMBER

Median is the middle value of ordered data.

First arrange the values from smallest to largest, then find the middle. If there are two middle values, average them. Median is particularly useful when data contains extreme values because those values have much less effect on the median than they do on the mean.