【发布时间】:2019-01-12 17:07:10
【问题描述】:
我正在学习 chi2 进行特征选择,遇到了 this 之类的代码
但是,我对 chi2 的理解是,较高的分数意味着该特征更多独立(因此对模型的用处较小),因此我们会对分数最低的特征感兴趣。然而,使用 scikit 学习 SelectKBest,选择器返回具有 最高 chi2 分数的值。我对使用 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
【问题讨论】:
标签: python machine-learning scikit-learn feature-selection chi-squared