How to extract feature importances from an Sklearn pipeline

喜你入骨 提交于 2019-12-04 20:40:10

问题


I've built a pipeline in Scikit-Learn with two steps: one to construct features, and the second is a RandomForestClassifier.

While I can save that pipeline, look at various steps and the various parameters set in the steps, I'd like to be able to examine the feature importances from the resulting model.

Is that possible?


回答1:


Ah, yes it is.

You list identify the step where you want to check the estimator:

For instance:

pipeline.steps[1]

Which returns:

('predictor',
 RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
             max_depth=None, max_features='auto', max_leaf_nodes=None,
             min_samples_leaf=1, min_samples_split=2,
             min_weight_fraction_leaf=0.0, n_estimators=50, n_jobs=2,
             oob_score=False, random_state=None, verbose=0,
             warm_start=False))

You can then access the model step directly:

pipeline.steps[1][1].feature_importances_



来源:https://stackoverflow.com/questions/38787612/how-to-extract-feature-importances-from-an-sklearn-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!