import xgboost as xgb
xgr = xgb.XGBRegressor(
  random_state=42, base_score=0.5
)
xgr.fit(bos_X_train, bos_y_train)
[14:43:03] WARNING: /workspace/src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
             colsample_bynode=1, colsample_bytree=1, gamma=0,
             importance_type='gain', learning_rate=0.1, max_delta_step=0,
             max_depth=3, min_child_weight=1, missing=None, n_estimators=100,
             n_jobs=1, nthread=None, objective='reg:linear', random_state=42,
             reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
             silent=None, subsample=1, verbosity=1)

16.1 셰이플리

sample_idx = 5
xgr.predict(bos_X.iloc[[sample_idx]])
array([27.269186], dtype=float32)
import shap
shap.initjs()

exp = shap.TreeExplainer(xgr)
vals = exp.shap_values(bos_X)

shap.force_plot(
  exp.expected_value,
  vals[sample_idx],
  bos_X.iloc[sample_idx],
)
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
shap.initjs()

shap.force_plot(
  exp.expected_value, vals, bos_X
)
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
import matplotlib.pyplot as plt

shap.dependence_plot("LSTAT", vals, bos_X)
shap.dependence_plot(
  "DIS", vals, bos_X, interaction_index="RM"
)
shap.summary_plot(vals, bos_X)