{ "cells": [ { "cell_type": "markdown", "id": "b1db8c9a", "metadata": {}, "source": [ "# Contrastive Reasons" ] }, { "cell_type": "markdown", "id": "514a5144", "metadata": {}, "source": [ "{: .attention }\n", "> Currently, contrastives reasons for Boosted trees are only available for binary classification." ] }, { "cell_type": "markdown", "id": "2570300b-d8a7-479a-880f-cd91d8ceb6af", "metadata": {}, "source": [ "Unlike abductive explanations that explain why an instance $x$ is classified as belonging to a given class, the **contrastive explanations** explains why $x$ has not been classified by the ML model as expected.\n", "\n", "Let 𝑓 be a Boolean function represented by a random forest 𝑅𝐹, 𝑥 be an instance and 1 (resp. 0) the prediction of 𝑅𝐹 on 𝑥 (𝑓(𝑥)=1 (resp $f(x)=0$)), a **contrastive reason** for $x$ is a term $t$ such that:\n", "* $t \\subseteq t_{x}$, $t_{x} \\setminus t$ is not an implicant of $f;$ \n", "* for every $\\ell \\in t$, $t \\setminus \\{\\ell\\}$ does not satisfy this previous condition (i.e., $t$ is minimal w.r.t. set inclusion).\n", "\n", "Formally, a **contrastive reason** for $x$ is a subset $t$ of the characteristics of $x$ that is minimal w.r.t. set inclusion among those such that at least one instance $x'$ that coincides with $x$ except on the characteristics from $t$ is not classified by the decision tree as $x$ is. Stated otherwhise, a **contrastive reason** represents adjustments of the features that we have to do to change the prediction for an instance. \n", "\n", "A contrastive reason is minimal w.r.t. set inclusion, i.e. there is no subset of this reason which is also a contrastive reason. A **minimal contrastive reason** for $x$ is a contrastive reason for $x$ that contains a minimal number of literals. In other words, a **minimal contrastive reason** has a minimal size. \n" ] }, { "cell_type": "markdown", "id": "ddab6f14-212f-4428-a052-6b13c1ce2b9a", "metadata": {}, "source": [ "| <ExplainerBT Object>.minimal_contrastive_reason(*, time_limit=None): | \n", "| :----------- | \n", "|This method computes a minimal contrastive reasons using a MIP solver. Return one minimal contrastive reason of the current instance. Supports the excluded features. The reasons are in the form of binary variables, you must use the ```to_features``` method if you want a representation based on the features considered at start.|\n", "| time_limit ```Integer``` ```None```: The time limit of the method in seconds. Set this to ```None``` to give this process an infinite amount of time. Default value is ```None```.|" ] }, { "cell_type": "markdown", "id": "aac420d8-b694-4326-b094-80f37df8505b", "metadata": {}, "source": [ "## Example from Building Trees" ] }, { "cell_type": "markdown", "id": "3ab1f94f-2827-4bd4-ba29-e9ce8439dc1b", "metadata": {}, "source": [ "We show now how to get them with PyXAI. We start by building the Boosted Tree:" ] }, { "cell_type": "code", "execution_count": 1, "id": "6039bf90-9d4f-4ae3-8088-a2a21d11819c", "metadata": {}, "outputs": [], "source": [ "from pyxai import Builder, Explainer\n", "\n", "node1_1 = Builder.DecisionNode(1, operator=Builder.GT, threshold=2, left=-0.2, right=0.3)\n", "node1_2 = Builder.DecisionNode(3, operator=Builder.EQ, threshold=1, left=-0.3, right=node1_1)\n", "node1_3 = Builder.DecisionNode(2, operator=Builder.GT, threshold=1, left=0.4, right=node1_2)\n", "node1_4 = Builder.DecisionNode(4, operator=Builder.EQ, threshold=1, left=-0.5, right=node1_3)\n", "tree1 = Builder.DecisionTree(4, node1_4)\n", "\n", "node2_1 = Builder.DecisionNode(4, operator=Builder.EQ, threshold=1, left=-0.4, right=0.3)\n", "node2_2 = Builder.DecisionNode(1, operator=Builder.GT, threshold=2, left=-0.2, right=node2_1)\n", "node2_3 = Builder.DecisionNode(2, operator=Builder.GT, threshold=1, left=node2_2, right=0.5)\n", "tree2 = Builder.DecisionTree(4, node2_3)\n", "\n", "node3_1 = Builder.DecisionNode(1, operator=Builder.GT, threshold=2, left=0.2, right=0.3)\n", "node3_2_1 = Builder.DecisionNode(1, operator=Builder.GT, threshold=2, left=-0.2, right=0.2)\n", "node3_2_2 = Builder.DecisionNode(4, operator=Builder.EQ, threshold=1, left=-0.1, right=node3_1)\n", "node3_2_3 = Builder.DecisionNode(4, operator=Builder.EQ, threshold=1, left=-0.5, right=0.1)\n", "node3_3_1 = Builder.DecisionNode(2, operator=Builder.GT, threshold=1, left=node3_2_1, right=node3_2_2)\n", "node3_3_2 = Builder.DecisionNode(2, operator=Builder.GT, threshold=1, left=-0.4, right=node3_2_3)\n", "node3_4 = Builder.DecisionNode(3, operator=Builder.EQ, threshold=1, left=node3_3_1, right=node3_3_2)\n", "tree3 = Builder.DecisionTree(4, node3_4)\n", "\n", "BT = Builder.BoostedTrees([tree1, tree2, tree3], n_classes=2)" ] }, { "cell_type": "markdown", "id": "7bb91b78-5e35-4c63-be7a-22a6c522e6ef", "metadata": {}, "source": [ "We compute a contrastive reason for these two instances: " ] }, { "cell_type": "code", "execution_count": 2, "id": "edda6920-1c68-4438-ac36-be411421677b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "target_prediction: 1\n", "minimal contrastive reason: ('f4 == 1',)\n" ] } ], "source": [ "explainer = Explainer.initialize(BT)\n", "explainer.set_instance((4,3,2,1))\n", "\n", "contrastive_reason = explainer.minimal_contrastive_reason()\n", "print(\"target_prediction:\", explainer.target_prediction)\n", "print(\"minimal contrastive reason:\", explainer.to_features(contrastive_reason))\n", "assert explainer.is_contrastive_reason(contrastive_reason), \"It is not a contrastive reason !\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "b1e548b3-abcc-4f6d-842e-2cd4c601537d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "target_prediction: 0\n" ] } ], "source": [ "# We can create a contrastive instance\n", "explainer.set_instance((4,3,2,0))\n", "print(\"target_prediction:\", explainer.target_prediction)\n", "# The prediction is 0 now." ] }, { "cell_type": "markdown", "id": "1622b7b9-2e82-4ee4-94a3-c1bc56cc8eb8", "metadata": {}, "source": [ "## Example from Real Dataset" ] }, { "cell_type": "markdown", "id": "e89785e8-cbcf-4703-91ff-de06253be788", "metadata": {}, "source": [ "For this example, we take the compas.csv known to be biased. We create a model using the hold-out approach (by default, the test size is set to 30%) and select a wrong classified instance." ] }, { "cell_type": "code", "execution_count": 4, "id": "79e0a715-e25b-41d5-a6f9-93cd79bbf305", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data:\n", " Number_of_Priors score_factor Age_Above_FourtyFive \\\n", "0 0 0 1 \n", "1 0 0 0 \n", "2 4 0 0 \n", "3 0 0 0 \n", "4 14 1 0 \n", "... ... ... ... \n", "6167 0 1 0 \n", "6168 0 0 0 \n", "6169 0 0 1 \n", "6170 3 0 0 \n", "6171 2 0 0 \n", "\n", " Age_Below_TwentyFive Origin_African_American Origin_Asian \\\n", "0 0 0 0 \n", "1 0 1 0 \n", "2 1 1 0 \n", "3 0 0 0 \n", "4 0 0 0 \n", "... ... ... ... \n", "6167 1 1 0 \n", "6168 1 1 0 \n", "6169 0 0 0 \n", "6170 0 1 0 \n", "6171 1 0 0 \n", "\n", " Origin_Hispanic Origin_Native_American Origin_Other Female \\\n", "0 0 0 1 0 \n", "1 0 0 0 0 \n", "2 0 0 0 0 \n", "3 0 0 1 0 \n", "4 0 0 0 0 \n", "... ... ... ... ... \n", "6167 0 0 0 0 \n", "6168 0 0 0 0 \n", "6169 0 0 1 0 \n", "6170 0 0 0 1 \n", "6171 1 0 0 1 \n", "\n", " Misdemeanor Two_yr_Recidivism \n", "0 0 0 \n", "1 0 1 \n", "2 0 1 \n", "3 1 0 \n", "4 0 1 \n", "... ... ... \n", "6167 0 0 \n", "6168 0 0 \n", "6169 0 0 \n", "6170 1 0 \n", "6171 0 1 \n", "\n", "[6172 rows x 12 columns]\n", "-------------- Information ---------------\n", "Dataset name: ../../../dataset/compas.csv\n", "nFeatures (nAttributes, with the labels): 12\n", "nInstances (nObservations): 6172\n", "nLabels: 2\n", "--------------- Evaluation ---------------\n", "method: HoldOut\n", "output: BT\n", "learner_type: Classification\n", "learner_options: {'seed': 0, 'max_depth': None, 'eval_metric': 'mlogloss'}\n", "--------- Evaluation Information ---------\n", "For the evaluation number 0:\n", "metrics:\n", " accuracy: 66.73866090712744\n", " precision: 66.66666666666666\n", " recall: 53.855278766310796\n", " f1_score: 59.580052493438316\n", " specificity: 77.50247770069376\n", " true_positive: 454\n", " true_negative: 782\n", " false_positive: 227\n", " false_negative: 389\n", " sklearn_confusion_matrix: [[782, 227], [389, 454]]\n", "nTraining instances: 4320\n", "nTest instances: 1852\n", "\n", "--------------- Explainer ----------------\n", "For the evaluation number 0:\n", "**Boosted Tree model**\n", "NClasses: 2\n", "nTrees: 100\n", "nVariables: 42\n", "\n", "--------------- Instances ----------------\n", "number of instances selected: 1\n", "----------------------------------------------\n" ] } ], "source": [ "from pyxai import Learning, Explainer\n", "\n", "learner = Learning.Xgboost(\"../../../dataset/compas.csv\", learner_type=Learning.CLASSIFICATION)\n", "model = learner.evaluate(method=Learning.HOLD_OUT, output=Learning.BT)\n", "instance, prediction = learner.get_instances(model, n=1, correct=False)\n", "\n" ] }, { "cell_type": "markdown", "id": "27ba9df8-5185-4d2d-a387-d383bbcacf37", "metadata": {}, "source": [ "We compute a contrastive instance (with a time limit equal to 5 seconds). In order to compute contrastive reasons, it is better to activate the theory related to the type of features (see this [page](/documentation/explainer/theories/))." ] }, { "cell_type": "code", "execution_count": 7, "id": "ab2f471d-1faf-4465-bf72-29efb76dbd00", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--------- Theory Feature Types -----------\n", "Before the encoding (without one hot encoded features), we have:\n", "Numerical features: 1\n", "Categorical features: 2\n", "Binary features: 3\n", "Number of features: 6\n", "Values of categorical features: {'Origin_African_American': ['Origin_', 'African_American', ['African_American', 'Asian', 'Hispanic', 'Native_American', 'Other']], 'Origin_Asian': ['Origin_', 'Asian', ['African_American', 'Asian', 'Hispanic', 'Native_American', 'Other']], 'Origin_Hispanic': ['Origin_', 'Hispanic', ['African_American', 'Asian', 'Hispanic', 'Native_American', 'Other']], 'Origin_Native_American': ['Origin_', 'Native_American', ['African_American', 'Asian', 'Hispanic', 'Native_American', 'Other']], 'Origin_Other': ['Origin_', 'Other', ['African_American', 'Asian', 'Hispanic', 'Native_American', 'Other']], 'Age_Above_FourtyFive': ['Age_', 'Above_FourtyFive', ['Above_FourtyFive', 'Below_TwentyFive']], 'Age_Below_TwentyFive': ['Age_', 'Below_TwentyFive', ['Above_FourtyFive', 'Below_TwentyFive']]}\n", "\n", "Number of used features in the model (before the encoding): 6\n", "Number of used features in the model (after the encoding): 11\n", "----------------------------------------------\n", "instance: [0 0 0 0 1 0 0 0 0 0 0]\n", "target_prediction: 0\n", "minimal contrastive reason: ('Number_of_Priors < 3.5', 'Origin_ = African_American')\n", "is contrastive: True\n" ] } ], "source": [ "types = {\n", " \"numerical\": [\"Number_of_Priors\"],\n", " \"categorical\": {\"Origin_*\":['African_American', 'Asian', 'Hispanic', 'Native_American', 'Other'],\n", " \"Age_*\": [\"Above_FourtyFive\", \"Below_TwentyFive\"]},\n", " \"binary\": Learning.DEFAULT,\n", " }\n", "\n", "\n", "\n", "explainer = Explainer.initialize(model, instance, features_type=types)\n", "\n", "contrastive_reason = explainer.minimal_contrastive_reason(time_limit=5)\n", "print(\"instance: \", instance)\n", "print(\"target_prediction:\", explainer.target_prediction)\n", "print(\"minimal contrastive reason:\", explainer.to_features(contrastive_reason, contrastive=True))\n", "print(\"is contrastive: \", explainer.is_contrastive_reason(contrastive_reason))" ] }, { "cell_type": "markdown", "id": "621a22d3-4e7d-4772-b15a-87c5dfb1fe42", "metadata": {}, "source": [ "If one wants to change the classification, one needs to change the origin and the number of priors (to at least 4)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }