# numpy.median() – How to compute median in Python

numpy.median function is used to calculate the median of an array along a specific axis or multiple axes. Median is defined as the middle 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 sort the values.

In this post, you will learn how to use the function and compute the median of an array.

## numpy.median(arr, axis=None, out=None)

• Purpose: Used to find the median value from ‘arr’ array using arithmetic median method.

• Parameters:

• arr: array_like This will be our input array to perform median method.
• axis: None or int or tuple of ints, optional Axis on which we perform the arithmetic median if specified. otherwise, the arr will be flattened.
• out: ndarray(optional) Used for defining an alternative output array in which the result is placed. The default is None; if provided it must have the same shape as the expected output. The type of output values will be cast when necessary.
• Returns:
• median: ndarray It returns the middle value of the array. If axis is specified then returns an array with median values.
# Import Packages
import numpy as np


## 1. How to compute median of 1-Dimensional arrays

#### Example 1: Compute median on 1-D array with odd number of elements in the array.

First, let’s create the 1-D Array

# Create 1-D array with odd nnumber of elements in it.
arr = np.array([2,3,5,7,8])


Apply median() function on the above array to find the median score of the array elements

# Compute median
median_arr = np.median(arr)
print("median of array is", median_arr)

median of array is 5.0


How to calculate median when there are odd number of elements?

Calculating Median for odd number of elements in a array:

1. Divide the no.of elements by 2.
2. Round up the quotient to the nearest value.
3. The rounded value will be position value of an array.
4. The element in the specified position value of an array is median value

median = Number of elements / 2
= 5/2

Quotient = 2.5
position value = 3
median = 5.0

array = [2,3,5,7,8]

From the above array the element in the third position is 5. so, the middle score value from the array is 5.

#### Example 2: Compute median on 1-D array with even number of elements in an array.

In this example you will learn how to compute median on 1-D array with even number of elements in an array.

# Create 1-D array with even nnumber of elements in it.
array = np.array([3,7,4,5,10,4])


Apply median() function on the above array to find the middle score of the array elements.

# Compute median
median_array = np.median(array)
print(median_array)

4.5


How to calculate median when there are even number of elements?

Median for even number of elements in a array:

array = [3,7,4,5,10,4]

1. Take the middle value pairs from the given array i.e [4,5]
2. Sum those middle value pairs = 4+5 = 9
3. Divide the sum value with 2
4. The returned quotient value will be the median or middle score value of the array.

Middle value pairs = 4,5
sum of middle value pairs = 4+5 = 9

Median = sum of middle value pairs / 2
= 9/2
Median = 4.5


## 2. How to compute median of 2-D arrays

#### Example 3: Compute median on 2-D array with odd number of elements in an array.

# create a 2-D array
arr = np.array([[2,6,5],
[4,2,3]])
arr

array([[2, 6, 5],
[4, 2, 3]])


Apply median() function on the above array to find the middle score of the array elements.

# compute median
median_arr = np.median(arr)
print(median_arr)

3.5


#### Example 4: Compute median on 2-D array define along specified axis

Create the 2D array.

# create a 2-D array
arr = np.array([[2,4,7],
[6,3,1]])


Specify axis=0 to compute the median column wise.

# compute median, specify axis=0
median_arr = np.median(arr, axis=0)
print(median_arr)

[4.  3.5 4. ]


Specify axis=1 to compute the median row-wise

# create a 2-D array
arr = np.array([[2,4,7],[6,3,1]])


Apply median() function on the above array to find the middle score of the array elements with specified axis.

# compute median
median_arr = np.median(arr, axis=1)
print(median_arr)


## 3. How to compute median of arrays with missing values.

Create array with missing value and use np.nanmedian() to compute the median. If we use the regular np.median(), it will raise an error.

# Create 1-D array with even nnumber of elements in it.
array = np.array([3,7,4,5,np.nan,4])
np.nanmedian(array)

4.0


Q1: Can you perform median operation when axis=1 on 1-D array?

Answer: No, you cannot perform median operation when axis =1 on 1-D array.This returns a axis error.

## Similar Articles

### Logistic Regression – A Complete Tutorial With Examples in R Course Preview

## Machine Learning A-Z™: Hands-On Python & R In Data Science

### Free Sample Videos: #### Machine Learning A-Z™: Hands-On Python & R In Data Science #### Machine Learning A-Z™: Hands-On Python & R In Data Science #### Machine Learning A-Z™: Hands-On Python & R In Data Science #### Machine Learning A-Z™: Hands-On Python & R In Data Science 