sklearn: get feature names after L1-based feature selection

前端 未结 2 1352
执念已碎
执念已碎 2021-02-10 08:15

This question and answer demonstrate that when feature selection is performed using one of scikit-learn\'s dedicated feature selection routines, then the names of the selected f

2条回答
  •  灰色年华
    2021-02-10 08:51

    For sparse estimators you can generally find the support by checking where the non-zero entries are in the coefficients vector (provided the coefficients vector exists, which is the case for e.g. linear models)

    support = np.flatnonzero(estimator.coef_)
    

    For your LinearSVC with l1 penalty it would accordingly be

    from sklearn.svm import LinearSVC
    svc = LinearSVC(C=1., penalty='l1', dual=False)
    svc.fit(X, y)
    selected_feature_names = np.asarray(vectorizer.get_feature_names())[np.flatnonzero(svc.coef_)]
    

提交回复
热议问题