Numpy Median Method Implementation with Examples

Numpy Median Method Implementation

Numpy has many mathematical methods that allow you to do computational work very fast. The Numpy median() method is one of them. It calculates the median of NumPy arrays and returns output as an array. Below is the syntax for the numpy.median() method.

numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)

The explanation of the parameters of the method is below.

a: It is the input NumPy array.

axis: Use it for calculation of median along row-wise or column-wise.

out: The output of the method you want to store. It can be an alternate array.

overwrite_input: The default value is false. If it is set to true then allow the use of memory of input array for calculations.

keepdims: Default is False. If this is set to true then the result will broadcast correctly against the original array.

Now I will show you the different examples where the numpy.median() method can be applied.

Examples for Calculating Numpy Median

In this section, I will discuss how to calculate the numpy median for one dimension as well as a multi-dimensional NumPy array.

Example 1: Find the median for a 1D Numpy array.

Here in this example, you will know how to find the median of the NumPy array of a single dimension. Let’s create a NumPy array.

You can create a NumPy array using the method np.array().

array_1d = np.array([10,20,30,40,50,60,70])

After the creation pass the array inside the median() method to get the results.

np.median(array_1d)

You will get a single output like below.

"<yoastmark

Example 2: Numpy median for 2D Numpy array.

The same median() method used to find the median for the two or more dimensional NumPy array. Use the code below to generate a 2-D Numpy array.

array_3x4 = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,120]])

There are three approaches for finding the median of the 2D Array. Let’s look at them.

Find the median of all the elements present in the array.

To calculate it just pass the array inside the median() method.

Median of all the elements present in the 2D array
Median of all the elements present in the 2D array

Median of each column of the array

The median of each column of the NumPy array can be found by passing the extra parameter. The parameter is axis=0.

np.median(array_3x4,axis=0)

The above code will output the array of medians. Each index value represents the median for that column.

Median of each column of 2D array
Median of each column of 2D array

Median of each row of the array

The same approach applied to find the median of each row. Here you have to pass axis =1. It will output the array of the median with each index value as the median of that row.

Median of each row of 2D array
Median of each row of 2D array

Other Approaches

Approach 1

There is also another approach to find the median of the 2D Array. Here you are first creating an empty array and passing out the results to that array. For example, I will create a zero NumPy array of the same size as the median calculated for it to each column. Then I will output the results by assigning them to that array.

Execute the following code to implement this approach.

array_3x4 = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,120]])
m = np.median(array_3x4,axis=0)
dummy = np.zeros_like(m)
np.median(array_3x4,axis=0,out=dummy)

Output

Output the median to empty array
Output the median to an empty array

Approach 2

Another approach to get the median is by overriding the dimension of the input array. Here I am first copying the input array. And then taking the results to it by adding an additional parameter, overwrite_input = True.

For example, I want to get the median for each column of the array I will execute the following code.

array_3x4 = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,120]])
copy_2d_array
np.median(copy_2d_array,axis=1,overwrite_input=True)y = np.copy(array_3x4)

Output

Output the median to copy array by changing the dimension
Output the median to copy an array by changing the dimension

Source:

Official Numpy Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner