3000 explained code solutions for 75 technologies

python-scikit-learnFeature selection using KBest and chi2

from sklearn import datasets, feature_selection

X, y = datasets.load_digits(return_X_y=True)

Xs = feature_selection.SelectKBest(feature_selection.chi2, k=20).fit_transform(X, y)ctrl + c
from sklearn import

import module from scikit-learn


returns digits dataset for classification


select features according to the k highest scores


use chi2 as feature scoring function


train and reduce given dataset to the selected number of features


we want to have 20 best features for our dataset