Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_lightgbm_constant_prediction():
# note: this test used to fail with lightgbm 2.2.1 with error:
# ValueError: zero-size array to reduction operation maximum which has no identity
# on TreeExplainer when trying to compute max nodes:
# max_nodes = np.max([len(t.values) for t in self.trees])
# The test does not fail with latest lightgbm 2.2.3 however
try:
import lightgbm
except:
print("Skipping test_lightgbm_constant_prediction!")
return
import shap
# train lightgbm model with a constant value for y
X, y = shap.datasets.boston()
# use the mean for all values
mean = np.mean(y)
y.fill(mean)
model = lightgbm.sklearn.LGBMRegressor(n_estimators=1)
model.fit(X, y)
# explain the model's predictions using SHAP values
shap_values = shap.TreeExplainer(model).shap_values(X)
def test_front_page_xgboost_global_path_dependent():
try:
import xgboost
except:
print("Skipping test_front_page_xgboost!")
return
# train XGBoost model
X, y = shap.datasets.boston()
model = xgboost.XGBRegressor()
model.fit(X, y)
# explain the model's predictions using SHAP values
explainer = shap.TreeExplainer(model, X, feature_perturbation="global_path_dependent")
shap_values = explainer.shap_values(X)
assert np.allclose(shap_values.sum(1) + explainer.expected_value, model.predict(X))
def test_catboost():
try:
import catboost
except:
print("Skipping test_catboost!")
return
import shap
# train catboost model
X, y = shap.datasets.boston()
X["RAD"] = X["RAD"].astype(np.int)
model = catboost.CatBoostRegressor(iterations=300, learning_rate=0.1, random_seed=123)
p = catboost.Pool(X, y, cat_features=["RAD"])
model.fit(p, verbose=False, plot=False)
# explain the model's predictions using SHAP values
ex = shap.TreeExplainer(model)
shap_values = ex.shap_values(p)
predicted = model.predict(X)
assert np.abs(shap_values.sum(1) + ex.expected_value - predicted).max() < 1e-6, \
"SHAP values don't sum to model output!"
def test_front_page_xgboost():
import xgboost
import shap
# load JS visualization code to notebook
shap.initjs()
# train XGBoost model
X, y = shap.datasets.boston()
model = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
# explain the model's predictions using SHAP values (use pred_contrib in LightGBM)
shap_values = shap.TreeExplainer(model).shap_values(X)
# visualize the first prediction's explaination
shap.force_plot(shap_values[0, :], X.iloc[0, :])
# visualize the training set predictions
shap.force_plot(shap_values, X)
# create a SHAP dependence plot to show the effect of a single feature across the whole dataset
shap.dependence_plot(5, shap_values, X, show=False)
shap.dependence_plot("RM", shap_values, X, show=False)
# summarize the effects of all the features
def test_front_page_xgboost():
try:
import xgboost
except Exception as e:
print("Skipping test_front_page_xgboost!")
return
import shap
# load JS visualization code to notebook
shap.initjs()
# train XGBoost model
X, y = shap.datasets.boston()
model = xgboost.train({"learning_rate": 0.01, "silent": 1}, xgboost.DMatrix(X, label=y), 100)
# explain the model's predictions using SHAP values
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
# visualize the first prediction's explaination
shap.force_plot(explainer.expected_value, shap_values[0, :], X.iloc[0, :])
# visualize the training set predictions
shap.force_plot(explainer.expected_value, shap_values, X)
# create a SHAP dependence plot to show the effect of a single feature across the whole dataset
shap.dependence_plot(5, shap_values, X, show=False)
shap.dependence_plot("RM", shap_values, X, show=False)
def test_xgboost_mixed_types():
try:
import xgboost
except Exception as e:
print("Skipping test_xgboost_mixed_types!")
return
import shap
import numpy as np
X,y = shap.datasets.boston()
X["LSTAT"] = X["LSTAT"].astype(np.int64)
X["B"] = X["B"].astype(np.bool)
bst = xgboost.train({"learning_rate": 0.01, "silent": 1}, xgboost.DMatrix(X, label=y), 1000)
shap_values = shap.TreeExplainer(bst).shap_values(X)
shap.dependence_plot(0, shap_values, X, show=False)
def test_front_page_xgboost():
try:
import xgboost
except Exception as e:
print("Skipping test_front_page_xgboost!")
return
import shap
# load JS visualization code to notebook
shap.initjs()
# train XGBoost model
X, y = shap.datasets.boston()
model = xgboost.train({"learning_rate": 0.01, "silent": 1}, xgboost.DMatrix(X, label=y), 100)
# explain the model's predictions using SHAP values
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
# visualize the first prediction's explaination
shap.force_plot(explainer.expected_value, shap_values[0, :], X.iloc[0, :])
# visualize the training set predictions
shap.force_plot(explainer.expected_value, shap_values, X)
# create a SHAP dependence plot to show the effect of a single feature across the whole dataset
shap.dependence_plot(5, shap_values, X, show=False)
shap.dependence_plot("RM", shap_values, X, show=False)
def test_xgboost_mixed_types():
try:
import xgboost
except Exception as e:
print("Skipping test_xgboost_mixed_types!")
return
import shap
import numpy as np
X,y = shap.datasets.boston()
X["LSTAT"] = X["LSTAT"].astype(np.int64)
X["B"] = X["B"].astype(np.bool)
bst = xgboost.train({"learning_rate": 0.01, "silent": 1}, xgboost.DMatrix(X, label=y), 1000)
shap_values = shap.TreeExplainer(bst).shap_values(X)
shap.dependence_plot(0, shap_values, X, show=False)