{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SHAP example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we show how one could use the shap python library and the `TreeExplainer` object to calculate the shap values for the LGBMOrdinal model.\n", "\n", "We note that the LGBMOrdinal model is based off the LGBMRegressor object, which is not a classifier. This allows us to calculate only one shap value per prediction, however many classes there are to predict, making it much easier to _explain_ the contribution of each feature to the prediction as well as the direction of the contribution." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import shap\n", "from shap import TreeExplainer\n", "from sklearn.metrics import ConfusionMatrixDisplay\n", "from sklearn.model_selection import train_test_split\n", "\n", "from ordinalgbt.data import make_ordinal_classification\n", "from ordinalgbt.lgb import LGBMOrdinal\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Training a model" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "X,y = make_ordinal_classification(n_classes=4,n_samples=1000, n_features = 100, n_informative =10,noise=2,\n", " random_state=42)\n", "X_train,X_test, y_train, y_test = train_test_split(X,y,train_size=0.8)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
LGBMOrdinal(objective=<function LGBMOrdinal._lgb_loss_factory.<locals>.loss at 0x143ea2040>)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LGBMOrdinal(objective=<function LGBMOrdinal._lgb_loss_factory.<locals>.loss at 0x143ea2040>)