numpy.median() in Python

The Python numpy.median() function calculates the median of given data along the specified axis.

Basic Syntax

Following is the basic syntax for numpy.median() function in Python:

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

And the parameters are:

ParameterDescription
arr[array-like] The input array
axis[default = None, OPTIONAL] int or sequence of int for the axis
out[default = None, OPTIONAL] different array must be with same dimension to place the result.

Return Value

This function returns a scalar value if axis = None or median of the array along given axis.

Example

Following are the examples:

Example 1

The output for the above program is as given below:

The Median, axis = None: 5.0
Median, axis = 0: [ 4., 5., 6.]
Median, axis = 1: [ 2., 5., 8.]

Example 2

The output for the above program is as given below:

my_array : [10, 20, 30, 40, 50, 60, 70, 80, 90]
median of my_array : 50.0
my_array2 : [15, 69, 78, 40, 56]
median of my_array : 56.0

LEAVE A REPLY

Please enter your comment!
Please enter your name here