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)])
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