Search code examples
pythonscikit-learnscikit-learn-pipeline

How can I access fitted estimators in a ColumnTransformer?


I designed my sklearn pipeline in the following way:

transf_pipe = make_column_transformer(
    (binning_pipe, ['na', 'nc']),
    (OneHotEncoder(drop='if_binary'), ['site_type']),
    (make_pipeline(
        common_country,
        OneHotEncoder()), 'visitor_country'),
    (make_pipeline(imputing_pipe,
                    StandardScaler()
                  ), ['window', 'day_of_week', 'los']),
    remainder='passthrough',
    n_jobs=-1
)

full_pipe = make_pipeline(
    transf_pipe,
    RandomForestClassifier(random_state=3, min_samples_leaf=8), 
)

I called full_pipe.fit(X_train, y_train and all works smoothly.

However, I want to inspect some of the transformers or estimators, such as the one hot encoder, when I access it through full_pipe.named_steps I am not able to see the fitted attributes. It is like the estimator was not fitted.

How can I access the fitted estimators of a pipeline?


Solution

    • You can access steps in a Pipeline with full_pipe.named_steps or with indices full_pipe[0].
    • You can access fitted transformers in a ColumnTransformer with transf_pipe.transformers_.

    In your example, use full_pipe[0].transformers_[1][1] to access the fitted OneHotEncoder.