首页 > 技术文章 > 特征选择之gbdt的shap(分类回归) 完全指南 part4

chentianyu 2022-01-07 15:43 原文

https://zhuanlan.zhihu.com/p/91991467

import xgboost as xgb import shap from sklearn.ensemble import RandomForestRegressor # load JS visualization code to notebook shap.initjs() # train XGBoost model X,y = shap.datasets.boston() X=X[['INDUS','CHAS']] model = xgb.XGBRegressor(n_estimator=1) model.fit(X,y) # explain the model's predictions using SHAP values # (same syntax works for LightGBM, CatBoost, scikit-learn and spark models) explainer = shap.TreeExplainer(model) shap_values = explainer.shap_values(X) # visualize the first prediction's explanation (use matplotlib=True to avoid Javascript) shap.force_plot(explainer.expected_value, shap_values[0:1,:], X.iloc[0:1,:])

推荐阅读