Census income classification with LightGBM

This notebook demonstrates how to use LightGBM to predict the probability of an individual making over $50K a year in annual income. It uses the standard UCI Adult income dataset. To download a copy of this notebook visit github.

Gradient boosting machine methods such as LightGBM are state-of-the-art for these types of prediction problems with tabular style input data of many modalities. Tree SHAP (arXiv paper) allows for the exact computation of SHAP values for tree ensemble methods, and has been integrated directly into the C++ LightGBM code base. This allows fast exact computation of SHAP values without sampling and without providing a background dataset (since the background is inferred from the coverage of the trees).

Here we demonstrate how to use SHAP values to understand LightGBM model predictions.

[1]:
import lightgbm as lgb
from sklearn.model_selection import train_test_split

import shap

# print the JS visualization code to the notebook
shap.initjs()

Load dataset

[2]:
X, y = shap.datasets.adult()
X_display, y_display = shap.datasets.adult(display=True)

# create a train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
d_train = lgb.Dataset(X_train, label=y_train)
d_test = lgb.Dataset(X_test, label=y_test)

Train the model

[3]:
params = {
    "max_bin": 512,
    "learning_rate": 0.05,
    "boosting_type": "gbdt",
    "objective": "binary",
    "metric": "binary_logloss",
    "num_leaves": 10,
    "verbose": -1,
    "min_data": 100,
    "boost_from_average": True,
    "early_stopping_round": 50,
}

model = lgb.train(
    params,
    d_train,
    10000,
    valid_sets=[d_test],
)

Explain predictions

Here we use the Tree SHAP implementation integrated into Light GBM to explain the entire dataset (32561 samples). The Light GBM implementation of Tree SHAP is called from within the shap.TreeExplainer.shap_values method.

[4]:
explainer = shap.TreeExplainer(model)
shap_values = explainer(X)
[5]:
shap.force_plot(
    explainer.expected_value, shap_values.values[1, :], X_display.iloc[0, :]
)
[5]:
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.

Visualize many predictions

To keep the browser happy we only visualize 1,000 individuals.

[6]:
shap.force_plot(
    explainer.expected_value, shap_values.values[:1000, :], X_display.iloc[:1000, :]
)
[6]:
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 Summary Plot

Rather than use a typical feature importance bar chart, we use a density scatter plot of SHAP values for each feature to identify how much impact each feature has on the model output for individuals in the validation dataset. Features are sorted by the sum of the SHAP value magnitudes across all samples. It is interesting to note that the relationship feature has more total model impact than the capital gain feature, but for those samples where capital gain matters it has more impact than age. In other words, capital gain effects a few predictions by a large amount, while age effects all predictions by a smaller amount.

Note that when the scatter points don’t fit on a line they pile up to show density, and the color of each point represents the feature value of that individual.

[7]:
shap.summary_plot(shap_values, X)
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_12_0.png

SHAP Dependence Plots

SHAP dependence plots show the effect of a single feature across the whole dataset. They plot a feature’s value vs. the SHAP value of that feature across many samples. SHAP dependence plots are similar to partial dependence plots, but account for the interaction effects present in the features, and are only defined in regions of the input space supported by data. The vertical dispersion of SHAP values at a single feature value is driven by interaction effects, and another feature is chosen for coloring to highlight possible interactions.

[8]:
for name in X_train.columns:
    shap.dependence_plot(name, shap_values.values, X, display_features=X_display)
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_0.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_1.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_2.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_3.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_4.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_5.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_6.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_7.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_8.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_9.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_10.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_14_11.png

Train a model with only two leaves per tree and hence no interaction terms between features

Forcing the model to have no interaction terms means the effect of a feature on the outcome does not depend on the value of any other feature. This is reflected in the SHAP dependence plots below as no vertical spread. A vertical spread reflects that a single value of a feature can have different effects on the model output depending on the context of the other features present for an individual. However, for models without interaction terms, a feature always has the same impact regardless of what other attributes an individual may have.

One of the benefits of SHAP dependence plots over traditional partial dependence plots is this ability to distigush between between models with and without interaction terms. In other words, SHAP dependence plots give an idea of the magnitude of the interaction terms through the vertical variance of the scatter plot at a given feature value.

[9]:
params = {
    "max_bin": 512,
    "learning_rate": 0.1,
    "boosting_type": "gbdt",
    "objective": "binary",
    "metric": "binary_logloss",
    "num_leaves": 2,
    "verbose": -1,
    "min_data": 100,
    "boost_from_average": True,
    "early_stopping_round": 50,
}

model_ind = lgb.train(
    params,
    d_train,
    20000,
    valid_sets=[d_test],
)
[10]:
explainer = shap.TreeExplainer(model_ind)
shap_values_ind = explainer(X)

Note that the interaction color bars below are meaningless for this model because it has no interactions.

[11]:
for name in X_train.columns:
    shap.dependence_plot(name, shap_values_ind.values, X, display_features=X_display)
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_0.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_1.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_2.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_3.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_4.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_5.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_6.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_7.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_8.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_9.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_10.png
../../../_images/example_notebooks_tabular_examples_tree_based_models_Census_income_classification_with_LightGBM_19_11.png