目录

Python Chi-square Test

卡方检验是一种统计方法,用于确定两个分类变量之间是否存在显着相关性。 这两个变量都应该来自同一群体,它们应该是分类的 - 是/否,男/女,红/绿等。例如,我们可以建立一个数据集,观察人们的冰淇淋购买模式并尝试关联他们喜欢冰淇淋味道的人的性别。 如果发现相关性,我们可以通过了解访问者的性别数量来计划适当的口味库存。

我们在numpy库中使用各种函数来进行卡方检验。

<p></p>
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
fig,ax = plt.subplots(1,1)
linestyles = [':', '--', '-.', '-']
deg_of_freedom = [1, 4, 7, 6]
for df, ls in zip(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()

output如下 -

chisquare.png
↑回到顶部↑
WIKI教程 @2018