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


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


先決條件:統計函數簡介

在數據分析和統計方麵,Python是一種非常流行的語言。幸運的是,Python3提供了statistics模塊,它具有非常有用的函數,例如mean()median()mode()等等

mean()函數可用於計算給定數字列表的均值/平均值。它返回作為參數傳遞的數據集的平均值。


算術平均值是數據的總和除以data-points的數量。它是一組範圍內變化的值中數據中心位置的度量。在Python中,我們通常通過將給定數字的總和除以當前數字進行計數。

Given set of numbers:[n1, n2, n3, n5, n6]

Sum of data-set = (n1 + n2 + n3 + n4 + n5)
Number of data produced = 5

Average or arithmetic mean  = (n1 + n2 + n3 + n4 + n5) / 5

用法:mean([data-set])

Parameters:
[data-set]:一組數字的列表或元組。

Returns:提供的data-set的樣本算術平均值。

Exceptions
TypeError當數值以外的任何其他參數作為參數傳遞時。


代碼1:工作中

# Python program to demonstrate mean() 
# function from the statistics module 
  
# Importing the statistics module 
import statistics 
  
# list of positive integer numbers 
data1 = [1, 3, 4, 5, 7, 9, 2] 
  
x = statistics.mean(data1) 
  
# Printing the mean 
print("Mean is:", x)

輸出:

 Mean is:4.428571428571429


代碼2:工作中

# Python program to demonstrate mean() 
# function from the statistics module 
  
# Importing the statistics module 
from statistics import mean 
  
# Importing fractions module as fr 
# Enables to calculate mean of a  
# set in Fraction  
from fractions import Fraction as fr 
  
  
# tuple of positive integer numbers 
data1 = (11, 3, 4, 5, 7, 9, 2) 
  
# tuple of a negative set of integers 
data2 = (-1, -2, -4, -7, -12, -19) 
  
# tuple of mixed range of numbers 
data3 = (-1, -13, -6, 4, 5, 19, 9) 
  
# tuple of a set of fractional numbers 
data4 = (fr(1, 2), fr(44, 12), fr(10, 3), fr(2, 3)) 
  
# dictionary of a set of values 
# Only the keys are taken in 
# consideration by mean() 
data5 = {1:"one", 2:"two", 3:"three"} 
  
  
# Printing the mean of above datsets 
print("Mean of data set 1 is % s" % (mean(data1))) 
print("Mean of data set 2 is % s" % (mean(data2))) 
print("Mean of data set 3 is % s" % (mean(data3))) 
print("Mean of data set 4 is % s" % (mean(data4))) 
print("Mean of data set 5 is % s" % (mean(data5)))

輸出:

Mean of data set 1 is 5.857142857142857
Mean of data set 2 is -7.5
Mean of data set 3 is 2.4285714285714284
Mean of data set 4 is 49/24
Mean of data set 5 is 2


代碼3:TypeError

# Python3 code to demonstrate TypeError 
  
# importing statistics module 
from statistics import mean 
  
# While using dictionaries, only keys are 
# taken into consideration by mean() 
dic = {"one":1, "three":3, "seven":7, 
       "twenty":20, "nine":9, "six":6} 
  
# Will raise TypeError 
print(mean(dic))

輸出:

Traceback (most recent call last):
  File "/home/9f8a941703745a24ddce5b5f6f211e6f.py", line 29, in 
    print(mean(dic))
  File "/usr/lib/python3.5/statistics.py", line 331, in mean
    T, total, count = _sum(data)
  File "/usr/lib/python3.5/statistics.py", line 161, in _sum
    for n, d in map(_exact_ratio, values):
  File "/usr/lib/python3.5/statistics.py", line 247, in _exact_ratio
    raise TypeError(msg.format(type(x).__name__))
TypeError:can't convert type 'str' to numerator/denominator


應用範圍:
平均值/算術平均值是非常重要的函數之一,同時可以處理統計數據和大數值。因此,借助mean()之類的函數,可以從大型數據集中提取趨勢和特征值。



相關用法


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