Py学习  »  Python

python imblearn make_pipeline typeerror:管道的最后一步应实现fit

pythondumb • 4 年前 • 1145 次点击  

我正试图在管道中实现IMB1RY的窒息。我的数据集是存储在熊猫数据框中的文本数据。请参见下面的代码段

text_clf =Pipeline([('vect', TfidfVectorizer()),('scale', StandardScaler(with_mean=False)),('smt', SMOTE(random_state=5)),('clf', LinearSVC(class_weight='balanced'))])

在此之后,我将使用gridsearchcv。

grid = GridSearchCV(text_clf, parameters, cv=4, n_jobs=-1, scoring = 'accuracy') 

其中参数只不过是tfidfvector()的调整参数。 我得到以下错误。

 All intermediate steps should be transformers and implement fit and transform. 'SMOTE

发布这个错误后,我将代码改为如下。

vect = TfidfVectorizer(use_idf=True,smooth_idf = True, max_df = 0.25, sublinear_tf = True, ngram_range=(1,2))
X = vect.fit_transform(X).todense()
Y = vect.fit_transform(Y).todense()
X_Train,X_Test,Y_Train,y_test = train_test_split(X,Y, random_state=0, test_size=0.33, shuffle=True)
text_clf =make_pipeline([('smt', SMOTE(random_state=5)),('scale', StandardScaler(with_mean=False)),('clf', LinearSVC(class_weight='balanced'))])
grid = GridSearchCV(text_clf, parameters, cv=4, n_jobs=-1, scoring = 'accuracy')

在哪里? parameters 只是调整而已 C 在里面 SVC 分类器。 这次我遇到以下错误:

Last step of Pipeline should implement fit.SMOTE(....) doesn't

这是怎么回事?有人能帮忙吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38656
 
1145 次点击  
文章 [ 1 ]  |  最新文章 4 年前
x3mka
Reply   •   1 楼
x3mka    5 年前

imbLearn.smote没有“transform”方法。Docs: https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.over_sampling.SMOTE.html

但除了管道中的最后一个步骤之外,所有步骤都应该有它以及“fit”。

要与sklearn pipeline一起使用smote,您应该在“transform”方法中实现一个自定义转换器调用smote.fit_sample()。

另一个更简单的选择是使用ibmlearn管道:

from imblearn.over_sampling import SMOTE
from imblearn.pipeline import Pipeline as imbPipeline

# This doesn't work with sklearn.pipeline.Pipeline because
# SMOTE doesn't have a .tranform() method.
# (It has .fit_sample() or .sample().)
pipe = imbPipeline([
    ... 
    ('oversample', SMOTE(random_state=5)),
    ('clf', LinearSVC(class_weight='balanced'))
])