【问题标题】:Using VotingClassifier with other classifiers inside a Sklearn Pipeline在 Sklearn 管道中使用 VotingClassifier 和其他分类器
【发布时间】:2018-10-15 08:06:22
【问题描述】:

我想在sklearn Pipeline 中使用VotingClassifier,我在其中定义了一组分类器..

我从这个问题中得到了一些直觉:Using VotingClassifier in Sklearn Pipeline 来构建下面的代码,但是在这个问题中,每个分类器都定义在一个独立的管道中。我不想以这种方式使用它,我事先准备好一组特征,在具有不同分类器的多个管道中重复生成这些特征不是一个好主意(耗时的过程)!

我怎么能做到这一点?!

model = Pipeline([
        ('feat', FeatureUnion([
            ('tfidf', TfidfVectorizer(analyzer='char', ngram_range=(3, 5), min_df=0.01, lowercase=True, tokenizer=tokenizeTfidf)),    
        ])),


        ('pip1', Pipeline([('clf1', GradientBoostingClassifier(n_estimators=1000, random_state=7))])),
        ('pip2', Pipeline([('clf2', SVC())])),
        ('pip3', Pipeline([('clf3', RandomForestClassifier())])),
        ('clf', VotingClassifier(estimators=["pip1", "pip2", "pip3"]))
    ])

clf = model.fit(X_train, y_train)

但我收到了这个错误:

 ('clf', VotingClassifier(estimators=["pip1", "pip2", "pip3"])),
  File "C:\Python35\lib\site-packages\imblearn\pipeline.py", line 115, in __init__
    self._validate_steps()
  File "C:\Python35\lib\site-packages\imblearn\pipeline.py", line 139, in _validate_steps
    "(but not both) '%s' (type %s) doesn't)" % (t, type(t)))
TypeError: All intermediate steps of the chain should be estimators that implement fit and transform or sample (but not both) 'Pipeline(memory=None,
     steps=[('clf1', GradientBoostingClassifier(criterion='friedman_mse', init=None,
              learning_rate=0.1, loss='deviance', max_depth=3,
              max_features=None, max_leaf_nodes=None,
              min_impurity_decrease=0.0, min_impurity_split=None,
              min_samples_leaf=1, min_samples_split=2,
              min_weight_fraction_leaf=0.0, n_estimators=1000,
              presort='auto', random_state=7, subsample=1.0, verbose=0,
              warm_start=False))])' (type <class 'imblearn.pipeline.Pipeline'>) doesn't)

【问题讨论】:

  • 请贴出完整的代码?您要导入哪个管道?
  • 其次,管道是顺序的。它里面的所有类都应该是转换器(除了最后一个,它也可以是一个估计器)。第一个transform() 的输出进入第二个fit() 的输入,第二个transform() 的输出进入第三个fit() 的输入等等......你希望'pip2' 的输入是什么?跨度>
  • 我如何在 Pipeline 中使用投票分类器!?

标签: python machine-learning scikit-learn imblearn


【解决方案1】:

我假设你想做这样的事情:

1) 使用 TfidfVectorizer 将文本数据转换为 tfidf 2) 将转换后的数据发送到 3 个估计器(GradientBoostingClassifier、SVC、RandomForestClassifier),然后使用投票获得预测。

如果是这样,这就是你需要的。

model = Pipeline([
        ('feat', FeatureUnion([
            ('tfidf', TfidfVectorizer(analyzer='char',  
                                      ngram_range=(3, 5), 
                                      min_df=0.01, 
                                      lowercase=True, 
                                      tokenizer=tokenizeTfidf)),    
        ])),
        ('clf', VotingClassifier(estimators=[("pip1", GradientBoostingClassifier(n_estimators=1000, 
                                                                                 random_state=7)), 
                                             ("pip2", SVC()), 
                                             ("pip3", RandomForestClassifier())]))
    ])

另外,如果您只使用单个TfidfVectorizer 而没有结合任何其他功能,您甚至不需要FeatureUnion

model = Pipeline([
        ('tfidf', TfidfVectorizer(analyzer='char',  
                                  ngram_range=(3, 5), 
                                  min_df=0.01, 
                                  lowercase=True, 
                                  tokenizer=tokenizeTfidf)),    
        ('clf', VotingClassifier(estimators=[("pip1", GradientBoostingClassifier(n_estimators=1000, 
                                                                                 random_state=7)), 
                                             ("pip2", SVC()), 
                                             ("pip3", RandomForestClassifier())]))
    ])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多