python 卡方檢驗
阿新 • • 發佈:2020-10-17
卡方檢驗是一種統計方法,用於確定兩個類別變數之間是否具有顯著相關性。這些變數都應來自相同的人群,並且應該是分類的,例如-是/否,男性/女性,紅色/綠色等。例如,我們可以使用對人們的冰淇淋購買模式的觀察來構建資料集,並嘗試進行關聯具有他們喜歡的冰淇淋口味的人的性別。如果發現相關性,我們可以通過了解來訪者的性別人數來計劃適當的風味儲備。
我們使用numpy庫中的各種函式來進行卡方檢驗。.
fromscipyimportstats
importnumpyasnp
importmatplotlib.pyplotasplt
x = np.linspace(0,10,100)
fig,ax = plt.subplots(1,1)
linestyles = [':','--','-.','-']
deg_of_freedom = [1,4,7,6]
fordf, lsinzip(deg_of_freedom, linestyles):
ax.plot(x, stats.chi2.pdf(x, df), linestyle=ls)
plt.xlim(0,10)
plt.ylim(0,0.4)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Chi-Square Distribution')
plt.legend()
plt.show()
感興趣的小夥伴,可以掃碼新增微信領取課程學習哦