The NumPy library has a ** numpy.median() **method 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

## numpy.median() Syntax

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

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

## numpy.median() 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

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**True**.`False`

**keepdims: bool (optional**) – If the value of`keepdims`

is set to

, then the axes which are reduced will be left in the result as dimensions with size one.**True**

## numpy.median() Return Value

The **numpy.median()** method will return an new array with median values. If the input contains integers or floats smaller than `float64`

, then the output data-type returned is `np.float64`

. Otherwise, the data-type of the output is the same as that of the input. If *out* is specified, that array is returned instead.

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