# numpy.median() Function

The NumPy library has a `numpy.median()` function is used to compute the median of an array in sorted order on a specified axis and returns the median of the array elements.

## What is a Median?

If the data set has an odd number of observations, the middle value is selected as a median. For example, the following list of seven numbers,1, 3, 3, 6, 7, 8, 9 will have a median of 6, which is the fourth value since the list has an odd count.

If the data set has an even number of observations, there is no distinct middle value and the median is usually defined to be the arithmetic mean of the two middle values. For example, this data set of 8 numbers1, 2, 3, 4, 5, 6, 8, 9 has a median value of 4.5, which is

## Syntax

The Syntax of `numpy.median()` method is:

``````numpy.median(arr, axis=None, out=None, overwrite_input=True, keepdims=True)
``````

## Parameters

The `numpy.median()` method can take the following parameters.

• arr: nd_array -the name of the input array
• axis: {None or int or tuple of ints} (optional) -the alignment or axis of the mean to be computed. The default is to compute the median along a flattened version of the array.
• out: nd_array (optional) – Alternate output `dtype` in which the results are to be returned. The default value is `None`.
• overwrite_input: bool (optional) – If the parameter is given as `True` then the compiler will allow the use of memory of the input array for calculations and the input array will be modified. The default value is `False`.
• keepdims: bool (optional) – If the value of `keepdims` is set to `True`, then the axes which are reduced will be left in the result as dimensions with size one.

## Return Value

The numpy.median() method will return a new array with median values.

• If the input contains integers or floats smaller than `float64`, the output data type returned is `np.float64`.
• Otherwise, the data type of the output is the same as that of the data type of the input.

## Example 1: Calculate the median of the array using numpy.median() method

The `numpy.median()` method will compute the median of the array and returns the `float64 `object as output.

``````# import numpy to be used as np
import numpy as np

# initialize the array
array_in = np.arange(9).reshape(3, 3)
print("The input array is :")
print(array_in)

# median of the elements
array_out = np.median(array_in)
print("The median of the array is:",array_out)
print(type(array_out))
``````

Output

``````The input array is :
[[0 1 2]
[3 4 5]
[6 7 8]]
The median of the array is:4.0
<class 'numpy.float64'>``````

## Example 2: Calculate the median of the array as column-major order

When the axis parameter is specified as `axis = 0`, then the column-major order will be called off for the median of the elements, that is the median of 3 columns is computed and returned an array of shape (3, ).

``````# import the numpy module
import numpy as np

# initialize the input array
array_in = [[15, 25, 30], [35, 55, 70]]

print("The input numpy array is:\n", array_in)

# the mean of the array as
print(
"The median of the array in column-major is:\n",
np.median(array_in, axis=0),
np.median(array_in, axis=0).shape,
type(np.median(array_in, axis=0)),
)
``````

Output

``````The input numpy array is:
[[15, 25, 30], [35, 55, 70]]

The median of the array in column-major is:
[25. 40. 50.] (3,) <class 'numpy.ndarray'>``````

## Example 3: the median of the array is considered as row-major order

When the axis parameter is specified as `axis = 1`, then the row-major order will be called off for the median of the elements, that is the median of 2 rows is computed and returned an array of shape (2, ).

``````# import the numpy module
import numpy as np

# initialize the input array
array_in = [[15, 25, 30], [35, 55, 70]]

print("The input numpy array is:\n", array_in)

# the mean of the array as
print(
"The median of the array in row-major is:\n",
np.median(array_in, axis=1),
np.median(array_in, axis=1).shape,
)
``````

Output

``````The input numpy array is:
[[15, 25, 30], [35, 55, 70]]

The median of the array in row-major is:
[25. 55.] (2,)``````

## Conclusion

We can compute the median of an Array in NumPy using the `numpy.median()` method, it computes the median of an array in sorted order on a specified axis and returns the new meidan array elements.

Reference: NumPy Library

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

## Python Print Variable

Table of Contents Hide How to use the print() function in Python?How to Print variable in Python?Method 1: Using comma , character to separate the variables in a print statementMethod…

## [Solved] Error: command errored out with exit status 1

If you are installing auto-py-to-exe package on Python 3.8 or below, you will get an error stating ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full…

## TypeError: unhashable type: ‘list’

Table of Contents Hide TypeError: unhashable type: ‘list’Example – unhashable type: ‘list’Solution to TypeError: unhashable type: ‘list’.Solution 1 – By Converting list into a tupleSolution 2 – By Adding list…

## Python ValueError: setting an array element with a sequence

In Python, if you are mainly working with numpy and creating a multi-dimensional array, you would have encountered valueerror: setting an array element with a sequence. What is valueerror: setting an…

## Python chr(): A Step-By-Step Guide

The chr() function takes an integer (representing Unicode) as an input argument and returns a string representing a character. Syntax – chr(num) Parameter and Return Value The chr() function can…

## TypeError: ‘list’ object is not callable

Table of Contents Hide Python TypeError: ‘list’ object is not callableScenario 1 – Using the built-in name list as a variable nameSolution for using the built-in name list as a…