Module3 forSesitivityAnalysisTopic
Module3 forSesitivityAnalysisTopic
The decision tree structure shows the path a decision takes depending on its value as
expected.
In this section, we imported the autopilot dataset and split it to obtain training data
and test data. We then created a decision tree classifier with default options, trained
it, and saved the model. We finally displayed the graph of the decision tree.
We now have a default decision tree classifier. We now need to work on our
explanations when the decision tree faces life and death situations.
estimator.tree_
n_nodes = estimator.tree_.node_count
[ 74 ]
Chapter 2
children_left = estimator.tree_.children_left
children_right = estimator.tree_.children_right
We can also view the feature used to split the node into the left and right child nodes:
feature = estimator.tree_.feature
threshold = estimator.tree_.threshold
The binary tree produces parallel arrays. The root node is node 0. The ith element
contains information about the node.
The program now parses the tree structure using the attribute arrays:
[ 75 ]
White Box XAI for AI Bias and Ethics
Once the decision tree structure's attribute arrays have been parsed, the program
prints the structure:
Our autopilot dataset will produce 255 nodes and list the tree structure, as shown in
the following excerpt:
The binary tree structure has 255 nodes and has the following tree
structure:
node=0 test node: go to node 1 if X[:, 3] <= 0.4599999934434891 else to
node 92.
node=1 test node: go to node 2 if X[:, 0] <= 0.35999999940395355
else to node 45.
node=2 test node: go to node 3 if X[:, 2] <= 0.5600000023841858
else to node 30.
A user will first accept an explanation but rapidly start asking what-if questions.
Let's answer two of the most common ones that come up during a project.
[ 76 ]
Chapter 2
First question
Why are there so many nodes? What if we reduced the number of nodes?
Good questions! The user is willing to use the software and help control the results
of the model before it goes into production. However, the user does not understand
what they are looking at!
We will go back to the code and decide to customize the decision tree classifier:
The binary tree structure has 5 nodes and has the following tree
structure:
node=0 test node: go to node 1 if X[:, 3] <= 0.4599999934434891 else to
node 2.
node=1 leaf node.
node=2 test node: go to node 3 if X[:, 1] <= 0.6599999964237213
else to node 4.
node=3 leaf node.
node=4 leaf node.
To make things easier, in the following chapters, we will progressively provide XAI
interfaces for a user. A user will be able to run some "what-if" scenarios on their own.
The user will understand the scenarios through XAI but will customize the model in
real time.
For this section and chapter, we did it manually. The user smiles. Our XAI scenario
has worked! The user will now control the decision tree by progressively increasing
the three key parameters.
However, a few seconds later, the user frowns again and comes up with the second
common question.
[ 77 ]
White Box XAI for AI Bias and Ethics
Second question
We reduced the number of nodes, and I appreciate the explanation. However, why has the
accuracy gone down?
Excellent question! What is the point of controlling a truncated decision tree? The
estimator will produce inaccurate results. If you scroll up to the training cell, you will
see that the accuracy has gone from 1 to 0.74:
Accuracy: 0.7483333333333333
Our customized scenario worked to explain AI. The user understands that, in some
cases, it will be possible to:
However, once the user understands the structure, can we visualize the tree structure
in another way?
Yes, we can use the graph of a decision tree, as described in the following section.
However, we discovered that if we simplify the decision tree classifier's job, we also
reduce the accuracy of the model.
We have another tool that we can use to explain the structure of a decision tree
iteratively. We can use the graph of the decision tree.
First, we go back, comment the customized estimator, and uncomment the default
estimator:
[ 78 ]
Chapter 2
prediction
[0 0 1 ... 1 1 0]
Accuracy: 1.0
The output will puzzle the user because many of the nodes overlap at lower levels of
the tree:
Furthermore, the image of the graph is huge. Our goal, in this section, is to explain a
decision tree with a graph. The next cells of the notebook will display a large graph
structure.
[ 79 ]
White Box XAI for AI Bias and Ethics
What if we reduced the max_depth of the graph by reducing it to 2 and reduced the
size of the figure as well? For example:
A user can now relate to this graph to understand how a decision tree works and
help confirm its calculations:
[ 80 ]
Chapter 2
The accuracy of the model is back to 1, but the graph displayed only shows a node
depth of 2.
For the moment, let's first introduce XAI and ethics in an autopilot decision tree.
Should we let an autopilot drive a car? Should we forbid the use of autopilots?
Should we find ways to alert the driver that the autopilot will be shut down in such
a situation? If the autopilot is shut down, will the human driver have enough time to
take over before hitting a pedestrian?
In this section, we will introduce real-life bias, moral, and ethical issues in the
decision tree to measure their reactions. We will then try to find ways to minimize
the damage autopilot errors can cause.
[ 81 ]