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
三 返回最常見數(shù)據(jù)或出現(xiàn)次數(shù)最多的數(shù)據(jù)(most common data)的函數(shù)mode()
>>> statistics.mode([1,3,5,7])#無法確定出現(xiàn)次數(shù)最多的唯一元素 Traceback(most recent call last): File"<pyshell#27>", line 1,in<module> statistics.mode([1,3,5,7])#無法確定出現(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)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Qt Quick QML-500行代碼實(shí)現(xiàn)合成大西瓜游戲
合成大西瓜游戲是前段時(shí)間比較火的小游戲,最近小編閑來無事,通過研究小球碰撞原理親自寫碰撞算法實(shí)現(xiàn)一個(gè)合成大西瓜游戲,下面小編把我的實(shí)現(xiàn)思路及核心代碼分析出來,供大家參考2021-05-05
Appium+python自動(dòng)化之連接模擬器并啟動(dòng)淘寶APP(超詳解)
這篇文章主要介紹了Appium+python自動(dòng)化之 連接模擬器并啟動(dòng)淘寶APP(超詳解)本文以淘寶app為例,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-06-06
python3 實(shí)現(xiàn)的對(duì)象與json相互轉(zhuǎn)換操作示例
這篇文章主要介紹了python3 實(shí)現(xiàn)的對(duì)象與json相互轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了Python3使用json模塊針對(duì)json格式數(shù)據(jù)轉(zhuǎn)換操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
python list count統(tǒng)計(jì)個(gè)數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了python list count統(tǒng)計(jì)個(gè)數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
python學(xué)習(xí)筆記--將python源文件打包成exe文件(pyinstaller)
這篇文章主要介紹了通過將pyinstallerpython源文件打包成exe文件的方法,需要的朋友可以參考下2018-05-05
使用Python實(shí)現(xiàn)U盤數(shù)據(jù)自動(dòng)拷貝
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)U盤數(shù)據(jù)自動(dòng)拷貝,即當(dāng)電腦上有U盤插入時(shí)自動(dòng)復(fù)制U盤內(nèi)的所有內(nèi)容,希望對(duì)大家有所幫助2025-02-02
基于Python實(shí)現(xiàn)PDF動(dòng)畫翻頁(yè)效果的閱讀器
在這篇博客中,我們將深入分析一個(gè)基于 wxPython 實(shí)現(xiàn)的 PDF 閱讀器程序,該程序支持加載 PDF 文件并顯示頁(yè)面內(nèi)容,同時(shí)支持頁(yè)面切換動(dòng)畫效果,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2025-01-01

