繁体   English   中英

如何从 scikit-learn 中的 TransformedTargetRegressor 管道中的经过训练的估计器访问属性?

[英]How to access attribute from a trained estimator in TransformedTargetRegressor pipeline in scikit-learn?

我用 scikit-Learn 设置了一个小管道,我将它包裹在TransforedTargetRegressor object 中。 训练结束后,我想从训练有素的估计器中访问属性(例如feature_importances_ )。 谁能告诉我如何做到这一点?

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)

我尝试了以下方法:

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

但这会导致以下NotFittedError

NotFittedError:此 RandomForestRegressor 实例尚未安装。 在使用此方法之前,使用适当的 arguments 调用“fit”。

当您查看TransformedTargetRegressor的文档时,它说属性.regressor_ (注意结尾的下划线)返回拟合的回归量。 因此,您的电话应如下所示:

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

您之前的电话只是返回一个不合适的克隆。 这就是错误的来源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM