Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Median Explained – Python np.median() Function with Examples

NumPy – Median Function

The median is a key concept in statistics that represents the middle value of a dataset.

In NumPy, the np.median() function is used to compute the median quickly and efficiently.

It is widely used in:

  • Data science
  • Machine learning
  • Statistics
  • Finance
  • Data analysis

What is Median?

The median is the middle value when data is sorted.

If the dataset has an even number of values, the median is the average of the two middle numbers.


Import NumPy

import numpy as np

1. Median of a Simple Array

import numpy as np

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

result = np.median(A)

print(result)

Output:

30.0

2. Median with Even Number of Elements

import numpy as np

A = np.array([10, 20, 30, 40])

print(np.median(A))

Output:

25.0

3. Median of 2D Array

import numpy as np

A = np.array([[1, 2],
[3, 4]])

print(np.median(A))

Output:

2.5

4. Median Along Axis (Rows vs Columns)

Median of Columns (axis=0)

import numpy as np

A = np.array([[1, 2],
[3, 4],
[5, 6]])

print(np.median(A, axis=0))

Output:

[3. 4.]

Median of Rows (axis=1)

import numpy as np

A = np.array([[1, 2],
[3, 4],
[5, 6]])

print(np.median(A, axis=1))

Output:

[1.5 3.5 5.5]

5. Median with Outliers

Median is useful when data has extreme values.

import numpy as np

A = np.array([10, 12, 14, 16, 1000])

print(np.median(A))

Output:

14.0

Why?

Median is not affected by extreme values (outliers).


6. Median vs Mean

import numpy as np

A = np.array([10, 12, 14, 16, 1000])

print("Mean:", np.mean(A))
print("Median:", np.median(A))

Output:

Mean: 210.4
Median: 14.0

Real-World Applications

1. Data Science

  • Central tendency measurement
  • Data cleaning

2. Machine Learning

  • Robust feature scaling
  • Outlier handling

3. Finance

  • Median income analysis
  • Market trend evaluation

4. Healthcare

  • Medical data analysis
  • Patient statistics

Why Use Median?

Median is useful because:

✔ Handles outliers well
✔ Represents real middle value
✔ More stable than mean in skewed data


Why Use NumPy?

Using NumPy provides:

  • Fast computation
  • Easy axis-based operations
  • Efficient large dataset handling
  • Reliable statistical functions

Combined with Python, it becomes essential for modern data analysis.


Summary

NumPy provides a simple way to calculate median using:

np.median(array, axis=...)

It works with 1D, 2D, and multi-dimensional arrays.


Conclusion

The median is a powerful statistical tool for understanding data distribution, especially when dealing with outliers. NumPy makes it easy, fast, and reliable for real-world applications.




Post a Comment

0 Comments