Python NumPy median()
function is used to compute the median of the given NumPy array over the specified axis or multiple axes. The term median is the value separating the higher half from the lower half of a data sample in other words median is a value in the middle when you sorted the values in the ascending order.
The numpy.median()
function is used to calculate the median of single-dimensional as well as multi-dimensional arrays. In this article, I will explain how to use the NumPy median() function in Python to return the median of the array elements.
1. Quick Examples of Python NumPy median() Function
If you are in a hurry, below are some quick examples of how to compute Python NumPy median()
function.
# Below are the quick examples
# Example 1: # Get the median value of 1-D array
arr = [12, 7, 15, 8, 9, 5, 3]
arr1 = np.median(arr)
# Example 2: Get the median value of 2-D array
arr = np.array([[5, 9, 7, 11], [8, 14, 15, 19],[32, 24, 19, 28]])
arr1 = np.median(arr)
# Example 3: Use numpy median() along axis = 0
# Get the median value of row
arr1 = np.median(arr, axis = 0)
# Example 4: use numpy median() along axis = 1
# Get the median value of column
arr1 = np.median(arr, axis = 1)
# Example 5: Use numpy.median() function to set out parameter
median=np.zeros(np.median(arr,axis=1).shape)
arr2 = np.median(arr,axis=1,out=median)
2. Syntax of median()
Following is the syntax of the numpy.median()
function.
# Syntax of numpy.median()
numpy.median(arr, axis=None, out=None, overwrite_input=False, keepdims=False)
2.1 Parameters of median()
arr
– Input array or object, which could be converted into an array.axis
– This parameter defines the axis along which medians are computed. By default, the median is computed of the flattened array. axis = 0 means along the row and axis = 1 means working along the column.out
– The output of the function you want to store. It can be an alternate output array.overwrite_input
– It is a boolean and optional. If True, then allow the use of memory of input array a for calculations. The input array will be modified by the call to themedian()
function.keepdims
– It is a boolean and optional. If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
2.2 Return Value of median()
This function returns the median of the array or an array with medians along the specified axis.
3. Usage of NumPy median() Function
The numpy.median()
function in the NumPy library is used to calculate the median value along with the specified axis of single-dimensional as-well as multi-dimensional array. This function returns the median value of the array as an output.
The following steps are shown how to calculate the median value
- Given data points.
- Arrange data in ascending order.
- If a total number of terms are odd, then the median value is equal to the middle term.
- If a total number of terms are even, then the median value is equal to the average of the terms in the middle.
3.1 Get the Median Value of 1-D NumPy Array
Let’s take a one-dimensional NumPy array and calculate the median()
of it. First, create the 1-D NumPy array and pass this an input to the median() function. For example, As I said above, it first sorts the elements internally and returns the middle value.
import numpy as np
# Get the median value of 1-D array
arr = [12, 7, 15, 8, 9, 5, 3]
arr1 = np.median(arr)
print(arr1)
# Output
# 8.0
4. Get the Median value of Multi-Dimensional Array
When used a median() on the multi-dimensional NumPy array, it by default returns the middle values of all elements reason being by default, the median is computed of the flattened array. In the following example, 14
and 15
are middle values hence, it returns 14.5
which is the average of these two values.
# Create 2-D numpy array
arr = np.array([[5, 9, 7, 11], [8, 14, 15, 19],[32, 24, 19, 28]])
# Get the median value of 2-D array
arr1 = np.median(arr)
print(arr2)
# Output
# 14.5
5. Get Median Value of Array Along with axis
We can compute the median value of a numpy array along with a specified axis. To find the median values of each row
use. axis=0
, and to get the median values of each column use axis=1
. In the following example, I have demonstrated these two examples.
# Use numpy median() along axis = 0
# Get the median value of row
arr1 = np.median(arr, axis = 0)
print(arr1)
# Output
# [ 8. 14. 15. 19.]
# use numpy median() along axis = 1
# Get the median value of column
arr1 = np.median(arr, axis = 1)
print(arr1)
# Output
# [ 8. 14.5 26. ]
6. Function To Set Out Parameter
You can also use set out parameters using this function.
# Use numpy.median() function to set out parameter
median=np.zeros(np.median(arr,axis=1).shape)
arr2 = np.median(arr,axis=1,out=median)
print(arr2)
# Output
# [ 8. 14.5 26. ]
7. Conclusion
In this article, I have explained how to use numpy.median()
function to compute the median value of an array along with the specified axis. The term median is the value separating the higher half from the lower half of a data sample in other words median is a value in the middle when you sorted the values in ascending order.
Happy Learning!!
Related Articles
- How to get square values of an array?
- Gets round values of NumPy array
- How to create NumPy one’s array?
- numpy.delete() Function
- numpy.divide() Function
- NumPy Array Addition
- NumPy nanmean() – Get Mean ignoring NAN Values
- How To Compute Average Of NumPy Array?