2

I setup a small pipeline with scikit-Learn that I wrapped in a TransforedTargetRegressor object. After the training, I would like to access the attribute from my trained estimator (e.g. feature_importances_). Can anyone tell me how this can be done?

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import MinMaxScaler
from sklearn.compose import TransformedTargetRegressor

# setup the pipeline
Pipeline(steps = [('scale', StandardScaler(with_mean=True, with_std=True)),
                  ('estimator', RandomForestRegressor())])

# tranform target variable
model = TransformedTargetRegressor(regressor=pipeline, 
                                   transformer=MinMaxScaler())
           
# fit model
model.fit(X_train, y_train)

I tried the following:

# try to access the attribute of the fitted estimator
model.get_params()['regressor__estimator'].feature_importances_
model.regressor.named_steps['estimator'].feature_importances_

But this results in the following NotFittedError:

NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

1 Answer 1

3

When you look into the documentation of TransformedTargetRegressor it says that the attribute .regressor_ (note the trailing underscore) returns the fitted regressor. Hence, your call should look like:

model.regressor_.named_steps['estimator'].feature_importances_

Your previous calls were just returning an unfitted clone. That's were the error came from.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the hint! the code snippet you proposed returned a "TypeError: 'Pipeline' object is not subscriptable". However, based on your answer I figured out, that "model.regressor_.named_steps['estimator'].feature_importances_" works!
Does anyone know why they used a trailing underscore here? Is there some convention about naming?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.