Home:ALL Converter>Ranking all features in order using scikit-learn

Ranking all features in order using scikit-learn

Ask Time:2019-03-11T16:50:21         Author:wieus

Json Formatter

I am trying to sort all features in order using scikit-learn f_regression and SelectKBest. The method works well if the number of ranked features k is smaller than the total number of features n. However, if I set k = n then the output from SelectKBest will be in the same order as the original feature array. How can I sort all features in order according to their importance?

The code is below:

from sklearn.feature_selection import SelectKBest, f_regression

n = len(training_features.columns)

selector = SelectKBest(f_regression, k = n)
selector.fit(training_features.values, training_targets.values[:, 0])

k_best_features = list(training_features.columns[selector.get_support(indices = True)])

Author:wieus,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55098122/ranking-all-features-in-order-using-scikit-learn
wieus :

I ended up using this solution:\n\nimport numpy as np\nfrom sklearn.feature_selection import f_regression\n\nk = 10 # number of best features to obtain\n\nscores, _ = f_regression(training_features.values, training_targets.values[:, 0])\nindices = np.argsort(scores)[::-1]\nk_best_features = list(training_features.columns.values[indices[0:k]])\n",
2019-03-13T12:32:01
yy