當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python statistics median_grouped()用法及代碼示例


談到統計函數,data-set的中位數是魯棒集中趨勢的度量,該趨勢受數據中異常值的影響較小。如前所示,使用median(),median_high(),median_low()的未分組data-set的中位數起作用。

Python還提供了計算分組數據和連續數據函數的中位數的選項,這是此健壯且方便的語言的最佳組成部分。統計模塊下的median_grouped()函數有助於從一組連續數據中計算中值。

假定數據被分組為寬度間隔的間隔。數組中的每個數據點都是包含真實值的間隔的中點。假設在該間隔內的真實值均勻分布,則通過在中間間隔(包含中間值的間隔)內進行插值來計算中間值:


median = L + interval * (N / 2 - CF) / FL = lower limit of the median interval
N = total number of data points
CF = number of data points below the median interval
F = number of data points in the median interval
用法: median_grouped( [data-set], interval)

Parameters:
[data-set]:列表或元組,或帶有一組數字的迭代。
interval (默認為1):確定分組數據的寬度和更改的寬度。它還將更改計算出的中位數的插值。

Returntype:返回分組的連續數據的中位數,計算為50th百分百。

Exceptions: StatisticsError當iterable通過為空或list為null時引發。


代碼1:

# Python3 code to demonstrate median_grouped() 
  
# importing median_grouped from 
# the statistics module 
from statistics import median_grouped 
  
# creating an simple data-set 
data1 = [15, 20, 25, 30, 35] 
  
# printing median_grouped for the set 
print("Grouped Median of the median is %s"
                %(median_grouped(data1)))

輸出:

Grouped Median of the median is 25.0


代碼2:median_grouped處理各種變化的數據

# Python code to demonsrate the 
# working of median_grouped() 
  
# importing statistics module 
from statistics import median_grouped 
  
# tuple of a set of positive integers 
set1 = [2, 5, 3, 4, 8, 9] 
  
# tuple of a set of negative integers 
set2 = [-6, -2, -9, -12] 
  
# tuple of a set of positive 
# and negative integers 
set3 = [2, 4, 8, 9, -2, -3, -5, -6] 
  
# Printing grouped median for 
# the given set of data 
print("Grouped Median of set 1 is % s" % (median_grouped(set1))) 
print("Grouped Median of set 2 is % s" % (median_grouped(set2))) 
print("Grouped Median of set 3 is % s" % (median_grouped(set3)))

輸出:

Grouped Median of set 1 is 4.5
Grouped Median of set 2 is -6.5
Grouped Median of set 3 is 1.5


代碼3:間隔工作

# Python code to demonsrate the working of 
# interval in median_grouped() function 
  
# importing statistics module 
from statistics import median_grouped 
  
# creating a tuple of simple data 
set1 = (10, 12, 13, 12, 13, 15) 
  
# Printing median_grouped() 
# keeping default interval at 1 
print("Grouped Median for Interval set as "\ 
      "(default) 1 is % s" %(median_grouped(set1))) 
  
# For interval value of 2 
print("Grouped Median for Interval set as "\ 
      "2 is % s" %(median_grouped(set1, interval = 2))) 
  
# Now for interval value of 5 
print("Grouped Median for Interval set as "\ 
      "5 is % s" %(median_grouped(set1, interval = 5)))

輸出:

Grouped Median for Interval set as (default) 1 is 12.5
Grouped Median for Interval set as 2 is 12.0
Grouped Median for Interval set as 5 is 10.5
Grouped Median for Interval set as 10 is 8.0


注意:觀察到隨著間隔值增加,中間值減小的模式。

代碼4:展示StatisticsError

# Python code to demonstrate StatisticsError 
  
# importing the statistics module 
import statistics 
  
# creating an empty dataset 
list1 = [] 
  
# Will raise StatisticsError 
print(statistics.median_grouped(list1))

輸出:

Traceback (most recent call last):
  File "/home/0990a4a3f5206c7cd12a596cf82a1587.py", line 10, in 
    print(statistics.median_grouped(list1))
  File "/usr/lib/python3.5/statistics.py", line 431, in median_grouped
    raise StatisticsError("no median for empty data")
statistics.StatisticsError:no median for empty data


應用範圍:
分組中位數與中位數具有相同的應用。它通常用於涉及大量數據(如銀行和金融)的計算中。它是統計的重要組成部分,它是數據計算中最強大的工具。



相關用法


注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python statistics | median_grouped()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。