NumPy median() | Programiz D11 Phase 2 Skip to main content

NumPy median()

In this tutorial, you will learn about the numpy.median() method with the help of examples.

The numpy.median() method computes the median along an array's specified axis.

Example

import numpy as np

# create an array
array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7])

# calculate the median of the array median1 = np.median(array1)
print(median1) # Output: 3.5

median() Syntax

The syntax of the numpy.median() method is:

numpy.median(array, axis = None, out = None, overwrite_input = False, keepdims = <no value>)

median() Arguments

The numpy.median() method takes following arguments:

  • array - array containing numbers whose median we need to compute (can be array_like)
  • axis (optional) - axis or axes along which the medians are computed (int or tuple of int)
  • out (optional) - output array in which to place the result (ndarray)
  • override_input (optional) - bool value that determines if intermediate calculations can modify an array
  • keepdims (optional) - specifies whether to preserve the shape of the original array (bool)

Notes: The default values of numpy.median() have the following implications:

  • axis = None - the median of the entire array is taken.
  • By default, keepdims will not be passed.

median() Return Value

The numpy.median() method returns the median of the array.


Example 1: Find the median of a ndArray

import numpy as np

# create an array
array1 = np.array([[[0, 1], 
                    [2, 3]],                     
                    [[4, 5], 
                    [6, 7]]])

# find the median of the entire array median1 = np.median(array1) # find the median across axis 0 median2 = np.median(array1, 0) # find the median across axis 0 and 1 median3 = np.median(array1, (0, 1))
print('\nmedian of the entire array:', median1) print('\nmedian across axis 0:\n', median2) print('\nmedian across axis 0 and 1', median3)

Output

median of the entire array: 3.5

median across axis 0:
 [[2. 3.]
 [4. 5.]]

median across axis 0 and 1 [3. 4.]

Example 2: Using Optional keepdims Argument

If keepdims is set to True, the resultant median array is of the same number of dimensions as the original array.

import numpy as np

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

# keepdims defaults to False result1 = np.median(array1, axis = 0) # pass keepdims as True result2 = np.median(array1, axis = 0, keepdims = True)
print('Dimensions in original array:', array1.ndim) print('Without keepdims:', result1, 'with dimensions', result1.ndim) print('With keepdims:', result2, 'with dimensions', result2.ndim)

Output

Dimensions in original array: 2
Without keepdims: [2.5 3.5 4.5] with dimensions 1
With keepdims: [[2.5 3.5 4.5]] with dimensions 2

Example 3: Using Optional out Argument

The out parameter allows to specify an output array where the result will be stored.

import numpy as np

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

# create an output array
output = np.zeros(3)

# compute median and store the result in the output array np.median(array1, out = output, axis = 0)
print('median:', output)

Output

median: [2.5 3.5 4.5]