NumPy median() function

In this tutorial, we will cover numpy.median() function of the NumPy library.

The term Median is basically defined as the value that is used to separate the higher range of data samples from a lower range of data samples.

  • The numpy.median() statistical function in the NumPy library is used to compute the median along any specified axis.

  • Thus this function returns the median of the array elements as an output.

  • This function is used to calculate the median of single-dimensional as well as multi-dimensional array.

Syntax of numpy.median():

The syntax required to use this function is as follows:

numpy.median(a, axis = None,out,dtype)

Parameters:

Below we have the description of parameters used by this function:

  • a
    This parameter is used to indicate the input array.

  • axis
    This parameter is used to indicate the axis along which we want to calculate the median. By default, the input array is flattened(that is working on all the axis). Here, for value of axis, axis = 0 means along the column and axis = 1 means working along the row.

  • out
    This is an optional parameter that is used to indicate an alternative array in which we want to place the result. The array must have the same dimensions as the expected output.

  • dtype
    It is an optional parameter that is used to indicate the type we desire while computing the median.

Returned Values:

This function returns the median of the array (it will return a scalar value if the axis is None)) or an array with median values along the specified axis.

Steps used to calculate the Median:

Following are the steps used to calculate the median:

  • There are some given data points as Input.

  • The second step is, to arrange them in an ascending order.

  • If total no. of the terms are odd, then median will be calculated as Median = middle term

  • If total no. of the terms are even, then median will be calculated as Median = Average of the terms in the middle

Example 1:

Given below is a basic example showing you the working of this function:

import numpy as np 

a = [26, 2, 73, 13, 34] 

print("The Input Array is : ") 
print(a)
print("The median of 1D array is : ")
print(np.median(a)) 


The Input Array is :
[26, 2, 73, 13, 34]
The median of 1D array is :
26.0

Example 2:

Now we will apply this method on two-dimensional array and will check the output:

import numpy as np 

inp = [[1, 17, 19, 33, 49], [14, 6, 87, 8, 19], [34, 2, 54, 4, 7]] 

print("\nThe median of array when axis = None : ")
print(np.median(inp)) 

# calculating median along the axis = 0 
print("\nThe median of array when axis = 0 : ")
print(np.median(inp, axis = 0)) 

#calculating median along the axis = 1 
print("\nThe median of array when axis = 1 : ")
print(np.median(inp, axis = 1)) 


The median of array when axis = None :
17.0

The median of array when axis = 0 :
[14. 6. 54. 8. 19.]

The median of array when axis = 1 :
[19. 14. 7.]

Summary

In this tutorial we learned about numpy.median(), a statistical function in the Numpy library. We covered what is median, syntax, parameters, and values returned by this function along with multiple examples for the same.