Hello geeks and welcome in today’s article, we will discuss NumPy Median(). Along with that, we will also look at its syntax and various parameters. We will also look at various examples that will help us understand the topic better. Median is one of the 3 M’s of statistics. Median is the central value of data when arranged in a particular order. In general, the formula for median calculation is (n+1)/2th term for the odd number of terms and mean of (n/2)th and (n/2 +1)th term for the even number of terms. NumPy being a powerful mathematical library of Python, provides us with a function Median. So we can conclude that NumPy Median() helps us in computing the Median of the given data along any given axis. Now let us look at the various aspects associated with it one by one.


Here we can see the general syntax


The syntax contains several parameters that we will be looking into detail up next.


a: array_like

The parameter represents the input array or objects that can be converted into an array. This generally the input of which the user wants to find the median.

axis: int, a sequence of int, none

This is the parameter along which the whole operation is performed. By default it is set to calculate along the flattened array.

out: ndarray

It is an optional parameter. It must have the same shape and buffer as the expected output array.

overwrite_input: bool

It is another optional parameter. If it is equal to true, it allows the memory of the input array for the calculation. This helps us by saving memory when we don’t wish to preserve it. In that case, it is undefined that will be partially or fully sorted. By default, it is set to “FALSE.”

Keepdims: bool

It is another optional parameter. If it is set to “TRUE,” with the help of this parameter, the results will be broadcasted correctly against the input array.


median: ndarray

It returns a new array holding the results. If the input contains values smaller than the float64(allows the larger number to be stored), then the output data-type is np.float64. Otherwise, the data-type of input and output is the same.


Now let us look at a couple of examples which will help us in understanding the concept in much better manner.

import numpy as ppool




Here in the above example, we used NumPy Median() to calculate the median. First, we have an imported NumPy library. Up next, we have defined an array. At last, we have used our Syntax to find out the median for the input array. Above, we have considered 2 different arrays one having an odd number of terms while the other having an even number of terms. Now let us look at another example and play with the syntax for a bit.

NumPy Median with axis=0

import numpy as ppool


[3.5 4.5 6.5 8.5 7. ]

In this example, we have used the steps similar to our first example. First importing array, up next defining the array. But here, we have a defined axis equal to zero. In this case, we get an output equivalent to [3.5,4.5,6.5,8.5,7]. Now I will explain the logic behind this output. Like in maths, the program arranges the value in ascending order for each same index of the subarray. Like order of [0,1,6,11] for the index value zero. Next, since the number of terms here is even, it takes n/2 th and n/2+1 th terms of array 1 and 6. Next, calculate the mean of 2 terms, which gets us our median value for that index number like 3.5 for index=0. Similarly, the process is repeated for every index number.

NumPy Median with axis=1

import numpy as ppool


[5. 6.]

Here in the above example, we can see that we have considered a 2-dimensional array. As always, first, we import the NumPy library. In the next step, we have defined our 2-d array. Here we have specified the axis to be 1. As a result of which we don’t get a flattened array in the output. Up next, it finds out the median for the 2 sub-arrays. Due to which we get 5 and 6 as the median in the output.

NumPy median filter

A median filter is used for Image manipulation or Image processing. A median filter occupies the intensity of the central pixel. It does a better job than the mean filter in removing. It preserves the edges of an image but does not deal with the speckle noise. Speckle is mostly detected in the case of medical images and active radar images.

Must Read


In this article, we covered the NumPy Median(). We looked at its syntax and different parameters. For better understanding, we also looked at a couple of examples. In the end, we can conclude that NumPy Median helps us by calculating the median for our input values. I hope this article was able to clear all your doubts. But in case if you still have any unsolved queries feel free to write them below in the comment section. Done reading this why not read Numpy dot product next.

5 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments