python 判斷一組數(shù)據(jù)是否符合正態(tài)分布
正態(tài)分布:
若隨機(jī)變量x服從有個數(shù)學(xué)期望為μ,方差為σ2 的正態(tài)分布,記為N(μ,σ)
其中期望值決定密度函數(shù)的位置,標(biāo)準(zhǔn)差決定分布的幅度,當(dāng)υ=0,σ=0 時的正態(tài)分布是標(biāo)準(zhǔn)正態(tài)分布
判斷方法有畫圖/k-s檢驗
畫圖:
#導(dǎo)入模塊import numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inline#構(gòu)造一組隨機(jī)數(shù)據(jù)s = pd.DataFrame(np.random.randn(1000)+10,columns = [’value’])#畫散點圖和直方圖fig = plt.figure(figsize = (10,6))ax1 = fig.add_subplot(2,1,1) # 創(chuàng)建子圖1ax1.scatter(s.index, s.values)plt.grid()ax2 = fig.add_subplot(2,1,2) # 創(chuàng)建子圖2s.hist(bins=30,alpha = 0.5,ax = ax2)s.plot(kind = ’kde’, secondary_y=True,ax = ax2)plt.grid()
結(jié)果如下:
使用ks檢驗:
#導(dǎo)入scipy模塊from scipy import stats'''kstest方法:KS檢驗,參數(shù)分別是:待檢驗的數(shù)據(jù),檢驗方法(這里設(shè)置成norm正態(tài)分布),均值與標(biāo)準(zhǔn)差結(jié)果返回兩個值:statistic → D值,pvalue → P值p值大于0.05,為正態(tài)分布H0:樣本符合 H1:樣本不符合 如何p>0.05接受H0 ,反之 '''u = s[’value’].mean() # 計算均值std = s[’value’].std() # 計算標(biāo)準(zhǔn)差stats.kstest(s[’value’], ’norm’, (u, std))
結(jié)果是KstestResult(statistic=0.01441344628501079, pvalue=0.9855029319675546),p值大于0.05為正太分布
以上就是python 判斷一組數(shù)據(jù)是否符合正態(tài)分布的詳細(xì)內(nèi)容,更多關(guān)于python 正態(tài)分布的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章: