Instagram
youtube
Facebook
Twitter

Median in Numpy

In statistics and data analysis, the median is a measure of central tendency that represents the middle value of a dataset when it is sorted. In this tutorial, we will explore how to calculate the median using NumPy.

Calculating the Median

In Numpy we can calculate the Median using two main functions:

  • numpy.median(): This function returns the median of a given array.

  • numpy.nanmedian(): This function is similar to numpy.median() but is designed to handle arrays with missing values.

Example: Using "numpy.median()"

As we have already mentioned above, "numpy.median()" is used to calculate the median of an array.

import numpy as np
# Creating an array
data = np.array([10, 2, 5, 8, 7, 3, 12])

# Calculating the median
median_value = np.median(data)

print("Median:", median_value)

In above example, we created an array called data, and then we calculated its median using np.median() function

Output:

Median: 7.0

Example: Using "numpy.nanmedian()"

If our dataset contains missing values (NaN), we can use the numpy.nanmedian() function to calculate median.

import numpy as np
# Creating an array with NaN values
data_with_nan = np.array([10, 2, 5, np.nan, 7, 3, 12])

# Calculate the median, ignoring NaN values
median_nan = np.nanmedian(data_with_nan)

print("Median:", median_nan)

In above example, NumPy automatically ignores the NaN values and computes the median for the remaining values.

Output:

Median: 6.0