Posted in Python onSeptember 06, 2019
本文实例讲述了Python统计分析模块statistics用法。分享给大家供大家参考,具体如下:
一 计算平均数函数mean()
>>>import statistics >>> statistics.mean([1,2,3,4,5,6,7,8,9])#使用整数列表做参数 5 >>> statistics.mean(range(1,10))#使用range对象做参数 5 >>>import fractions >>> x =[(3,7),(1,21),(5,3),(1,3)] >>> y =[fractions.Fraction(*item)for item in x] >>> y [Fraction(3,7),Fraction(1,21),Fraction(5,3),Fraction(1,3)] >>> statistics.mean(y)#使用包含分数的列表做参数 Fraction(13,21) >>>import decimal >>> x =('0.5','0.75','0.625','0.375') >>> y = map(decimal.Decimal, x) >>> statistics.mean(y) Decimal('0.5625')
二 中位数函数median()、median_low()、median_high()、median_grouped()
>>> statistics.median([1,3,5,7])#偶数个样本时取中间两个数的平均数 4.0 >>> statistics.median_low([1,3,5,7])#偶数个样本时取中间两个数的较小者 3 >>> statistics.median_high([1,3,5,7])#偶数个样本时取中间两个数的较大者 5 >>> statistics.median(range(1,10)) 5 >>> statistics.median_low([5,3,7]), statistics.median_high([5,3,7]) (5,5) >>> statistics.median_grouped([5,3,7]) 5.0 >>> statistics.median_grouped([52,52,53,54]) 52.5 >>> statistics.median_grouped([1,3,3,5,7]) 3.25 >>> statistics.median_grouped([1,2,2,3,4,4,4,4,4,5]) 3.7 >>> statistics.median_grouped([1,2,2,3,4,4,4,4,4,5], interval=2) 3.4
三 返回最常见数据或出现次数最多的数据(most common data)的函数mode()
>>> statistics.mode([1,3,5,7])#无法确定出现次数最多的唯一元素 Traceback(most recent call last): File"<pyshell#27>", line 1,in<module> statistics.mode([1,3,5,7])#无法确定出现次数最多的唯一元素 File"D:\Python36\lib\statistics.py", line 507,in mode 'no unique mode; found %d equally common values'% len(table) statistics.StatisticsError: no unique mode; found 4 equally common values >>> statistics.mode([1,3,5,7,3]) 3 >>> statistics.mode(["red","blue","blue","red","green","red","red"]) 'red'
四 pstdev(),返回总体标准差(population standard deviation ,the square root of the population variance)
>>> statistics.pstdev([1.5,2.5,2.5,2.75,3.25,4.75]) 0.986893273527251 >>> statistics.pstdev(range(20)) 5.766281297335398
五 pvariance(),返回总体方差(population variance)或二次矩(second moment)
>>> statistics.pvariance([1.5,2.5,2.5,2.75,3.25,4.75]) 0.9739583333333334 >>> x =[1,2,3,4,5,10,9,8,7,6] >>> mu = statistics.mean(x) >>> mu 5.5 >>> statistics.pvariance([1,2,3,4,5,10,9,8,7,6], mu) 8.25 >>> statistics.pvariance(range(20)) 33.25 >>> statistics.pvariance((random.randint(1,10000)for i in range(30))) >>>import random >>> statistics.pvariance((random.randint(1,10000)for i in range(30))) 7117280.4
希望本文所述对大家Python程序设计有所帮助。
Python统计分析模块statistics用法示例
- Author -
cakincqm声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@