2

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

我从这个问题中得到了一些直觉:Using VotingClassifierin Sklearn Pipeline to build the code below,但是在这个问题中,每个分类器都定义在一个独立的管道中。我不想以这种方式使用它,我有一个之前准备了一组特征,在具有不同分类器的多个管道中重复生成这些特征不是一个好主意(耗时的过程)!

我怎么能做到这一点?!

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

1 回答 1

9

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

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())]))
    ])
于 2018-05-07T03:42:05.383 回答