用Python學線性代數:概率密度函數擬合


問題

如果有一組數據,如何確定他們來自哪個統計分布?

從數據分析的角度,我們並不想要通過嚴格的統計方法去找到這個分布,Python中有一個可以自動擬合數據分析的庫 —— distfit 。這是一個python包,用於通過殘差平方和(RSS)和擬合優度檢驗(GOF)對89個單變量分布進行概率密度擬合,並返回最佳分布。

distfit 簡單又好用

# 安裝
pip install distfit

distfit 常見的用法:

.fit_transform(): 在經驗數據 X 上擬合分布
.summary:獲得擬合數據(內部)並測試擬合優度的所有分布的摘要。
.predict():預測響應變量的概率
.model:輸出分布的最佳參數
.plot(): 繪制最佳的擬合分布

示例

from distfit import distfit
import numpy as np
X = np.random.normal(0, 2, [100,10])
y = [-8,-6,0,1,2,3,4,5,6]
dist = distfit(todf=True)
dist.fit_transform(X)
dist.plot()

輸出用於擬合過程的函數列表,它使用最流行的 10 個發行版的列表進行掃描。

[distfit] >[norm      ] [0.00 sec] [RSS: 0.0120713] [loc=-0.047 scale=1.984]
[distfit] >[expon     ] [0.0 sec] [RSS: 0.4092169] [loc=-7.320 scale=7.273]
[distfit] >[pareto    ] [0.06 sec] [RSS: 0.4098607] [loc=-98251992.546 scale=98251985.226]
[distfit] >[dweibull  ] [0.01 sec] [RSS: 0.0229391] [loc=-0.046 scale=1.721]
[distfit] >[t         ] [0.04 sec] [RSS: 0.0120717] [loc=-0.047 scale=1.984]
[distfit] >[genextreme] [0.05 sec] [RSS: 0.0107445] [loc=-0.755 scale=1.981]
[distfit] >[gamma     ] [0.04 sec] [RSS: 0.0118907] [loc=-191.812 scale=0.021]
[distfit] >[lognorm   ] [0.09 sec] [RSS: 0.0117103] [loc=-107.148 scale=107.084]
[distfit] >[beta      ] [0.03 sec] [RSS: 0.0116300] [loc=-14.479 scale=28.973]
[distfit] >[uniform   ] [0.0 sec] [RSS: 0.2545746] [loc=-7.320 scale=12.835]
[distfit] >[loggamma  ] [0.04 sec] [RSS: 0.0123738] [loc=-360.941 scale=54.518]

當然,distfit 支持的分布還有很多:

 支持的分布
最后繪制最佳的擬合分布

dist.summary 輸出各分布的參數。

         distr      score  ...       scale                                       arg
0   genextreme  0.0107445  ...      1.9814                    (0.27387377680364405,)
1         beta    0.01163  ...     28.9726  (26.055079164665887, 26.248878389403494)
2      lognorm  0.0117103  ...     107.084                    (0.01848064379707136,)
3        gamma  0.0118907  ...   0.0205321                      (9339.777394183824,)
4         norm  0.0120713  ...     1.98429                                        ()
5            t  0.0120717  ...     1.98428                      (1863131.339823592,)
6     loggamma  0.0123738  ...     54.5178                      (750.2645425001494,)
7     dweibull  0.0229391  ...     1.72129                     (1.2898970802235787,)
8      uniform   0.254575  ...     12.8346                                        ()
9        expon   0.409217  ...     7.27316                                        ()
10      pareto   0.409861  ...  9.8252e+07                     (13542571.060345018,)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM