# 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.

##### You May Also Like ## Python String istitle()

Table of Contents Hide What is titlecase?SyntaxParametersReturn valueExample 1: Python string istitle() working examplesExample 2: Program to check if the string is titlecase Python string istitle() method is a built-in… ## [Solved] TypeError: __init__() missing 2 required positional arguments

Table of Contents Hide What is TypeError: __init__() missing 2 required positional argumentsHow to fix TypeError: __init__() missing 2 required positional argumentsSolution 1 – Pass the required positional argumentsSolution 2… ## Python ValueError: cannot reindex from a duplicate axis

Table of Contents Hide Verify if your DataFrame Index contains Duplicate valuesTest which values in an index is duplicateDrop rows with duplicate index valuesPrevent duplicate values in a DataFrame indexOverwrite… ## Python String isidentifier()

Table of Contents Hide SyntaxParametersReturn ValueExample 1: How isidentifier() method work Example 2: How to use isidentifier() method in actual program The Python String isidentifier() method is a built-in function… ## Python syntaxerror: eol while scanning string literal

Table of Contents Hide What does EOL while scanning string literal error mean in Python?Missing quotation marksString spanning multiple lines using the wrong syntaxMismatch of quotesUsing backslash before the ending… ## [Solved] RuntimeWarning: invalid value encountered in true_divide

Table of Contents Hide What is RuntimeWarning: invalid value encountered in true_divide?How to fix RuntimeWarning: invalid value encountered in true_divide?Conclusion Suppose you attempt to divide the NumPy arrays elements using…