Skip to Content

Get the Median of Numpy Array – (With Examples)

The Numpy library in Python comes with a number of useful built-in functions for computing common descriptive statistics like mean, median, standard deviation, etc. In this tutorial, we will look at how to get the median value in a Numpy array with the help of some examples.

How do you get the median of an array in Numpy?

You can use the Numpy median() function to get the median value of a Numpy array. Pass the array as an argument.

The following is the syntax –

# median of all values in array
numpy.median(ar)

It returns the median of the values in the array. For multi-dimensional arrays, you can specify the axis along which you want to compute the median (see the examples below).

Examples

Let’s now look at some examples of using the above syntax on single and multi-dimensional arrays.

Example 1 – Median of a one-dimensional Numpy array

median of values in numpy array

Let’s first create a one-dimensional Numpy array.

import numpy as np

# create numpy array
ar = np.array([1, 3, 4, 5, 7])
# display the array
print(ar)

Output:

[1 3 4 5 7]

Here, we used the numpy.array() function to create a one-dimensional array containing some numeric values.

Let’s now get the median value in the above array.

# median of array
print(np.median(ar))

Output:

4.0

We get the median as 4.0 since 4 is the middle value in the above array. Note that the array need not be sorted for using the numpy.median() function. The function will do that internally when estimating the middle value.

Example 2 – Median of multi-dimensional Numpy array

First, let’s create a 2-D Numpy array.

# create 2-D numpy array
ar = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])
# display the array
print(ar)

Output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Here, we used the numpy.array() function to create an array with three rows and three columns.

If you use the Numpy median() function on an array without specifying the axis, it will return the median taking into consideration all the values inside the array.

# median of array
print(np.median(ar))

Output:

5.0

We get the median of all the values inside the 2-D array as 5.0 (which is the middle value if you line up all the values in the above 2-D array in sorted order).

Use the numpy.median() function with axis=1 to get the median value for each row in the array.

# median of each row in array
print(np.median(ar, axis=1))

Output:

[2. 5. 8.]

We get the median of each row in the above 2-D array. The median of values – in the first row (1, 2, 3) is 2, in the second row (4, 5, 6) is 5, and in the third row (7, 8, 9) is 8.

Use the numpy.median() function with axis=0 to get the median of each column in the array.

# median of each column in array
print(np.median(ar, axis=0))

Output:

[4. 5. 6.]

We get the median of each column in the above 2-D array. The median of values – in the first column (1, 4, 7) is 4, in the second column (2, 5, 8) is 5, and in the third column (3, 6, 9) is 6.

Summary

In this tutorial, we looked at how to use the numpy.median() function to get the median of values in an array. The following are the key takeaways from this tutorial.

  • Use the numpy.median() function without any arguments to get the median of all the values inside the array.
  • For multi-dimensional arrays, use the axis parameter to specify the axis along which to compute the median. For example, for a 2-D array –
    • Pass axis=1 to get the median of each row.
    • Pass axis=0 to get the median of each column.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.