amiref
amiref

Reputation: 3431

Feature selection using Chi2 generates nan

I want to do Chi2 analysis for feature evaluation on my dataset, but the results includes nan values. Why are nan values appeared in the results and how I can resolve the problem? For instance, in the following example, what is the importance score of the second feature?

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_

Output:

(array([0.33333333,        nan, 0.        , 0.00237983, 1.33333334]),
 array([0.56370286,        nan, 1.        , 0.96109184, 0.24821308]))

Upvotes: 1

Views: 595

Answers (1)

Odz
Odz

Reputation: 211

you only have zero values in that specific feature, thus one of the Expected values in the contingency table for that feature will have E=0, and since chi2 formula has the ff:

(O-E)^2 / E

then if E=0, it produces NAN values.

Upvotes: 2

Related Questions