Python is a very popular language when it comes to data analysis and statistics. Luckily, Python3 provide statistics module, which comes with very useful functions like
mean(), median(), mode() etc.
median() function in the statistics module can be used to calculate median value from an unsorted data-list. The biggest advantage of using median() function is that the data-list does not need to be sorted before being sent as parameter to the median() function.
Median is the value that separates the higher half of a data sample or probability distribution from the lower half. For a dataset, it may be thought of as the middle value. The median is the measure of the central tendency of the properties of a data-set in statistics and probability theory. Median has a very big advantage over Mean, which is the median value is not skewed so much by extremely large or small values. The median value is either contained in the data-set of values provided or it doesn’t sway too much from the data provided.
For odd set of elements, the median value is the middle one.
For even set of elements, the median value is the mean of two middle elements.
Median can be represented by the following formula :
Syntax :median( [data-set] )
[data-set] : List or tuple or an iterable with a set of numeric values
Returns :Return the median (middle value) of the iterable containing the data
Exceptions :StatisticsError is raised when iterable passed is empty or when list is null.
Code #1 : Working
Median of data-set is : 3.5
Code #2 :
Median of data-set 1 is 5 Median of data-set 2 is 5.9 Median of data-set 3 is 2 Median of data-set 4 is -5 Median of data-set 5 is 0.0
Code #3 : Demonstrating StatisticsError
Traceback (most recent call last): File "/home/3c98774036f97845ee9f65f6d3571e49.py", line 12, in print(median(empty)) File "/usr/lib/python3.5/statistics.py", line 353, in median raise StatisticsError("no median for empty data") statistics.StatisticsError: no median for empty data
For practical applications, different measures of dispersion and population tendency are compared on the basis of how well the corresponding population values can be estimated. For example, a comparison shows that the sample mean is more statistically efficient than the sample median when the data is uncontaminated by data from heavily-tailed data distribution or from mixtures of data distribution, but less efficient otherwise and that the efficiency of the sample median is higher than that for a wide range of distributions. To be more specific, the median has 64% efficiency compared to minimum-variance-mean ( for large normal samples ).
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- mode() function in Python statistics module
- median_grouped() function in Python statistics module
- median_low() function in Python statistics module
- median_high() function in Python statistics module
- stdev() method in Python statistics module
- Python statistics | mean() function
- Python - Power-Function Distribution in Statistics
- fmean() function in Python statistics
- Numpy MaskedArray.median() function | Python
- Python statistics | pvariance()
- Use Pandas to Calculate Statistics in Python
- Python statistics | variance()
- Python - Moyal Distribution in Statistics
- Python - Maxwell Distribution in Statistics
- Python - Lomax Distribution in Statistics
- Python - Log Normal Distribution in Statistics
- Python - Log Laplace Distribution in Statistics
- Python - Logistic Distribution in Statistics
- Python - Log Gamma Distribution in Statistics
- Python - Levy_stable Distribution in Statistics
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.