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

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

Python統(tǒng)計(jì)分析模塊statistics用法示例

系統(tǒng) 2024 0

本文實(shí)例講述了Python統(tǒng)計(jì)分析模塊statistics用法。分享給大家供大家參考,具體如下:

一 計(jì)算平均數(shù)函數(shù)mean()

            
>>>import statistics
>>> statistics.mean([1,2,3,4,5,6,7,8,9])#使用整數(shù)列表做參數(shù)
5
>>> statistics.mean(range(1,10))#使用range對(duì)象做參數(shù)
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)#使用包含分?jǐn)?shù)的列表做參數(shù)
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')


          

二 中位數(shù)函數(shù)median()、median_low()、median_high()、median_grouped()

            
>>> statistics.median([1,3,5,7])#偶數(shù)個(gè)樣本時(shí)取中間兩個(gè)數(shù)的平均數(shù)
4.0
>>> statistics.median_low([1,3,5,7])#偶數(shù)個(gè)樣本時(shí)取中間兩個(gè)數(shù)的較小者
3
>>> statistics.median_high([1,3,5,7])#偶數(shù)個(gè)樣本時(shí)取中間兩個(gè)數(shù)的較大者
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


          

三 返回最常見(jiàn)數(shù)據(jù)或出現(xiàn)次數(shù)最多的數(shù)據(jù)(most common data)的函數(shù)mode()

            
>>> statistics.mode([1,3,5,7])#無(wú)法確定出現(xiàn)次數(shù)最多的唯一元素
Traceback(most recent call last):
File"
            
              ", line 1,in
              
                
statistics.mode([1,3,5,7])#無(wú)法確定出現(xiàn)次數(shù)最多的唯一元素
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(),返回總體標(biāo)準(zhǔn)差(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


          

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 湖州市| 财经| 南平市| 东阳市| 江都市| 宁明县| 德昌县| 博兴县| 宜宾县| 长沙县| 九寨沟县| 利川市| 南漳县| 全椒县| 迁安市| 达孜县| 资溪县| 神池县| 南郑县| 郑州市| 澄江县| 望奎县| 扶绥县| 宕昌县| 合肥市| 康马县| 余姚市| 京山县| 禹州市| 延长县| 甘谷县| 南和县| 沙河市| 安泽县| 抚顺县| 河津市| 竹北市| 临洮县| 蚌埠市| 大荔县| 濉溪县|