# Tabular Explanations

In this demo we will:

* Create a deployment which can be used to classify tabular data
* Create an explainer that will offer insight into why a particular prediction was made for a given input
* Send a prediction request to the deployment
* View the explanation

{% hint style="info" %}
**Note**: This demo uses a model trained to predict high or low income based on [demographic features from a 1996 US census](https://archive.ics.uci.edu/dataset/20/census+income).
{% endhint %}

## Register an income classifier model

Register a pre-trained income classifier SKLearn model.

1. In the `Model Catalog` page, click `Register a new model`:

   !["Register a new model" button on the Model Catalog page](/files/f40YduRjaeZW0pvnAmij)
2. In the `Register New Model` wizard, enter the following information, then click `Register Model`:

   * *Model Name*: `income-classifier`
   * *URI*: `gs://seldon-models/scv2/samples/mlserver_1.6.0/income-sklearn/classifier/`
   * *Artifact Type*: `SciKit Learn`
   * *Version*: `v1`

   ![Model configuration wizard](/files/JW50768QuvhqNKagvKha)

## Configure predictions schema for classifier

Edit the model metadata to update the prediction schema for the model. The prediction schema is a generic schema structure for machine learning model predictions. It is a definition of feature inputs and output targets from the model prediction. Learn more about the predictions schema at the [ML Predictions Schema](https://github.com/SeldonIO/ml-prediction-schema) open source repository. Use the income classifier model predictions schema `income-classifier-prediction-schema.json` to edit and save the model level metadata.

{% file src="/files/KarkDsbbSlDBd4oKaUuy" %}

1. Click the model `income-classifier` that you registered.

   ![Select "income-classifier" model on the Model Catalog page](/files/wp6bml9poTIBCtgT3G2h)
2. Click `Edit Metadata` to update the **Prediction schema** field associated with the model using the contents of prediction schema `income-classifier-prediction-schema.json`.

   ![Model's metadata wizard](/files/V1P3kl66ymxfPb1anFpx)
3. Click `Save Metadata`.

## Launch a Seldon Deployment

Deploy the income classifier model from the catalog into an appropriate namespace

1. In the **Model catalog**, select **Deploy** of the **Action** dropdown.

   !["Deploy" action in the dropdown on the Model Catalog page](/files/i0adewhXXW3T6A44aK8Z)
2. Enter the deployment details in the deployment creation wizard and click `Next`:

   * *Name*: `income-classifier`
   * *Namespace*: `seldon`
   * *Type*: `Seldon Deployment`

   ![Income classifier deployment details](/files/rrLmHj6CXlPAE12LQtN7)
3. The predictor details should already be filled in from the model catalog except the **Model Name** field. Use `income` for the **Model Name** field.

   * *Model Name*: `income`

   <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>The <code>Model Name</code> is linked to the name described in the <code>model-settings.json</code> file, located in the Google Cloud Storage location. Changing the name in the JSON file would also require changing the <code>Model Name</code>, and vice versa.</p></div>

![Income classifier deployment predictor](/files/wNHacbYPW2ZClgGK3O1d)

4. Click `Next` for the remaining steps, then click `Launch`.

## Make a prediction

1. Click the `income-classifier` deployment that you created.
2. In the deployment dashboard, click **Predict** in the left pane.
3. In the **Predict**page, click **Enter JSON** and paste the following request payload:

```json
{
  "inputs": [
    {
      "name": "income",
      "datatype": "INT64",
      "shape": [1, 12],
      "data": [53, 4, 0, 2, 8, 4, 2, 0, 0, 0, 60, 9]
    }
  ]
}
```

4. Click **Predict**.

## Add an Explainer

The Seldon Enterprise Platform supports several explanation methods for prediction requests. In this demo, you can learn about using these two methods:

* [Anchor Explainer](https://docs.seldon.io/projects/alibi/en/latest/methods/Anchors.html#id3)
* [Kernel SHAP Explainer](https://docs.seldon.io/projects/alibi/en/latest/methods/KernelSHAP.html#Kernel-SHAP)

The `Anchor Explainer` can be used to generate explanations for tabular data, text, and image classification models. Whereas, the `Kernel SHAP Explainer` can be used to generate explanations for only tabular data classification models. You can use the tabular data classification capabilities of these methods because the `income-classifier` deployment is considered a tabular data classification model.

### Add an Anchor Explainer

1. In the `income-classifier` deployment dashboard, click **Add** in the **MODEL EXPLANATION** card.
2. In the `Model Data Type` tab, select the `Tabular` model data type option and click `Next`.
3. In the `Explainer Types` tab, select the `Anchor` explainer type option and click `Next`.
4. In the `Explainer URI` tab, enter the following details:
   * *Explainer URI*: `gs://seldon-models/scv2/samples/mlserver_1.6.0/income-sklearn/anchor-explainer`
   * *Explainer Project*: `default`
   * *Storage Secret*: (leave blank/none)

![Using the Anchor Explainer URI](/files/4PwC62eoJJxBl0EfCwyH)

5. Click `Next` for the remaining steps, then click `Launch`.
6. If your explainer is launched successfully, both the deployment and the explainer will show an `Available` status.

### Add a Kernel SHAP Explainer

1. From the `income-classifier` deployment dashboard, click `Add` inside the `Model Explanation` card.
2. In the `Model Data Type` tab, select the `Tabular` model data type option and click `Next`.
3. In the `Explainer Types` tab, select the `KernelShap` explainer type option and click `Next`.
4. In the `Explainer URI` tab, enter the following details:
   * *Explainer URI*: `gs://seldon-models/scv2/samples/mlserver_1.6.0/income-sklearn/kernel-shap-explainer-sd`
   * *Explainer Project*: `default`
   * *Storage Secret*: (leave blank/none)

![Using the Kernel SHAP Explainer URI](/files/RTAKyBV3HAWeaAHWVVIe)

5. Click `Next` for the remaining steps, then click `Launch`.
6. If your explainer is launched successfully, both the deployment and the explainer will show an `Available` status.

{% hint style="info" %}
**Note**: If in the previous step you added the Anchor explainer, and you would like to use the Kernel SHAP explainer instead, you can do so by removing the Anchor explainer first. You will need to click on the ellipsis inside the `Model Explanation` card, select the `Delete` button, and confirm the deletion of the Anchor explainer.
{% endhint %}

## Explain a prediction

Once the explainer of choice is successfully launched, you can now generate explanations for the prediction request made earlier.

1. Navigate to the `Requests` page using the left navigation drawer.
2. Click on the `View explanation` button to generate explanations for the request.

### An explanation made using the Anchor Explainer

The Anchor explanation reveals which features most contributed to the classification of 'Low Income'. Here, for around 87% of the training data, a 'Relationship' of 'Unmarried' correlates to Low Income. Having a 'Sex' of 'Female' also correlates but is a less strong indicator. At the bottom-most screenshot, we are shown other examples of Low Income data points. This helps to reveal the patterns that the model is relying on to make predictions.

![The explanation page showing the original prediction request and the predicted class - Low Income](/files/IWfH8NzxkAB2fvtCAJ6V)

![The explanation summary consisting of the key contributing features towards the predicted class, and their request input values](/files/7CouqBm2au1RnoVXFxvf)

![The Anchor metrics of the explanation: Precision and Coverage](/files/uYewN6tTm2vV1T6CFbZp)

![Perturbed variation samples of the prediction request which comply with the prediction](/files/8hgSySMwO1geqUuvYzAh)

### An explanation made using the Kernel SHAP Explainer

The Kernel SHAP explanation, on the other hand, reveals the top 6 contributing features towards the predicted class of 'Low Income'. The features and their corresponding SHAP values shown in the purple bars indicate the most influential features towards the predicted class, whereas the blue bars indicate the features contributing the most towards the opposite class of 'High Income'. Here, the most influential 'positive' features are 'Marital Status' of 'Separated', followed by 'Sex' of 'Female, 'Race' of 'Black, and others. The most influential 'negative' features are 'Occupation' of 'White-Collar', 'Hours per week' of '60', 'Age' of '53', and others.

![The explanation page showing the original prediction request and the predicted response](/files/SmaIvJYiId1KnOoK4ExF)

![The explanation summary consisting of the key contributing features towards the predicted class, and the correspoding SHAP values](/files/1N4fXTJWl29aPEulbzuV)

Congratulations, you've created an explanation for the request! 🥳

## Next Steps

Why not try our other [demos](/seldon-enterprise-platform/demos.md)? Ready to dive in? Read our [operations guide](/seldon-enterprise-platform/operations.md) to learn more about how to use Enterprise Platform.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.seldon.ai/seldon-enterprise-platform/demos/seldon-core-v1/explainer-tabular.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
