我正在学习 chi2 用于特征选择,并遇到了类似的代码this https://chrisalbon.com/machine_learning/feature_selection/chi-squared_for_feature_selection/
然而,我对 chi2 的理解是,分数越高意味着该功能是more独立(因此对模型不太有用),因此我们会对得分最低的特征感兴趣。然而,使用 scikit learns选择KBest http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectKBest.html,选择器返回值highestchi2 分数。我对使用 chi2 测试的理解是否不正确?或者 sklearn 中的 chi2 分数是否会产生 chi2 统计数据以外的其他内容?
请参阅下面的代码了解我的意思(主要是从上面的链接复制的,除了结尾)
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
import pandas as pd
import numpy as np
# Load iris data
iris = load_iris()
# Create features and target
X = iris.data
y = iris.target
# Convert to categorical data by converting data to integers
X = X.astype(int)
# Select two features with highest chi-squared statistics
chi2_selector = SelectKBest(chi2, k=2)
chi2_selector.fit(X, y)
# Look at scores returned from the selector for each feature
chi2_scores = pd.DataFrame(list(zip(iris.feature_names, chi2_selector.scores_, chi2_selector.pvalues_)), columns=['ftr', 'score', 'pval'])
chi2_scores
# you can see that the kbest returned from SelectKBest
#+ were the two features with the _highest_ score
kbest = np.asarray(iris.feature_names)[chi2_selector.get_support()]
kbest
你的理解搞反了。
chi2 检验的原假设是“两个分类变量是独立的”。因此,chi2 统计量的值较高意味着“两个分类变量是相关的”并且对于分类更有用。
SelectKBest 为您提供基于较高 chi2 值的最佳两个 (k=2) 特征。因此,您需要获取它提供的那些功能,而不是获取 chi2 选择器上的“其他功能”。
您从 chi2_selector.scores_ 获取 chi2 统计数据以及从 chi2_selector.get_support() 获取最佳功能是正确的。它会根据独立测试的 chi2 测试为您提供“花瓣长度 (cm)”和“花瓣宽度 (cm)”作为前 2 个特征。希望它能澄清这个算法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)