簡體   English   中英

imblearn pipeline 和 Pipeline 的區別

[英]Difference between imblearn pipeline and Pipeline

我想使用sklearn.pipeline而不是使用imblearn.pipeline來合並“RandomUnderSampler()”。 我的原始數據需要缺失值插補和縮放。 在這里,我有乳腺癌數據作為玩具示例。 但是,它給了我以下錯誤消息。 我很欣賞你的建議。 謝謝你的時間!

from numpy.random import seed
seed(12)
from sklearn.datasets import load_breast_cancer
import time
from sklearn.metrics import make_scorer
from imblearn.metrics import geometric_mean_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MaxAbsScaler
from imblearn.under_sampling import RandomUnderSampler
gmean = make_scorer(geometric_mean_score, greater_is_better=True)

X, y = load_breast_cancer(return_X_y=True)
start_time1 = time.time()
scoring = {'G-mean': gmean}
LR_pipe =  Pipeline([("impute", SimpleImputer(strategy='constant',fill_value= 0)),("scale", MaxAbsScaler()),("rus", RandomUnderSampler()),("LR", LogisticRegression(solver='lbfgs', random_state=0, class_weight='balanced', max_iter=100000))])
LRscores = cross_validate(LR_pipe,X, y, cv=5,scoring=scoring)
end_time1 = time.time()
print ("Computational time in seconds = " +str(end_time1 - start_time1) )
sorted(LRscores.keys())
LR_Gmean = LRscores['test_G-mean'].mean()

print("G-mean: %f" % (LR_Gmean))

錯誤信息:

TypeError: All intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' 'RandomUnderSampler()' (type <class 'imblearn.under_sampling._prototype_selection._random_under_sampler.RandomUnderSampler'>) doesn't

我們應該從imblearn.pipeline而不是從sklearn.pipeline導入make_pipeline :來自make_pipeline的 make_pipeline 需要轉換器來實現fittransform方法。 sklearn.pipeline導入管道與imblearn.pipeline導入管道沖突!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM