NumPy median()


Numpy median() Function

The numpy.median() function calculates the median of elements along a specified axis in a numpy array.

Given a collection of values, organize and order the values from smallest to largest, and the Median is middle value in this ordered collection.

For example, if [3, 1, 7, 2, 9] is the given data, then ordering the items in it from smallest to largest gives us [1, 2, 3, 7, 9], and the middle value is 3. Therefore median is 3. If there are even number of elements in the given data, then the average of the middle two items is taken as median.

Syntax

The syntax of numpy median() function is

numpy.median(a, axis=None)

where

ParameterDescription
aThe input numpy array.
axis(Optional) The axis along which the median is computed. If not provided, the median is computed for the entire array.

Examples

Let's go through some examples to understand how to use the numpy.median() function.

1. Calculating Median of a 1D Array

Here's how you can calculate the median of a 1D numpy array:

Python Program

import numpy as np

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

Output

3.5

Median

Given array = [3, 1, 7, 2, 9]
Ordered     = [1, 2, 3, 7, 9]
                     ↑
                     3 is the median

Now, let us consider a 1D array with even number of elements, and find its median.

Python Program

import numpy as np

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

Output

3.5

Median

Given array = [3, 1, 7, 2, 9, 4]
Ordered     = [1, 2, 3, 4, 7, 9]
                     ----
                      ↑    average of these two values
                     3.5   is the median

2. Calculating Median of a 2D Array along an Axis

You can also calculate the median along a specific axis of a 2D numpy array.

In the following program, we take a 2D array, and find the median along axis=1.

Python Program

import numpy as np

arr_2d = np.array([[10, 5, 8],
                   [4, 12, 6]])
median = np.median(arr_2d, axis=1)
print(median)

Output

[8. 6.]

Median

          [    [10, 5, 8],  [4, 12, 6]   ]
axis  :   0    1            1 
               ↑            ↑
median         8            6      along axis=1

3. Calculating Median of a 3D Array along Multiple Axes

You can calculate the median along multiple axes of a 3D numpy array. In the following program, we take a 3D array in arr_3d, and find its median along axis=(0, 1).

Python Program

import numpy as np

arr_3d = np.array([[[10, 5, 8],
                    [4, 12, 6]],
                   
                   [[7, 3, 9],
                    [2, 11, 8]]])

median = np.median(arr_3d, axis=(0, 1))
print(median)

Output

[5.5 8.  8. ]

In this example, the medians along axes 0 and 1 are [7. 8. 7.].

You can use the axis parameter with a tuple to specify the axes along which the median should be calculated.

Summary

In this NumPy Tutorial, we learned how to use numpy.median() function to calculate the median value of elements in a numpy array, for a 1D array, along an axis for a 2D array, and along multiple axes for 3D array, with examples.