【问题标题】:Difference between imblearn pipeline and Pipelineimblearn pipeline 和 Pipeline 的区别
【发布时间】:2021-07-14 23:53:01
【问题描述】:

我想使用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 版本的原因。为什么不想使用它?
  • @BenReiniger 因为我不能在imblearn 管道中包含SimpleImputerMaxAbsScaler。我不确定我错过了什么!
  • 应该没有问题,包括那些;我建议发布一个关于此的新问题。

标签: python machine-learning scikit-learn pipeline imbalanced-data


【解决方案1】:

我们应该从imblearn.pipeline而不是sklearn.pipeline导入make_pipeline:来自sklearn的make_pipeline需要转换器实现fittransform方法。 sklearn.pipeline import Pipeline 与 imblearn.pipeline import Pipeline 冲突!

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 2020-11-17
    • 1970-01-01
    • 2018-12-21
    • 2017-03-14
    • 2017-04-04
    • 2017-11-23
    • 2019-07-31
    • 2019-12-22
    相关资源
    最近更新 更多