Compute the median of the flattened NumPy array


Median

The median is the statistical measure for the central tendency that represents the middle value of the sorted list of values. In other words, we can say that the median is the value which separates the upper half of the dataset from the lower half of the dataset.

The mathematical formula for calculating the median is as follows when the total number of elements is odd.

Median = (n+1)/2

Where, n is the last element from the given set.

Flattened array

Flattening is a process of reducing the dimensionality of an array. flattened array is a 1-d array created by flattening the multidimensional arrays, in which all the elements in the array will be concatenated into one single row.

In Numpy we have two functions ravel() and flatten() for flattening the arrays. Any of the mentioned methods can be used to flatten the given multidimensional array.

Syntax

Similarly we can calculate the median of an array using the median() function.

array.flatten()
np.median(flatten_array)

Where,

  • Numpy is the name of the library

  • flatten is the function used to flatten the given array

  • array is the input array

  • median is the function used to find the median of the given array

  • flatten_array is the variable which holds the flattened array

Example

In order to calculate the median of an array first of all, we should flatten it using the flatten() function and pass the resultant flattened-array value to the median() function as shown in the following example –

import numpy as np
a = np.array([[[34,23],[90,34]],[[43,23],[10,34]]])
print("The input array:",a)
flattened_array = a.flatten()
print("The flattened array of the given array:",flattened_array)
med = np.median(flattened_array)
print("The median of the given flattened array:",med)

Output

Following is the output of the median calculated for the flatten array.

The input array: [[[34 23]
  [90 34]]

 [[43 23]
  [10 34]]]
The flattened array of the given array: [34 23 90 34 43 23 10 34]
The median of the given flattened array: 34.0

Example

Let’s see another example where we are trying to calculate the median of a 3D array –

import numpy as np
a = np.array([[[23,43],[45,56]],[[24,22],[56,78]]])
print("The input array:",a)
flattened_array = a.flatten()
print("The flattened array of the given 3-d array:",flattened_array)
med = np.median(flattened_array)
print("The median of the given flattened array:",med)

Output

The input array: [[[23 43]
[45 56]]
[[24 22]
[56 78]]]
The flattened array of the given 3-d array: [23 43 45 56 24 22 56 78]
The median of the given flattened array: 44.0

Example

This is another example of finding the median of the 5-d flattened array.

import numpy as np
a = np.array([[[23,43],[45,56]],[[24,22],[56,78]]],ndmin = 5)
print("The input array:",a)
print("The dimension of the array:",np.ndim(a))
flattened_array = a.flatten()
print("The flattened array of the given 3-d array:",flattened_array)
med = np.median(flattened_array)
print("The median of the given flattened array:",med)

Output

The input array: [[[[[23 43]
[45 56]]
[[24 22]
[56 78]]]]]
The dimension of the array: 5
The flattened array of the given 3-d array: [23 43 45 56 24 22 56 78]
The median of the given flattened array: 44.0

Updated on: 07-Aug-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements