🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.median()

AI & DATA SCIENCE // np-median

np.median() computes the middle value of a dataset when sorted, either over the whole array or along a specified axis — for an even number of elements, it averages the two middle values.

Syntax

np.median(arr, axis=None)

Deep Dive Course

Unlike the mean, the median isn't affected by extreme outliers, since it only depends on the position of values once sorted, not their actual magnitude — a small dataset with one huge outlier has a mean dragged way up by that outlier, but a median that still reflects the typical value much better. Computing it requires sorting, or an equivalent selection algorithm, internally, which is more expensive than computing a mean, but that's rarely a practical concern for typical dataset sizes.

1Understanding np.median()

Unlike the mean, the median isn't affected by extreme outliers, since it only depends on the position of values once sorted, not their actual magnitude — a small dataset with one huge outlier has a mean dragged way up by that outlier, but a median that still reflects the typical value much better. Computing it requires sorting, or an equivalent selection algorithm, internally, which is more expensive than computing a mean, but that's rarely a practical concern for typical dataset sizes.

💡

Prefer the median over the mean specifically when a dataset likely contains outliers or a skewed distribution, like income or response-time data, since the mean can be pulled far away from the typical value by a small number of extreme points.

editor.html
import numpy as np

arr = np.array([1, 3, 3, 6, 7, 8, 9])
print(np.median(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.median() showing how it is used in production NumPy code.

editor.html
import numpy as np

incomes = np.array([30000, 32000, 31000, 500000])
print(np.mean(incomes))
print(np.median(incomes))
localhost:3000

3Best Practices

Follow these guidelines when working with np.median():

1. Use the median instead of the mean when outliers or skew could distort what 'typical' means for the dataset

2. Use np.nanmedian() when the data might contain NaN values that should be ignored

3. Check whether an even-length dataset's median, an average of two middle values, actually appears as a real data point or just falls between two, if that distinction matters for your use case

⚠️

Tip: Prefer the median over the mean specifically when a dataset likely contains outliers or a skewed distribution, like income or response-time data, since the mean can be pulled far away from the typical value by a small number of extreme points.

editor.html
import numpy as np

arr = np.array([1, 3, 3, 6, 7, 8, 9])
print(np.median(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 3, 3, 6, 7, 8, 9])
print(np.median(arr))
Example 02Advanced Example
import numpy as np

incomes = np.array([30000, 32000, 31000, 500000])
print(np.mean(incomes))
print(np.median(incomes))

Best Practices

  • Use the median instead of the mean when outliers or skew could distort what 'typical' means for the dataset
  • Use np.nanmedian() when the data might contain NaN values that should be ignored
  • Check whether an even-length dataset's median, an average of two middle values, actually appears as a real data point or just falls between two, if that distinction matters for your use case

Interview Question

Why does adding one extreme outlier to a dataset change its mean a lot but barely affect its median?

Hint: Think about what information the mean uses versus what the median uses.

The mean incorporates the actual magnitude of every value, so one extremely large or small number can pull the average substantially toward it, proportional to how extreme it is. The median only depends on the relative order of values, specifically which value ends up in the middle position once sorted — an outlier just becomes one more value at an extreme end of that sorted order, and unless it changes which value lands in the middle position, the median stays essentially unaffected regardless of how extreme the outlier actually is.

Exercises

MediumPractice using np.median() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 3, 3, 6, 7, 8, 9])
print(np.median(arr))

Frequently Asked Questions

Why does adding one extreme outlier to a dataset change its mean a lot but barely affect its median?

The mean incorporates the actual magnitude of every value, so one extremely large or small number can pull the average substantially toward it, proportional to how extreme it is. The median only depends on the relative order of values, specifically which value ends up in the middle position once sorted — an outlier just becomes one more value at an extreme end of that sorted order, and unless it changes which value lands in the middle position, the median stays essentially unaffected regardless of how extreme the outlier actually is.

Related Functions

np-meannp-percentilenp-std