Machine learning 使用Chi2生成nan的特征选择
我想对我的数据集进行Chi2分析以进行特征评估,但结果包括Machine learning 使用Chi2生成nan的特征选择,machine-learning,scikit-learn,feature-selection,Machine Learning,Scikit Learn,Feature Selection,我想对我的数据集进行Chi2分析以进行特征评估,但结果包括nan值。为什么结果中会出现nan值?我如何解决该问题?例如,在下面的示例中,第二个特征的重要性分数是多少 from sklearn.feature_selection import chi2 from sklearn.feature_selection import SelectKBest X = np.array([[0. , 0. , 0.968 , 0.57894737, 0.46666667
nan
值。为什么结果中会出现nan值?我如何解决该问题?例如,在下面的示例中,第二个特征的重要性分数是多少
from sklearn.feature_selection import chi2
from sklearn.feature_selection import SelectKBest
X = np.array([[0. , 0. , 0.968 , 0.57894737, 0.46666667],
[0. , 0. , 0.968 , 0.65789474, 0. ],
[0.5 , 0. , 0.968 , 0.65789474, 0.55 ],
[0. , 0. , 0.968 , 0.65789474, 0. ],
[0. , 0. , 0.968 , 0.65789474, 0. ],
[0. , 0. , 0.968 , 0.55263158, 0.56666667],
[0. , 0. , 0.968 , 0.71052632, 0.41666667],
[0. , 0. , 0.968 , 0.42105263, 0. ],
[0. , 0. , 0.968 , 0.42105263, 0. ],
[0. , 0. , 0.968 , 0.55263158, 0. ]])
y = np.array([[1],
[0],
[1],
[1],
[0],
[1],
[1],
[0],
[1],
[0]])
chi2_selector = SelectKBest(score_func=chi2, k=3)
X_kbest = chi2_selector.fit_transform(X, y)
chi2_selector.scores_
输出:
(array([0.33333333, nan, 0. , 0.00237983, 1.33333334]),
array([0.56370286, nan, 1. , 0.96109184, 0.24821308]))
在该特定特征中只有零值,因此该特征列联表中的一个期望值将具有E=0,并且由于chi2公式具有ff: (O-E)^2/E
然后,如果E=0,则产生NAN值。该特定特征中只有零值,因此该特征列联表中的一个期望值将具有E=0,并且由于chi2公式具有ff: (O-E)^2/E 然后,如果E=0,则生成NAN值