- numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)[source]#
-
Compute the median along the specified axis.
Returns the median of the array elements.
- Parameters:
-
- aarray_like
-
Input array or object that can be converted to an array.
- axis{int, sequence of int, None}, optional
-
Axis or axes along which the medians are computed. The default
is to compute the median along a flattened version of the array.
A sequence of axes is supported since version 1.9.0. - outndarray, optional
-
Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output,
but the type (of the output) will be cast if necessary. - overwrite_inputbool, optional
-
If True, then allow use of memory of input array a for
calculations. The input array will be modified by the call to
median
. This will save memory when you do not need to preserve
the contents of the input array. Treat the input as undefined,
but it will probably be fully or partially sorted. Default is
False. If overwrite_input isTrue
and a is not already an
ndarray
, an error will be raised. - keepdimsbool, optional
-
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original arr.New in version 1.9.0.
- Returns:
-
- medianndarray
-
A new array holding the result. If the input contains integers
or floats smaller thanfloat64
, then the output data-type is
np.float64
. Otherwise, the data-type of the output is the
same as that of the input. If out is specified, that array is
returned instead.
Notes
Given a vector
V
of lengthN
, the median ofV
is the
middle value of a sorted copy ofV
,V_sorted
– i
e.,V_sorted[(N-1)/2]
, whenN
is odd, and the average of the
two middle values ofV_sorted
whenN
is even.Examples
>>> a = np.array([[10, 7, 4], [3, 2, 1]]) >>> a array([[10, 7, 4], [ 3, 2, 1]]) >>> np.median(a) 3.5 >>> np.median(a, axis=0) array([6.5, 4.5, 2.5]) >>> np.median(a, axis=1) array([7., 2.]) >>> m = np.median(a, axis=0) >>> out = np.zeros_like(m) >>> np.median(a, axis=0, out=m) array([6.5, 4.5, 2.5]) >>> m array([6.5, 4.5, 2.5]) >>> b = a.copy() >>> np.median(b, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a==b) >>> b = a.copy() >>> np.median(b, axis=None, overwrite_input=True) 3.5 >>> assert not np.all(a==b)
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
numpy.median(arr, axis = None)
: Compute the median of the given data (array elements) along the specified axis.
How to calculate median?
- Given data points.
- Arrange them in ascending order
- Median = middle term if total no. of terms are odd.
- Median = Average of the terms in the middle (if total no. of terms are even)
Parameters :
arr : [array_like]input array.
axis : [int or tuples of int]axis along which we want to calculate the median. Otherwise, it will consider arr to be flattened(works on all the axis). axis = 0 means along the column and axis = 1 means working along the row.
out : [ndarray, optional] Different array in which we want to place the result. The array must have the same dimensions as expected output.
dtype : [data-type, optional]Type we desire while computing median.Results : Median of the array (a scalar value if axis is none) or array with median values along specified axis.
Code #1:
import
numpy as np
arr
=
[
20
,
2
,
7
,
1
,
34
]
print
(
"arr : "
, arr)
print
(
"median of arr : "
, np.median(arr))
Output :
arr : [20, 2, 7, 1, 34] median of arr : 7.0
Code #2:
import
numpy as np
arr
=
[[
14
,
17
,
12
,
33
,
44
],
[
15
,
6
,
27
,
8
,
19
],
[
23
,
2
,
54
,
1
,
4
, ]]
print
(
"nmedian of arr, axis = None : "
, np.median(arr))
print
(
"nmedian of arr, axis = 0 : "
, np.median(arr, axis
=
0
))
print
(
"nmedian of arr, axis = 1 : "
, np.median(arr, axis
=
1
))
out_arr
=
np.arange(
3
)
print
(
"nout_arr : "
, out_arr)
print
(
"median of arr, axis = 1 : "
,
np.median(arr, axis
=
1
, out
=
out_arr))
Output :
median of arr, axis = None : 15.0 median of arr, axis = 0 : [15. 6. 27. 8. 19.] median of arr, axis = 1 : [17. 15. 4.] out_arr : [0 1 2] median of arr, axis = 1 : [17 15 4]
Last Updated :
28 Nov, 2018
Like Article
Save Article
Вычислите медиану вдоль указанной оси.
Возвращает медиану элементов массива.
- Parameters
-
- aarray_like
-
Входной массив или объект,который может быть преобразован в массив.
- ось {int, последовательность int, None}, необязательно
-
Оси или топоры,по которым вычисляются медианы.По умолчанию медиана вычисляется вдоль сплющенной версии массива.Последовательность осей поддерживается начиная с версии 1.9.0.
- outndarray, optional
-
Альтернативный выходной массив,в который помещать результат.Он должен иметь ту же форму и длину буфера,что и ожидаемый вывод,но при необходимости будет приведен тип (тип вывода).
- overwrite_inputbool, optional
-
Если True, то разрешить использование памяти входного массива
a
для вычислений. Входной массив будет изменен вызовомmedian
. Это сэкономит память, когда вам не нужно сохранять содержимое входного массива. Считайте ввод неопределенным, но он, вероятно, будет полностью или частично отсортирован. По умолчанию – False. Еслиoverwrite_input
имеет значениеTrue
, аa
еще не являетсяndarray
, возникнет ошибка. - keepdimsbool, optional
-
Если установлено значение True, уменьшенные оси останутся в результате как размеры с размером один. С этой опцией результат будет транслироваться правильно относительно исходного
arr
.Новинка в версии 1.9.0.
- Returns
-
- medianndarray
-
Новый массив, содержащий результат. Если входные данные содержат целые числа или числа с плавающей
float64
, меньшие, чем float64 , то тип выходных данных –np.float64
. В противном случае тип данных вывода такой же, как и у ввода. Если указанout
, вместо этого возвращается этот массив.
Notes
Для вектора V
длины N
, медиана V
является средним значением отсортированной копии V
, V_sorted
– т.е., V_sorted[(N-1)/2]
, когда N
нечетно, и среднее значение два средних значения V_sorted
, когда N
четно.
Examples
>>> a = np.array([[10, 7, 4], [3, 2, 1]]) >>> a array([[10, 7, 4], [ 3, 2, 1]]) >>> np.median(a) 3.5 >>> np.median(a, axis=0) array([6.5, 4.5, 2.5]) >>> np.median(a, axis=1) array([7., 2.]) >>> m = np.median(a, axis=0) >>> out = np.zeros_like(m) >>> np.median(a, axis=0, out=m) array([6.5, 4.5, 2.5]) >>> m array([6.5, 4.5, 2.5]) >>> b = a.copy() >>> np.median(b, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a==b) >>> b = a.copy() >>> np.median(b, axis=None, overwrite_input=True) 3.5 >>> assert not np.all(a==b)
In this Python NumPy tutorial, we will learn how to get median of a NumPy array in Python.
To find the median of a numpy array in Python, we use the Python numpy.median() function. In Python, this is a mathematical function and it is used to compute the median of the elements in an array.
This method is available in the NumPy package module and always returns the median of the numpy array value as an output. If you are using a multidimensional array then you can also get the median value of each column and row.
Syntax:
Let’s have a look at the Syntax and understand the working of Python numpy.median() function
numpy.median
(
a,
axis=None,
out=None,
overwrite_input=False,
keepdims=False
)
- This Syntax contains several parameters
- a: This parameter specifies the input array on which we want to operate on.
- axis: This parameter indicates the axis on which we want to calculate the median along with the flattened input array. If the axis is 0 then the direction down the rows and if the axis is 1 then the direction down column-wise.
- out: It is an optional parameter and it is used to store the result of numpy.median() function and by default it takes none value.
- keepdims: This parameter defines the dimension of the input array and if the value is ‘True’ the axes which are reduced are left in the output.
Example:
Let’s take an example and check how to use the numpy.median() function in Python
Source Code:
import numpy as np
new_arr=np.array([67,89,113,145,167])
new_output= np.median(new_arr)
print("Median of an array:",new_output)
In the above code, we imported the numpy library and then initialize an array by using the numpy.array() function and now we have to find the median of the input array. To do this we are going to use the numpy.median() function.
In the above array, we have an odd number of terms in ascending order. It will calculate the array median=middle term.
Here is the implementation of the following given code
Read: Python NumPy Replace + Examples
Python Numpy Median
The median is a measure of central tendency that is commonly used in statistics to find the middle value of a dataset. In NumPy, there are several ways to compute the median of an array or a sequence of numbers. These methods include using the median() function, partitioning, percentile(), etc.
Method-1: Python numpy median using the np.median() function
The easiest way to calculate the median in NumPy is to use the np.median() function, which calculates the median of an array along a specified axis.
# Import the NumPy library
import numpy as np
# Create a one-dimensional array containing the values 67, 89, 113, 145, and 167
new_arr = np.array([67, 89, 113, 145, 167])
# Calculate the median of the array using the np.median() function
new_output = np.median(new_arr)
# Print the calculated median using the print() function, along with a message
print("Median of an array:", new_output)
The above code imports the NumPy library and creates a one-dimensional array new_arr containing the values 67, 89, 113, 145, and 167.
- The code then calculates the median of the array using the np.median() function and assigns the result to the variable new_output.
- Finally, the code prints the calculated median using the print() function, along with the message “Median of an array:”.
Method-2: Python numpy median using np.sort() function
Another way to calculate the median in NumPy is to use the np.sort() function to sort the array, and then take the middle element(s) as the median.
# Import the NumPy library
import numpy as np
# Create a NumPy array containing the values 1, 3, 2, 4, and 5
arr = np.array([1, 3, 2, 4, 5])
# Sort the array using the np.sort() function
sorted_arr = np.sort(arr)
# Get the length of the array
n = len(arr)
# Check if the length of the array is even or odd, and calculate the median accordingly
if n % 2 == 0:
median = (sorted_arr[n//2-1] + sorted_arr[n//2])/2
else:
median = sorted_arr[n//2]
# Print the calculated median, along with a message
print(median)
The above code calculates the median of a given NumPy array. To do this, it first sorts the array in ascending order using the np.sort() function.
- Then, it checks if the length of the array is even or odd by checking if the remainder of n % 2 is 0 or not, where
n
is the length of the array. If the length is even, the code calculates the median by taking the average of the two middle values in the sorted array. - If the length is odd, the code calculates the median by taking the middle value in the sorted array. Finally, the code prints the calculated median value using the print() function.
Output: 3.0
Method-3: Python numpy median using np.partition() function
Similar to Method 2, this method first partitions the array using numpy.partition()
, then uses indexing to extract the median value.
import numpy as np
# Create a NumPy array
array = np.array([1, 2, 3, 4, 5])
# Use np.partition() to partition the array around its median
partitioned_array = np.partition(array, len(array) // 2)
# Get the length of the partitioned array
n = len(partitioned_array)
# Calculate the median of the partitioned array
# If the length is odd, the median is the middle value
# If the length is even, the median is the average of the two middle values
median = partitioned_array[n // 2] if n % 2 != 0 else (partitioned_array[n // 2 - 1] + partitioned_array[n // 2]) / 2
# Print the median
print(median)
The above code imports the NumPy library and creates an array with values [1, 2, 3, 4, 5].
- It then partitions the array using np.partition() around its median value, and calculates the median value of the partitioned array. If the partitioned array has an odd length, the median is the middle value.
- If the partitioned array has an even length, the median is the average of the two middle values. Finally, the code prints the median value to the console.
Output: 3
Method-4: Python numpy median using numpy.mean() of the two middle values
Another way to calculate the median of a NumPy array is to find the two middle values (if the length of the array is even) and take their average using numpy.mean().
import numpy as np
# Create a NumPy array
arr = np.array([11, 12, 13, 14, 15])
# Get the length of the array
n = len(arr)
# Sort the array
sorted_arr = np.sort(arr)
# Calculate the median of the sorted array
# If the length is odd, the median is the middle value
# If the length is even, the median is the average of the two middle values
median = (sorted_arr[(n - 1) // 2] + sorted_arr[n // 2]) / 2
# Print the median
print(median)
In the above code, the input array arr is sorted using np.sort(), and then the median value of the sorted array is calculated using a formula.
- If the length of the array is odd, the median is simply the middle value of the sorted array. If the length of the array is even, the median is the average of the two middle values of the sorted array.
- Finally, the median value is printed to the console using print().
Output: 13.0
Method-5: Python numpy median using numpy.percentile()
numpy.percentile() is a function that returns the specified percentile(s) of an array. To find the median, we can use the 50th percentile.
import numpy as np
# Create a NumPy array
arr = np.array([11, 12, 13, 14, 15])
# Calculate the median using the np.percentile() function
# The second argument to np.percentile() specifies the percentile value to compute,
# so setting it to 50 gives the median
median = np.percentile(arr, 50)
# Print the median
print(median)
In the above code, the median value of the input array arr is computed using the np.percentile() function.
- This function takes two arguments: the array to compute the percentile on, and the percentile value to compute. Setting the second argument to 50 computes the median value. Finally, the median value is printed to the console using print().
Output: 13.0
Method-6: Python numpy median using numpy.argpartition()
This method uses numpy.argpartition() to partition the array around its median and get the indices of the partitioned elements. Then it calculates the median using those indices and the values in the array.
import numpy as np
# Create a NumPy array
arr = np.array([21, 22, 23, 24, 25])
# Partition the array around its median using np.argpartition()
# This returns the indices that would partition the array
# The second argument to np.argpartition() specifies the index of the partition point
partitioned_indices = np.argpartition(arr, len(arr) // 2)
# Get the length of the partitioned indices
n = len(partitioned_indices)
# Calculate the median of the partitioned array
# If the length is odd, the median is the middle value
# If the length is even, the median is the average of the two middle values
median = arr[partitioned_indices[n // 2]] if n % 2 != 0 else (arr[partitioned_indices[n // 2 - 1]] + arr[partitioned_indices[n // 2]]) / 2
# Print the median
print(median)
In the above code, the input array arr is partitioned around its median using np.argpartition().
- This function returns the indices that would partition the array, so we use these indices to calculate the median value of the partitioned array.
- If the length of the partitioned array is odd, the median is simply the middle value. If the length of the partitioned array is even, the median is the average of the two middle values. Finally, the median value is printed to the console using print().
Output: 23
Python numpy median example
- In this section, we will discuss how to use the numpy.median() function in Python.
- In Python, the numpy median is used to generate the median value in the NumPy array and this function involves many parameters namely axis.keepdims and it is also used for specifying the data type that a user needs to be operand on.
- In this example, we will use the axis and keepdims parameter to check how to get the median value of the numpy array.
Example:
Let’s take an example and check how to use the numpy.median() function in Python
Source Code:
import numpy as np
new_arr=np.array([[67,89,113,145,167],
[14,16,18,20,24],
[33,45,67,89,91]])
new_output= np.median(new_arr,axis=0)
new_output2= np.median(new_arr,axis=1,)
print("Axis row-wise median:",new_output)
print("Axis column-wise median",new_output2)
In the above program, we have used the axis parameter in numpy.median() function and it will calculate the row and column medians.
Here is the execution of the following given code
Also, take a look at some more Python NumPy tutorials.
- Python NumPy to list with examples
- Python NumPy square with examples
- Python NumPy where with examples
- Numpy Divide in Python
In this Python NumPy tutorial, we have learned how to get median of a NumPy array in Python.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.
The Numpy library in Python comes with a number of useful built-in functions for computing common descriptive statistics like mean, median, standard deviation, etc. In this tutorial, we will look at how to get the median value in a Numpy array with the help of some examples.
How do you get the median of an array in Numpy?
You can use the Numpy median()
function to get the median value of a Numpy array. Pass the array as an argument.
The following is the syntax –
# median of all values in array numpy.median(ar)
It returns the median of the values in the array. For multi-dimensional arrays, you can specify the axis along which you want to compute the median (see the examples below).
Let’s now look at some examples of using the above syntax on single and multi-dimensional arrays.
Example 1 – Median of a one-dimensional Numpy array
Let’s first create a one-dimensional Numpy array.
import numpy as np # create numpy array ar = np.array([1, 3, 4, 5, 7]) # display the array print(ar)
Output:
[1 3 4 5 7]
Here, we used the numpy.array()
function to create a one-dimensional array containing some numeric values.
Let’s now get the median value in the above array.
# median of array print(np.median(ar))
Output:
4.0
We get the median as 4.0 since 4 is the middle value in the above array. Note that the array need not be sorted for using the numpy.median()
function. The function will do that internally when estimating the middle value.
Example 2 – Median of multi-dimensional Numpy array
First, let’s create a 2-D Numpy array.
# create 2-D numpy array ar = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # display the array print(ar)
Output:
[[1 2 3] [4 5 6] [7 8 9]]
Here, we used the numpy.array()
function to create an array with three rows and three columns.
If you use the Numpy median()
function on an array without specifying the axis, it will return the median taking into consideration all the values inside the array.
# median of array print(np.median(ar))
Output:
5.0
We get the median of all the values inside the 2-D array as 5.0 (which is the middle value if you line up all the values in the above 2-D array in sorted order).
Use the numpy.median()
function with axis=1
to get the median value for each row in the array.
# median of each row in array print(np.median(ar, axis=1))
Output:
[2. 5. 8.]
We get the median of each row in the above 2-D array. The median of values – in the first row (1, 2, 3) is 2, in the second row (4, 5, 6) is 5, and in the third row (7, 8, 9) is 8.
Use the numpy.median()
function with axis=0
to get the median of each column in the array.
# median of each column in array print(np.median(ar, axis=0))
Output:
[4. 5. 6.]
We get the median of each column in the above 2-D array. The median of values – in the first column (1, 4, 7) is 4, in the second column (2, 5, 8) is 5, and in the third column (3, 6, 9) is 6.
Summary
In this tutorial, we looked at how to use the numpy.median()
function to get the median of values in an array. The following are the key takeaways from this tutorial.
- Use the
numpy.median()
function without any arguments to get the median of all the values inside the array. - For multi-dimensional arrays, use the
axis
parameter to specify the axis along which to compute the median. For example, for a 2-D array –- Pass
axis=1
to get the median of each row. - Pass
axis=0
to get the median of each column.
- Pass
You might also be interested in –
- Numpy – Get Max Value in Array
- Python – Get median of a List
- Python – Find Average of values in a List
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
-
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.
View all posts