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,:])