{"library":"shap","code":"import shap\nimport xgboost\nimport pandas as pd\nfrom sklearn.datasets import make_classification\n\n# Generate synthetic data\nX, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=2, n_classes=2, random_state=42)\nX = pd.DataFrame(X, columns=[f'feature_{i}' for i in range(X.shape[1])])\n\n# Train an XGBoost model\nmodel = xgboost.XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42)\nmodel.fit(X, y)\n\n# For Jupyter notebooks, uncomment the following line to enable JavaScript visualizations:\n# shap.initjs()\n\n# Create a SHAP Explainer (automatically infers TreeExplainer for XGBoost)\nexplainer = shap.Explainer(model, X)\n\n# Calculate SHAP values for the dataset\nshap_values = explainer(X)\n\n# Visualize the global impact of features using a beeswarm plot\n# This plot shows the distribution of SHAP values for each feature.\nshap.plots.beeswarm(shap_values, max_display=10)\n\n# To visualize an individual prediction (e.g., the first instance) with a waterfall plot:\n# shap.plots.waterfall(shap_values[0])","lang":"python","description":"This quickstart demonstrates how to use SHAP to explain an XGBoost classifier. It involves generating synthetic data, training a model, initializing the SHAP explainer (which automatically detects the model type), calculating SHAP values using the modern callable explainer API, and visualizing the feature impacts with a beeswarm plot. For interactive environments like Jupyter, `shap.initjs()` may be required for plot rendering.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}