Python統(tǒng)計(jì)分析模塊statistics用法示例
本文實(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"<pyshell#27>", line 1,in<module> 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ì)有所幫助。
相關(guān)文章
Python實(shí)現(xiàn)批量上傳本地maven庫(kù)到nexus
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)批量上傳本地maven庫(kù)到nexus,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下2024-01-01python執(zhí)行js腳本報(bào)錯(cuò)CryptoJS is not defined問(wèn)題
這篇文章主要介紹了python執(zhí)行js腳本報(bào)錯(cuò)CryptoJS is not defined問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05在Pycharm terminal中字體大小設(shè)置的方法
今天小編就為大家分享一篇在Pycharm terminal中字體大小設(shè)置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01基于注解實(shí)現(xiàn) SpringBoot 接口防刷的方法
這篇文章主要介紹了基于注解實(shí)現(xiàn) SpringBoot 接口防刷的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03使用python telnetlib批量備份交換機(jī)配置的方法
今天小編就為大家分享一篇使用python telnetlib批量備份交換機(jī)配置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07