Python numpy median – Python NumPy median() Function

NumPy median() Function:

Python numpy median: The median along the given axis is computed using the median() function of the NumPy module. By default, the median is calculated over the flattened array; otherwise, it is calculated over the given axis.

Syntax:

numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)

Parameters

a: This is required. It is an array of numbers whose mean is to be computed.

axis: This is optional. Indicate which axis or axes will be used to determine the medians. The default, axis=None, computes the flattened array’s median.

out: This is optional. It Indicates the output array for the result.  None is the default value. It must have the same shape as output if it is provided.

overwrite_input: This is optional. The input array will be changed if True. An error will be thrown if overwrite input is True and “a” is not already an ndarray. False is the default value.

keepdims: This is optional. The reduced axes are left in the output as dimensions with size one if this is set to True. The result will broadcast correctly against the input array if you use this option.

Return Value:

Python median numpy: When out=None, returns an array with the median values; otherwise, returns a reference to the output array.

NumPy median() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the mean() function of numpy module to calculate the mean of all values in the given array
  • Store it in another variable.
  • Print the mean of all values in the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5,20],[35, 40]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the median() function of numpy module 
# to calculate the median of all values in the given array 
# Store it in another variable.
rslt = np.median(gvn_arry)
# Print the median of all values in the given array 
print("The median of all values in the given array =", rslt)

Output:

The above given array is:
[[ 5 20]
 [35 40]]
The median of all values in the given array = 27.5

Example2

Numpy.median: The median is calculated over the specified axes when the axis parameter is set.

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, axis=0 as the arguments to the median() function of numpy module to calculate the median in the given array along axis=0.
  • Store it in another variable.
  • Print the median in the given array along axis=0
  • Pass the given array, axis=0 as the arguments to the median() function of numpy module to calculate the median in the given array along axis=1.
  • Store it in another variable.
  • Print the median in the given array along axis=1
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=0 as the arguments to the median() function of numpy module 
# to calculate the median in the given array along axis=0
# Store it in another variable.
rslt_1 = np.median(gvn_arry, axis=0)
# Print the median in the given array along axis=0
print("The median in the given array along axis=0: ", rslt_1)
# Pass the given array, axis=1 as the arguments to the median() function of numpy module 
# to calculate the median in the given array along axis=1
# Store it in another variable.
rslt_2 = np.median(gvn_arry, axis=1)
# Print the median in the given array along axis=1
print("The median in the given array along axis=1: ", rslt_2)

Output:

The above given array is:
[[ 5 25 50]
 [35 40 60]]
The median in the given array along axis=0: [20. 32.5 55. ]
The median in the given array along axis=1: [25. 40.]