python - 如何在 scikit 学习管道中实现 RandomUnderSampler?

标签 python scikit-learn imblearn

我有一个 scikit 学习管道来缩放数字特征和编码分类特征。在我尝试实现 RandomUnderSampler 之前,它运行良好。来自 imblearn。我的目标是实现欠采样步骤,因为我的数据集非常不平衡 1:1000。

我确保使用 imblearn 的 Pipeline 方法而不是 sklearn。下面是我试过的代码。

代码数据在没有欠采样器方法的情况下工作(使用 sklearn 管道)。

from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from imblearn.pipeline import make_pipeline as make_pipeline_imb
from imblearn.pipeline import Pipeline as Pipeline_imb

from sklearn.base import BaseEstimator, TransformerMixin
class TypeSelector(BaseEstimator, TransformerMixin):
    def __init__(self, dtype):
        self.dtype = dtype
    def fit(self, X, y=None):
        return self
    def transform(self, X):
        assert isinstance(X, pd.DataFrame)
        return X.select_dtypes(include=[self.dtype])

transformer = Pipeline([
    # Union numeric, categoricals and boolean
    ('features', FeatureUnion(n_jobs=1, transformer_list=[
         # Select bolean features                                                  
        ('boolean', Pipeline([
            ('selector', TypeSelector('bool')),
        ])),
         # Select and scale numericals
        ('numericals', Pipeline([
            ('selector', TypeSelector(np.number)),
            ('scaler', StandardScaler()),
        ])),
         # Select and encode categoricals
        ('categoricals', Pipeline([
            ('selector', TypeSelector('category')),
            ('encoder', OneHotEncoder(handle_unknown='ignore')),
        ])) 
    ])),
])
pipe = Pipeline([('prep', transformer), 
                 ('clf', RandomForestClassifier(n_estimators=500, class_weight='balanced'))
                 ])

使用欠采样器方法不起作用的代码(使用 imblearn 管道)。

from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from imblearn.pipeline import make_pipeline as make_pipeline_imb
from imblearn.pipeline import Pipeline as Pipeline_imb

from sklearn.base import BaseEstimator, TransformerMixin
class TypeSelector(BaseEstimator, TransformerMixin):
    def __init__(self, dtype):
        self.dtype = dtype
    def fit(self, X, y=None):
        return self
    def transform(self, X):
        assert isinstance(X, pd.DataFrame)
        return X.select_dtypes(include=[self.dtype])

transformer = Pipeline_imb([
    # Union numeric, categoricals and boolean
    ('features', FeatureUnion(n_jobs=1, transformer_list=[
         # Select bolean features                                                  
        ('boolean', Pipeline_imb([
            ('selector', TypeSelector('bool')),
        ])),
         # Select and scale numericals
        ('numericals', Pipeline_imb([
            ('selector', TypeSelector(np.number)),
            ('scaler', StandardScaler()),
        ])),
         # Select and encode categoricals
        ('categoricals', Pipeline_imb([
            ('selector', TypeSelector('category')),
            ('encoder', OneHotEncoder(handle_unknown='ignore')),
        ])) 
    ])),  
])
pipe = Pipeline_imb([
                 ('sampler', RandomUnderSampler(0.1)),
                 ('prep', transformer), 
                 ('clf', RandomForestClassifier(n_estimators=500, class_weight='balanced'))
                 ])


这是我得到的错误:

/usr/local/lib/python3.6/dist-packages/sklearn/pipeline.py in __init__(self, steps, memory, verbose)
    133     def __init__(self, steps, memory=None, verbose=False):
    134         self.steps = steps
--> 135         self._validate_steps()
    136         self.memory = memory
    137         self.verbose = verbose

/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in _validate_steps(self)
    144             if isinstance(t, pipeline.Pipeline):
    145                 raise TypeError(
--> 146                     "All intermediate steps of the chain should not be"
    147                     " Pipelines")
    148 

TypeError: All intermediate steps of the chain should not be Pipelines

最佳答案

如果您在文件 imblearn/pipeline.py 中探索 imblean 的代码here , 在函数下 _validate_steps , 他们会检查 transformers 中的每一项是否存在作为 scikit 管道实例的转换器 ( isinstance(t, pipeline.Pipeline) )。

从您的代码中,transformers

  • RandomUnderSampler
  • transformer

  • 和类(class) Pipeline_imb使用 Pipeline_imb 时继承了 scikit 的流水线在您的代码中是多余的。

    话虽如此,我会像下面这样调整你的代码
    transformer = FeatureUnion(n_jobs=1, transformer_list=[
         # Select bolean features                                                  
        ('selector1', TypeSelector('bool'),
         # Select and scale numericals
        ('selector2', TypeSelector(np.number)),
        ('scaler', StandardScaler()),
         # Select and encode categoricals
        ('selector3', TypeSelector('category')),
        ('encoder', OneHotEncoder(handle_unknown='ignore'))
    ])
    
    pipe = Pipeline_imb([
        ('sampler', RandomUnderSampler(0.1)),
        ('prep', transformer), 
        ('clf', RandomForestClassifier(n_estimators=500, class_weight='balanced'))
    ])
    

    关于python - 如何在 scikit 学习管道中实现 RandomUnderSampler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193413/

    相关文章:

    python - 在 MLPClassification Python 中实现 K 折交叉验证

    python - 为什么sklearn(python)和matlab统计包中的LASSO不同?

    python - 如何在不平衡数据集中提高精度而不降低召回率?

    python - 重采样数据 - 使用 imblearn 中的 SMOTE 和 3D numpy 数组

    python-2.7 - PDF 格式的 scikit 学习文档

    python - 将CGImage转换为python图像(pil/opencv)

    python - 如何在 Flask 中使用 sqlalchemy core 进行分页?

    python - 加载经过训练的 Keras 模型并继续训练

    python - 我的项目 FLASK PYTHON 需要内部连接