sklearn: get feature names after L1-based feature selection

前端 未结 2 1350
执念已碎
执念已碎 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:40

    I've been using sklearn 15.2, and according to LinearSVC documentation , coef_ is an array, shape = [n_features] if n_classes == 2 else [n_classes, n_features]. So first, np.flatnonzero doesn't work for multi-class. You'll have index out of range error. Second, it should be np.where(svc.coef_ != 0)[1] instead of np.where(svc.coef_ != 0)[0] . 0 is index of classes, not features. I ended up with using np.asarray(vectorizer.get_feature_names())[list(set(np.where(svc.coef_ != 0)[1]))]

提交回复
热议问题