python 双类别型变量关联性分析-卡方检验chi2()

文章展示了如何利用Python的sklearn库中的SelectKBest和chi2方法,对Iris数据集进行卡方检验,以筛选与目标变量相关的特征。通过计算卡方值和P值来评估特征的重要性,并选取了两个最佳特征。
摘要由CSDN通过智能技术生成

卡方检验:主要用于两个和两个以上样本率(构成比)及两个二值型离散变量的关联性分析,即比较理论频次与实际频次的吻合程度或拟合程度。

以iris数据集为例,在sklearn库中使用卡方检验筛选与目标变量相关的特征:

from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest
# 导入卡方检验
from sklearn.feature_selection import chi2

iris = load_iris()
X, y = iris.data, iris.target
print("X,y行列数: ", X.shape, y.shape)

ChiValues = chi2(X, y)
# 计算X单个特征变量对目标变量y的卡方值和P值
print("计算X单个特征变量对目标变量y的卡方值和P值: ", ChiValues)

'''
    sklearn.feature_selection.SelectKBest中基于卡方chi2,提取出来2个比较好的特征变量,
    可以理解为在所有特征变量里面相对更好的特征,
    但是并不是统计里面分类变量与目标变量通过卡方检验得出的是否相关的结果
'''
sk = SelectKBest(chi2, k=2)
X_new = sk.fit_transform(X, y)
print(sk.scores_)  # X单个特征变量对目标变量y的卡方值
print(sk.pvalues_)  # p值
# 筛选出来的两个特征
print("筛选出来的两个特征: ", X_new)

 
输出结果:

X,y行列数:  (150, 4) (150,)
计算X单个特征变量对目标变量y的卡方值和P值:  (array([ 10.81782088,   3.7107283 , 116.31261309,  67.0483602 ]), array([4.47651499e-03, 1.56395980e-01, 5.53397228e-26, 2.75824965e-15]))
[ 10.81782088   3.7107283  116.31261309  67.0483602 ]
[4.47651499e-03 1.56395980e-01 5.53397228e-26 2.75824965e-15]
筛选出来的两个特征:  [[1.4 0.2]
 [1.4 0.2]
 [1.3 0.2]
 [1.5 0.2]
 [1.4 0.2]
 [1.7 0.4]
 [1.4 0.3]
 [1.5 0.2]
 [1.4 0.2]
 [1.5 0.1]
 [1.5 0.2]
 [1.6 0.2]
 [1.4 0.1]
 [1.1 0.1]
 [1.2 0.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.8]
 [6.1 2.5]
 [5.1 2. ]
 [5.3 1.9]
 [5.5 2.1]
 [5.  2. ]
 [5.1 2.4]
 [5.3 2.3]
 [5.5 1.8]
 [6.7 2.2]
 [6.9 2.3]
 [5.  1.5]
 [5.7 2.3]
 [4.9 2. ]
 [6.7 2. ]
 [4.9 1.8]
 [5.7 2.1]
 [6.  1.8]
 [4.8 1.8]
 [4.9 1.8]
 [5.6 2.1]
 [5.8 1.6]
 [6.1 1.9]
 [6.4 2. ]
 [5.6 2.2]
 [5.1 1.5]
 [5.6 1.4]
 [6.1 2.3]
 [5.6 2.4]
 [5.5 1.8]
 [4.8 1.8]
 [5.4 2.1]
 [5.6 2.4]
 [5.1 2.3]
 [5.1 1.9]
 [5.9 2.3]
 [5.7 2.5]
 [5.2 2.3]
 [5.  1.9]
 [5.2 2. ]
 [5.4 2.3]
 [5.1 1.8]]

Process finished with exit code 0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值