NUMPY MEDIAN() WITH EXAMPLES IN PYTHON

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.

SYNTAX OF NUMPY MEDIAN()

Here we can see the general syntax

numpy.median(aaxis=Noneout=Noneoverwrite_input=Falsekeepdims=False)

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

PARAMETERS OF NUMPY MEDIAN()

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.

RETURN

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.

EXAMPLE

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
a=ppool.array([10,12,14,16,20])
print(ppool.median(a))

b=ppool.array([12,13,16,18,19,22])
print(ppool.median(b))

Output:

14.0
17.0

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
a=[
   [1,2,3,4,5],
   [6,7,8,9,10],
   [0,5,7,8,9],
   [11,4,6,45,1]]
print(ppool.median(a,axis=0))

Output:

[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
a=[[1,3,5,7,9],
   [2,4,6,8,10]]
print(ppool.median(a,axis=1))

Output:

[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

CONCLUSION

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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments