gpt4 book ai didi

python - 在 Sklearn Pipeline 中将 VotingClassifier 与其他分类器一起使用

转载 作者:行者123 更新时间:2023-11-30 09:28:41 26 4
gpt4 key购买 nike

我想在 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)

最佳答案

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

1) 使用 TfidfVectorizer 将文本数据转换为 tfidf2)将转换后的数据发送到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())]))
])

关于python - 在 Sklearn Pipeline 中将 VotingClassifier 与其他分类器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com