Python 3.4 has statistics.median:
Return the median (middle value) of numeric data.
When the number of data points is odd, return the middle data point. When the number of data points is even, the median is interpolated by taking the average of the two middle values:
>>> median([1, 3, 5]) 3 >>> median([1, 3, 5, 7]) 4.0
Usage:
import statistics
items = [6, 1, 8, 2, 3]
statistics.median(items)
#>>> 3
It's pretty careful with types, too:
statistics.median(map(float, items))
#>>> 3.0
from decimal import Decimal
statistics.median(map(Decimal, items))
#>>> Decimal('3')
Answer from Veedrac on Stack Overflow Top answer 1 of 16
352
Python 3.4 has statistics.median:
Return the median (middle value) of numeric data.
When the number of data points is odd, return the middle data point. When the number of data points is even, the median is interpolated by taking the average of the two middle values:
>>> median([1, 3, 5]) 3 >>> median([1, 3, 5, 7]) 4.0
Usage:
import statistics
items = [6, 1, 8, 2, 3]
statistics.median(items)
#>>> 3
It's pretty careful with types, too:
statistics.median(map(float, items))
#>>> 3.0
from decimal import Decimal
statistics.median(map(Decimal, items))
#>>> Decimal('3')
2 of 16
201
(Works with python-2.x):
def median(lst):
n = len(lst)
s = sorted(lst)
return (s[n//2-1]/2.0+s[n//2]/2.0, s[n//2])[n % 2] if n else None
>>> median([-5, -5, -3, -4, 0, -1])
-3.5
numpy.median():
>>> from numpy import median
>>> median([1, -4, -1, -1, 1, -3])
-1.0
For python-3.x, use statistics.median:
>>> from statistics import median
>>> median([5, 2, 3, 8, 9, -2])
4.0
NumPy
numpy.org › doc › stable › reference › generated › numpy.median.html
numpy.median — NumPy v2.4 Manual
If out is specified, that array is returned instead. ... Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.
Videos
03:50
NumPy Median Tutorial: Master np.median() for Array Statistics ...
06:43
Python for Beginners: Easily Find the Median in a List! - YouTube
Median of Two Sorted Arrays | Решение на Python | LeetCode 4
03:53
How to Calculate the Median in Python from Scratch (Without any ...
13:22
Master Median of Medians Algorithm in Python | Step-by-Step Tutorial.
03:29
Get Median of Array with np.median Function of NumPy Library in ...
NumPy
numpy.org › devdocs › reference › generated › numpy.median.html
numpy.median — NumPy v2.5.dev0 Manual
If out is specified, that array is returned instead. ... Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .median()
Python:NumPy | Built-in Functions | .median() | Codecademy
May 1, 2024 - Returns the median of the elements in a given array.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.median.html
numpy.median — NumPy v2.1 Manual
Returns the median of the array elements.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.median.html
numpy.median — NumPy v2.3 Manual
If out is specified, that array is returned instead. ... Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.
Programiz
programiz.com › python-programming › numpy › methods › median
NumPy median()
The numpy.median() method returns the median of the array.
GeeksforGeeks
geeksforgeeks.org › numpy-median-in-python
numpy.median() in Python - GeeksforGeeks
November 28, 2018 - numpy.median(arr, axis = None) : Compute the median of the given data (array elements) along the specified axis.
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.ndimage.median.html
median — SciPy v1.17.0 Manual
Calculate the median of the values of an array over labeled regions.
Educative
educative.io › answers › how-to-use-the-numpymedian-function-in-python
How to use the NumPy.median() function in Python
The function numpy. median() returns the median of a NumPy array.
W3Schools
w3schools.com › python › ref_stat_median.asp
Python statistics.median() Method
Tip: The mathematical formula for Median is: Median = {(n + 1) / 2}th value, where n is the number of values in a set of data. In order to calculate the median, the data must first be sorted in ascending order.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.median.html
numpy.median — NumPy v2.0 Manual
If out is specified, that array is returned instead. ... Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ma.median.html
numpy.ma.median — NumPy v2.2 Manual
A new array holding the result ... input data-type, otherwise. ... Given a vector V with N non masked values, the median of V is the middle value of a sorted copy of V (Vs) - i.e....