>>importstatistics>>>statistics.mean([1,2,3,4,5,6,7,8,9])#使用整數列表做參數5>>>statistics.mean(range(1,10))#使用range對象做參數5>>>importfractions>>>x=[(3,7),(1,21),(5,3),(1,3)]>>>y=" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python統計分析模塊statistics用法示例

系統 1987 0

本文實例講述了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"
            
              ", line 1,in
              
                
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數學運算技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 通海县| 来凤县| 岫岩| 玉田县| 郴州市| 屏东县| 彰武县| 宜都市| 兴国县| 仪陇县| 麻江县| 郯城县| 进贤县| 安国市| 礼泉县| 安陆市| 景东| 新乡市| 涿鹿县| 乐都县| 吴江市| 江津市| 定兴县| 邢台市| 志丹县| 晋中市| 浙江省| 资兴市| 上高县| 茂名市| 渝中区| 斗六市| 廊坊市| 古蔺县| 营山县| 鄂托克旗| 通化县| 酉阳| 张家界市| 巴南区| 彩票|