中位數通常被稱為中心位置的可靠度量,並且較少受數據中異常值的影響。
統計Python中的模塊允許使用三個選項來處理數據集中的中間/中間元素,分別是median(), median_low() and median_high()
。
低中位數始終是數據集的成員。當數據點的數量為奇數時,將返回中間值。當它為偶數時,將返回兩個中間值中較小的一個。讓我們看看如何median_low()
作品。
用法: median_low( [data-set] )
Parameters:
[data-set]:采用列表,元組或一組可交互的數值數據。
Returntype:
返回數值數據的低位數。中位數低是實際data-set的成員。
Exceptions:
StatisticsError當data-set為空時引發。
代碼1:工作中
# Python code to demonstrate the
# working of median_low()
# importing the statistics module
import statistics
# simple list of a set of integers
set1 = [1, 3, 3, 4, 5, 7]
# Note:low median will always be
# a member of the data-set.
# Print low median of the data-set
print("Low median of the data-set is % s "
% (statistics.median_low(set1)))
輸出:
Low median of the data-set is 3
代碼2:median_low()和中位數的工作方式以區分它們。
# Python code to demonstrate the
# working of median_low()
# importing the statistics module
import statistics
# simple list of a set of integers
set1 = [1, 3, 3, 4, 5, 7]
# Print median of the data-set
# Median value may or may not
# lie within the data-set
print("Median of the set is % s"
% (statistics.median(set1)))
# Print low median of the data-set
print("Low Median of the set is % s "
% (statistics.median_low(set1)))
輸出:
Median of the set is 3.5 Low Median of the set is 3
代碼3:median_low()在不同範圍的data-set上的工作
# Python code to demonstrate the
# working of median_low()
# importing statistics module
from statistics import median_low
# Importing fractions module as fr
from fractions import Fraction as fr
# tuple of positive integer numbers
data1 = (2, 3, 4, 5, 7, 9, 11)
# tuple of a set of floating point values
data2 = (2.4, 5.1, 6.7, 8.9)
# tuple of a set of fractional numbers
data3 = (fr(1, 2), fr(44, 12),
fr(10, 3), fr(2, 3))
# tuple of a set of negative integers
data4 = (-5, -1, -12, -19, -3)
# tuple of set of positive
# and negative integers
data5 = (-1, -2, -3, -4, 4, 3, 2, 1)
# Print low_median() of given data-sets
print("Low Median of data-set 1 is % s" % (median_low(data1)))
print("Low Median of data-set 2 is % s" % (median_low(data2)))
print("Low Median of data-set 3 is % s" % (median_low(data3)))
print("Low Median of data-set 4 is % s" % (median_low(data4)))
print("Low Median of data-set 5 is % s" % (median_low(data5)))
輸出:
Low Median of data-set 1 is 5 Low Median of data-set 2 is 5.1 Low Median of data-set 3 is 2/3 Low Median of data-set 4 is -5 Low Median of data-set 5 is -1
代碼4:提高StatisticsError
# Python code to demonstrate
# StatisticsError of median_low()
# importing the statistics module
from statistics import median_low
# creating an empty data-set
empty = []
# will raise StatisticsError
print(median_low(empty))
輸出:
Traceback (most recent call last): File "/home/5f3e758236f872d014f9d741743c30a4.py", line 10, in print(median_low(empty)) File "/usr/lib/python3.5/statistics.py", line 376, in median_low raise StatisticsError("no median for empty data") statistics.StatisticsError:no median for empty data
應用範圍:
median_low()當數據是離散的並且使用中位數作為數據中的實際點而不是外推點時,將使用。
相關用法
- Python statistics median_high()用法及代碼示例
- Python statistics mean()用法及代碼示例
- Python statistics harmonic_mean()用法及代碼示例
- Python statistics variance()用法及代碼示例
- Python statistics pvariance()用法及代碼示例
- Python - statistics stdev()用法及代碼示例
- Python statistics median()用法及代碼示例
- Python statistics median_grouped()用法及代碼示例
注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python statistics | median_low()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。