【问题标题】:How can I check the changes made by Scikit-Learn Pipeline?如何检查 Scikit-Learn Pipeline 所做的更改?
【发布时间】:2021-10-17 08:53:03
【问题描述】:

这是一个非常简单的问题,但我在任何地方都找不到答案。我尝试了 Google、TDS、Analytics Vidhya、StackOverflow 等……所以,事情就是这样,我正在使用 Scikit-Learn Pipelines,但我想看看我的数据是如何被 Pipeline 处理的。我的意思是,假设我缺少值,现在它已被填充。我想看填充的数据,我想看编码器生成的假人等等

【问题讨论】:

    标签: python scikit-learn pipeline scikit-learn-pipeline


    【解决方案1】:

    这种检查没有通用的解决方案,因为管道可以由非常不同的步骤和非常不同的数据处理步骤组成,例如插补、矢量化、特征编码等。因此,每个步骤的可用信息可能非常不同。

    因此,我认为最好的方法是通过变压器安装后将暴露的属性或变压器检索信息的专用方法分别检查每个步骤。

    假设您有以下数据和管道:

    from sklearn.compose import ColumnTransformer
    from sklearn.impute import SimpleImputer
    from sklearn.preprocessing import OneHotEncoder
    from sklearn.pipeline importPipeline
    import numpy as np
    
    
    X = [['Male', 1, 7], ['Female', 3, 5], ['Female', 2, 12], [np.nan, 2, 4], ['Male', np.nan, 15]]
    
    pipeline = Pipeline(steps=[
        ('imputation', ColumnTransformer(transformers=[
            ('categorical', SimpleImputer(strategy='constant', fill_value='Missing'), [0]),
            ('numeric', SimpleImputer(strategy='mean'), [1, 2])
        ])),
        ('encoding', OneHotEncoder(handle_unknown='ignore'))
    ])
    
    Xt = pipeline.fit_transform(X)
    

    那么最好检查一下具体步骤的属性:

    >>> print(pipeline['imputation'].transformers_[1][1].statistics_) # computed mean for features 1 and 2
    [2.  8.6] 
    
    >>> print(pipeline['encoding'].get_feature_names()) # names of encoded categories
    [... 'x2_Female' 'x2_Male' 'x2_Missing']  
    

    这当然假设您知道您的管道是如何组成的,以及在拟合后每个步骤将公开哪些属性以及它提供了哪些其他方法(scikit-learn 的文档是查找的最佳位置)。

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 2017-10-04
      • 2014-09-27
      • 2021-09-18
      • 2018-06-01
      • 2018-01-27
      • 1970-01-01
      • 2020-09-02
      • 2019-08-11
      相关资源
      最近更新 更多