从L1正则化逻辑回归中恢复命名特征

时间:2015-05-05 14:18:32

标签: python machine-learning scikit-learn

我有以下管道:

sg = Pipeline([('tfidf', TfidfVectorizer()),                                                                                                                                            
               ('normalize', Normalizer()),                                                                                                                                                 
               ('l1', LogisticRegression(penalty="l1", dual=False))])

在完成配件后,我想提取出相应的标记 到非零权重。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

features = pipeline.named_steps['tfidf'].get_feature_names()
print(features[pipeline.named_steps['l1'].coef_ != 0])

请参阅TfidfTransformer docsLogisticRegression docsunmerged improved pipeline docs here