Python数据处理
Python数据可视化
Python统计分析

Python卡方检验

Python卡方检验详细操作教程
卡方检验是确定两个分类变量是否具有显着相关性的统计方法。 这两个变量应该来自相同的人口,他们应该是类似的 - 是/否,男/女,红/绿等。例如,我们可以建立一个数据集,观察人们的冰淇淋购买模式并尝试关联 - 他们喜欢的冰淇淋味道的人的性别。 如果发现相关性,我们可以通过了解访问人群的性别数量来计划适当的口味。
在numpy库中使用各种功能来执行卡方检验。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
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()
执行上面示例代码,得到以下结果 -
Python卡方检验
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4