9951 explained code solutions for 126 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

.load_digits(

returns digits dataset for classification

.SelectKBest(

select features according to the k highest scores

feature_selection.chi2

use chi2 as feature scoring function

.fit_transform(

train and reduce given dataset to the selected number of features

k=20

we want to have 20 best features for our dataset