Mathematical Concept of Median

1. Definition

The median is the middle value of a dataset when the numbers are arranged in ascending or descending order.

Formally:

  • If ( n ) (number of observations) is odd:

    \[\text{Median} = x_{\frac{n+1}{2}}\]
  • If ( n ) is even:

    \[\text{Median} = \frac{x_{\frac{n}{2}} + x_{\frac{n}{2} + 1}}{2}\]

Here, \(x_i\) represents the \(i^{th}\) smallest value after sorting.


2. Intuitive Understanding

The median divides the dataset into two equal halves:

  • 50% of the data lies below the median
  • 50% lies above it

Unlike the mean, which can be heavily affected by outliers, the median gives a robust measure of central tendency β€” especially for skewed distributions.

For example:

Dataset Mean Median
[1, 2, 3, 4, 5] 3.0 3
[1, 2, 3, 4, 50] 12.0 3

Here, the extreme value (50) distorts the mean but not the median.


3. Example 1 (Odd Number of Observations)

Dataset: [3, 5, 7, 9, 11]

Sorted data: [3, 5, 7, 9, 11]
Number of observations ( n = 5 ) (odd)

\[\text{Median} = x_{\frac{5+1}{2}} = x_3 = 7\]

Median = 7


4. Example 2 (Even Number of Observations)

Dataset: [3, 5, 7, 9]

Sorted data: [3, 5, 7, 9]
Number of observations ( n = 4 ) (even)

\[\text{Median} = \frac{x_2 + x_3}{2} = \frac{5 + 7}{2} = 6\]

Median = 6


5. Relationship with Data Distribution

Type of Distribution Mean vs Median Relationship Description
Symmetrical Mean = Median Balanced data
Right-skewed Mean > Median Tail extends right
Left-skewed Mean < Median Tail extends left

Thus, comparing mean and median helps identify data skewness.


6. Mathematical Properties

  1. Median is robust β€” not affected by extreme values.
  2. Median minimizes the sum of absolute deviations:

    \[\text{Median} = \arg\min_m \sum |x_i - m|\]

    (while mean minimizes squared deviations)

  3. Median exists for both numerical and ordinal data (e.g., rankings).

NumPy Function for Median

1. Function: numpy.median()

Syntax:

numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)

2. Parameters

Parameter Description
a Input array or data sequence
axis Axis or axes along which to compute the median
out Optional output array
overwrite_input If True, allows modifying input array to save memory
keepdims If True, retains reduced dimensions (for broadcasting)

3. Example 1: Simple Median

import numpy as np

data = np.array([3, 5, 7, 9, 11])
median_value = np.median(data)
print("Median:", median_value)

Output:

Median: 7.0

4. Example 2: Even-Length Dataset

data = np.array([3, 5, 7, 9])
median_value = np.median(data)
print("Median:", median_value)

Output:

Median: 6.0

5. Example 3: Median Along Axis

matrix = np.array([[10, 30, 20],
                   [50, 40, 60]])

median_row = np.median(matrix, axis=1)  # Row-wise
median_col = np.median(matrix, axis=0)  # Column-wise

print("Row-wise median:", median_row)
print("Column-wise median:", median_col)

Output:

Row-wise median: [20. 50.]
Column-wise median: [30. 35. 40.]

6. Example 4: Ignoring NaN Values

When the dataset contains missing values (NaN), use numpy.nanmedian():

data = np.array([1, 2, np.nan, 4, 5])
median_value = np.nanmedian(data)
print("Median ignoring NaN:", median_value)

Output:

Median ignoring NaN: 3.0

7. Median vs Mean Visualization

Imagine a distribution with an extreme outlier:

data = np.array([1, 2, 3, 4, 100])
print("Mean:", np.mean(data))
print("Median:", np.median(data))

Output:

Mean: 22.0
Median: 3.0

The median better represents the β€œtypical” value in such skewed data.


8. Applications in Data Science

  • Robust central tendency for noisy or skewed data
  • Income and property price analysis
  • Median filtering in signal and image processing
  • Benchmarking (e.g., median CPU usage, median salary)

Summary

Concept Formula NumPy Function Sensitive to Outliers Best For
Median Middle value of sorted data np.median() ❌ No Skewed or ordinal data

Quick Comparison (Mean vs Median)

Dataset Mean Median Better Measure
[2, 4, 6, 8, 10] 6 6 Either
[2, 4, 6, 8, 100] 24 6 Median