Explainable AI Recipes: Implement Solutions To Model Explainability and Interpretability With Python 1st Edition Pradeepta Mishra
Explainable AI Recipes: Implement Solutions To Model Explainability and Interpretability With Python 1st Edition Pradeepta Mishra
or textbooks at https://ebookmass.com
_____ Follow the link below to get your download now _____
https://ebookmass.com/product/explainable-ai-recipes-
implement-solutions-to-model-explainability-and-
interpretability-with-python-1st-edition-pradeepta-mishra/
https://ebookmass.com/product/productionizing-ai-how-to-deliver-
ai-b2b-solutions-with-cloud-and-python-1st-edition-barry-walsh/
https://ebookmass.com/product/introduction-to-responsible-ai-
implement-ethical-ai-using-python-1st-edition-manure/
https://ebookmass.com/product/productionizing-ai-how-to-deliver-
ai-b2b-solutions-with-cloud-and-python-1st-edition-barry-walsh-2/
Time Series Algorithms Recipes: Implement Machine Learning
and Deep Learning Techniques with Python Akshay R Kulkarni
https://ebookmass.com/product/time-series-algorithms-recipes-
implement-machine-learning-and-deep-learning-techniques-with-python-
akshay-r-kulkarni/
https://ebookmass.com/product/introduction-to-datafication-implement-
datafication-using-ai-and-ml-algorithms-shivakumar-r-goniwada-2/
https://ebookmass.com/product/introduction-to-datafication-implement-
datafication-using-ai-and-ml-algorithms-shivakumar-r-goniwada/
https://ebookmass.com/product/introduction-to-prescriptive-ai-a-
primer-for-decision-intelligence-solutioning-with-python-akshay-
kulkarni/
Explainable AI Recipes: Implement Solutions to Model Explainability and
Interpretability with Python
Pradeepta Mishra
Bangalore, Karnataka, India
Acknowledgments�����������������������������������������������������������������������������xxi
Introduction�������������������������������������������������������������������������������������xxiii
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
Conclusion����������������������������������������������������������������������������������������������������������71
References����������������������������������������������������������������������������������������������������������72
ix
Table of Contents
Recipe 3-6. SHAP Feature Importance for Tree Regression Models with
All Mixed Input Variables�������������������������������������������������������������������������������������90
Problem���������������������������������������������������������������������������������������������������������90
Solution���������������������������������������������������������������������������������������������������������91
How It Works�������������������������������������������������������������������������������������������������91
Recipe 3-7. LIME Explainer for Tabular Data�������������������������������������������������������93
Problem���������������������������������������������������������������������������������������������������������93
Solution���������������������������������������������������������������������������������������������������������93
How It Works�������������������������������������������������������������������������������������������������93
Recipe 3-8. ELI5 Explainer for Tabular Data��������������������������������������������������������96
Problem���������������������������������������������������������������������������������������������������������96
Solution���������������������������������������������������������������������������������������������������������96
How It Works�������������������������������������������������������������������������������������������������96
Recipe 3-9. How the Permutation Model in ELI5 Works������������������������������������100
Problem�������������������������������������������������������������������������������������������������������100
Solution�������������������������������������������������������������������������������������������������������101
How It Works�����������������������������������������������������������������������������������������������101
Recipe 3-10. Global Explanation for Decision Tree Models�������������������������������101
Problem�������������������������������������������������������������������������������������������������������101
Solution�������������������������������������������������������������������������������������������������������101
How It Works�����������������������������������������������������������������������������������������������102
Recipe 3-11. Partial Dependency Plot for a Nonlinear Classifier����������������������104
Problem�������������������������������������������������������������������������������������������������������104
Solution�������������������������������������������������������������������������������������������������������104
How It Works�����������������������������������������������������������������������������������������������104
Recipe 3-12. Global Feature Importance from the Nonlinear Classifier������������107
Problem�������������������������������������������������������������������������������������������������������107
Solution�������������������������������������������������������������������������������������������������������107
How It Works�����������������������������������������������������������������������������������������������107
x
Table of Contents
xi
Table of Contents
Recipe 4-5. Explain the Catboost Regressor with Global and Local
Interpretations��������������������������������������������������������������������������������������������������139
Problem�������������������������������������������������������������������������������������������������������139
Solution�������������������������������������������������������������������������������������������������������139
How It Works�����������������������������������������������������������������������������������������������140
Recipe 4-6. Explain the EBM Classifier with Global and
Local Interpretations�����������������������������������������������������������������������������������������142
Problem�������������������������������������������������������������������������������������������������������142
Solution�������������������������������������������������������������������������������������������������������142
How It Works�����������������������������������������������������������������������������������������������143
Recipe 4-7. SHAP Partial Dependency Plot for Regression Models with
Mixed Input�������������������������������������������������������������������������������������������������������145
Problem�������������������������������������������������������������������������������������������������������145
Solution�������������������������������������������������������������������������������������������������������145
How It Works�����������������������������������������������������������������������������������������������145
Recipe 4-8. SHAP Feature Importance for Tree Regression Models with
Mixed Input Variables����������������������������������������������������������������������������������������149
Problem�������������������������������������������������������������������������������������������������������149
Solution�������������������������������������������������������������������������������������������������������149
How It Works�����������������������������������������������������������������������������������������������150
Recipe 4-9. Explaining the XGBoost Model�������������������������������������������������������154
Problem�������������������������������������������������������������������������������������������������������154
Solution�������������������������������������������������������������������������������������������������������154
How It Works�����������������������������������������������������������������������������������������������154
Recipe 4-10. Random Forest Regressor for Mixed Data Types�������������������������159
Problem�������������������������������������������������������������������������������������������������������159
Solution�������������������������������������������������������������������������������������������������������159
How It Works�����������������������������������������������������������������������������������������������159
xii
Table of Contents
xiii
Table of Contents
xiv
Table of Contents
xv
Table of Contents
Index�������������������������������������������������������������������������������������������������251
xvi
About the Author
Pradeepta Mishra is an AI/ML leader,
experienced data scientist, and artificial
intelligence architect. He currently heads
NLP, ML, and AI initiatives for five products at
FOSFOR by LTI, a leading-edge innovator in AI
and machine learning based out of Bangalore,
India. He has expertise in designing artificial
intelligence systems for performing tasks such
as understanding natural language and making
recommendations based on natural language
processing. He has filed 12 patents as an inventor and has authored
and coauthored five books, including R Data Mining Blueprints (Packt
Publishing, 2016), R: Mining Spatial, Text, Web, and Social Media Data
(Packt Publishing, 2017), PyTorch Recipes (Apress, 2019), and Practical
Explainable AI Using Python (Apress, 2023). There are two courses
available on Udemy based on these books.
Pradeepta presented a keynote talk on the application of bidirectional
LSTM for time-series forecasting at the 2018 Global Data Science
Conference. He delivered the TEDx talk “Can Machines Think?” on the
power of artificial intelligence in transforming industries and job roles
across industries. He has also delivered more than 150 tech talks on data
science, machine learning, and artificial intelligence at various meetups,
technical institutions, universities, and community forums. He is on
LinkedIn (www.linkedin.com/in/pradeepta/) and Twitter
(@pradmishra1).
xvii
About the Technical Reviewer
Bharath Kumar Bolla has more than ten years
of experience and is currently working as a
senior data science engineer consultant at
Verizon, Bengaluru. He has a PG diploma in
data science from Praxis Business School and
an MS in life sciences from Mississippi State
University. He previously worked as a data
scientist at the University of Georgia, Emory
University, and Eurofins LLC & Happiest
Minds. At Happiest Minds, he worked on
AI-based digital marketing products and
NLP-based solutions in the education domain. Along with his day-to-day
responsibilities, Bharath is a mentor and an active researcher. To date, he
has published ten articles in journals and peer-reviewed conferences. He is
particularly interested in unsupervised and semisupervised learning and
efficient deep learning architectures in NLP and computer vision.
xix
Acknowledgments
I would like to thank my wife, Prajna, for her continuous inspiration and
support and for sacrificing her weekends to help me complete this book;
and my daughters, Aarya and Aadya, for being patient throughout the
writing process.
A big thank-you to Celestin Suresh John and Mark Powers for fast-
tracking the whole process and guiding me in the right direction.
I would like to thank the authors of the Appliances Energy Prediction
dataset (http://archive.ics.uci.edu/ml) for making it available: D. Dua
and C. Graff. I use this dataset in the book to show how to develop a model
and explain the predictions generated by a regression model for the
purpose of model explainability using various explainable libraries.
xxi
Introduction
Artificial intelligence plays a crucial role determining the decisions
businesses make. In these cases, when a machine makes a decision,
humans usually want to understand whether the decision is authentic
or whether it was generated in error. If business stakeholders are not
convinced by the decision, they will not trust the machine learning system,
and hence artificial intelligence adoption will gradually reduce within that
organization. To make the decision process more transparent, developers
must be able to document the explainability of AI decisions or ML model
decisions. This book provides a series of solutions to problems that require
explainability and interpretability. Adopting an AI model and developing a
responsible AI system requires explainability as a component.
This book covers model interpretation for supervised learning linear
models, including important features for regression and classification
models, partial dependency analysis for regression and classification
models, and influential data point analysis for both classification and
regression models. Supervised learning models using nonlinear models is
explored using state-of-the-art frameworks such as SHAP values/scores,
including global explanation, and how to use LIME for local interpretation.
This book will also give you an understanding of bagging, boosting-
based ensemble models for supervised learning such as regression and
classification, as well as explainability for time-series models using LIME
and SHAP, natural language processing tasks such as text classification,
and sentiment analysis using ELI5, ALIBI. The most complex models for
classification and regression, such as neural network models and deep
learning models, are explained using the CAPTUM framework, which
shows feature attribution, neuron attribution, and activation attribution.
xxiii
Introduction
xxiv
CHAPTER 1
Introducing
Explainability and
Setting Up Your
Development
Environment
Industries in which artificial intelligence has been applied include
banking, financial services, insurance, healthcare, manufacturing, retail,
and pharmaceutical. There are regulatory requirements in some of these
industries where model explainability is required. Artificial intelligence
involves classifying objects, recognizing objects to detect fraud, and so forth.
Every learning system requires three things: input data, processing, and an
output. If the performance of any learning system improves over time by
learning from new examples or data, it is called a machine learning system.
When the number of features for a machine learning task increases or the
volume of data increases, it takes a lot of time to apply machine learning
techniques. That’s when deep learning techniques are used.
Figure 1-1 represents the relationships between artificial intelligence,
machine learning, and deep learning.
2
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
• Adoption: To increase AI adoption among the users
S
olution
The solution to this problem is to use the simple pip or conda option.
3
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
How It Works
Let’s take a look at the following script examples. The SHAP Python library
is based on a game theoretic approach that attempts to explain local and
as well as global explanations.
or
5
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Recipe 1-2. LIME Installation
Problem
You want to install the LIME Python library.
Solution
You can install the LIME library using pip or conda.
How It Works
Let’s take a look at the following example script:
or
6
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Requirement already satisfied: scikit-learn>=0.18 in /usr/
local/lib/python3.7/dist-packages (from lime) (1.0.2)
Requirement already satisfied: scikit-image>=0.12 in /usr/
local/lib/python3.7/dist-packages (from lime) (0.18.3)
Requirement already satisfied: networkx>=2.0 in /usr/local/lib/
python3.7/dist-packages (from scikit-image>=0.12->lime) (2.6.3)
Requirement already satisfied: PyWavelets>=1.1.1 in /usr/local/
lib/python3.7/dist-packages (from scikit-image>=0.12->
lime) (1.3.0)
Requirement already satisfied: pillow!=7.1.0,!=7.1.1,>=4.3.0 in
/usr/local/lib/python3.7/dist-packages (from scikit-
image>=0.12->lime) (7.1.2)
Requirement already satisfied: imageio>=2.3.0 in /usr/local/
lib/python3.7/dist-packages (from scikit-image>=0.12->
lime) (2.9.0)
Requirement already satisfied: tifffile>=2019.7.26 in /usr/
local/lib/python3.7/dist-packages (from scikit-image>=0.12->
lime) (2021.11.2)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/
lib/python3.7/dist-packages (from matplotlib->lime) (1.4.4)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/
python3.7/dist-packages (from matplotlib->lime) (0.11.0)
Requirement already satisfied: python-dateutil>=2.1 in /usr/
local/lib/python3.7/dist-packages (from matplotlib->
lime) (2.8.2)
Requirement already satisfied:
pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/
python3.7/dist-packages (from matplotlib->lime) (3.0.9)
Requirement already satisfied: typing-extensions in /usr/local/
lib/python3.7/dist-packages (from kiwisolver>=1.0.1->
matplotlib->lime) (4.1.1)
7
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Requirement already satisfied: six>=1.5 in /usr/local/lib/
python3.7/dist-packages (from python-dateutil>=2.1->matplotlib->
lime) (1.15.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/
local/lib/python3.7/dist-packages (from scikit-learn>=0.18->
lime) (3.1.0)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/
python3.7/dist-packages (from scikit-learn>=0.18->lime) (1.1.0)
Building wheels for collected packages: lime
Building wheel for lime (setup.py) ... done
Created wheel for lime: filename=lime-0.2.0.1-py3-none-any.
whl size=283857 sha256=674ceb94cdcb54588f66c5d5bef5f6ae0326c7
6e645c40190408791cbe4311d5
Stored in directory: /root/.cache/pip/wheels/ca/cb/e5/
ac701e12d365a08917bf4c6171c0961bc880a8181359c66aa7
Successfully built lime
Installing collected packages: lime
Successfully installed lime-0.2.0.1
Solution
If you want to use a combination of functions from both the LIME library
and the SHAP library, then you can use the SHAPASH library. You just have
to install it, which is simple.
8
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
How It Works
Let’s take a look at the following code to install SHAPASH. This is not
available on the Anaconda distribution; the only way to install it is by
using pip.
S
olution
Since this is a Python library, you can use pip.
How It Works
Let’s take a look at the following script:
9
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
|████████████████████
████████████| 133 kB 42.7 MB/s
Requirement already satisfied: numpy>=1.9.0 in /usr/local/lib/
python3.7/dist-packages (from eli5) (1.21.6)
Requirement already satisfied: scipy in /usr/local/lib/
python3.7/dist-packages (from eli5) (1.7.3)
Requirement already satisfied: six in /usr/local/lib/python3.7/
dist-packages (from eli5) (1.15.0)
Requirement already satisfied: scikit-learn>=0.20 in /usr/
local/lib/python3.7/dist-packages (from eli5) (1.0.2)
Requirement already satisfied: graphviz in /usr/local/lib/
python3.7/dist-packages (from eli5) (0.10.1)
Requirement already satisfied: tabulate>=0.7.7 in /usr/local/
lib/python3.7/dist-packages (from eli5) (0.8.10)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/
lib/python3.7/dist-packages (from jinja2>=3.0.0->eli5) (2.0.1)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/
python3.7/dist-packages (from scikit-learn>=0.20->eli5) (1.1.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/
local/lib/python3.7/dist-packages (from scikit-learn>=0.20->
eli5) (3.1.0)
Building wheels for collected packages: eli5
Building wheel for eli5 (setup.py) ... done
Created wheel for eli5: filename=eli5-0.13.0-py2.py3-none-
any.whl size=107748 sha256=3e02d416bd1cc21aebce60420712991
9a096a92128d7d27c50be1f3a97d3b1de
Stored in directory: /root/.cache/pip/wheels/cc/3c/96/3ead31a
8e6c20fc0f1a707fde2e05d49a80b1b4b30096573be
Successfully built eli5
Installing collected packages: jinja2, eli5
10
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Attempting uninstall: jinja2
Found existing installation: Jinja2 2.11.3
Uninstalling Jinja2-2.11.3:
Successfully uninstalled Jinja2-2.11.3
ERROR: pip's dependency resolver does not currently take into
account all the packages that are installed. This behavior is
the source of the following dependency conflicts.
flask 1.1.4 requires Jinja2<3.0,>=2.10.1, but you have jinja2
3.1.2 which is incompatible.
Successfully installed eli5-0.13.0 jinja2-3.1.2
S
olution
Skater is an open-source framework to enable model interpretation for
various kinds of machine learning models. The Python-based Skater
library provides both global and local interpretations and can be installed
using pip.
How It Works
Let’s take a look at the following script:
11
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Recipe 1-6. Skope-rules Installation
Problem
You want to install Skopes-rule.
Solution
Skope-rules offers a trade-off between the interpretability of a decision tree
and the modeling power of a random forest model. The solution is simple;
you use the pip command.
How It Works
Let’s take a look at the following code:
12
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/
local/lib/python3.7/dist-packages (from pandas>=0.18.1->skope-
rules) (2.8.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/
python3.7/dist-packages (from python-dateutil>=2.7.3->
pandas>=0.18.1->skope-rules) (1.15.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/
local/lib/python3.7/dist-packages (from scikit-learn>=0.17.1->
skope-rules) (3.1.0)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/
python3.7/dist-packages (from scikit-learn>=0.17.1->skope-
rules) (0.11)
Installing collected packages: skope-rules
Successfully installed skope-rules-1.0.1
S
olution
The explainability method depends on who is the consumer of the model
output, if it is the business or senior management then the explainability
should be very simple and plain English without any mathematical
formula and if the consumer of explainability is data scientists and
machine learning engineers then the explanations may include the
mathematical formulas.
13
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
How It Works
The levels of transparency of the machine learning models can be
categorized into three buckets, as shown in Figure 1-2.
DĞƚŚŽĚƐŽĨ EĂƚƵƌĂů>ĂŶŐƵĂŐĞ'ĞŶĞƌĂƟŽŶ
džƉůĂŝŶĂďŝůŝƚLJ
dĞdžƚƵĂůdžƉůĂŝŶĂďŝůŝƚLJ
^ƵŵŵĂƌLJ'ĞŶĞƌĂƟŽŶ
sŝƐƵĂůdžƉůĂŝŶĂďŝůŝƚLJ dƌĞĞďĂƐĞĚ&ůŽǁĐŚĂƌƚ
ZƵůĞdžƚƌĂĐƟŽŶ
džĂŵƉůĞĂƐĞĚ hƐŝŶŐĐŽŵŵŽŶĞdžĂŵƉůĞƐ
ƵƐŝŶĞƐƐ^ĐĞŶĂƌŝŽƐ
14
Chapter 1 INTRODUCING EXPLAINABILITY AND SETTING UP YOUR DEVELOPMENT
ENVIRONMENT
C
onclusion
In various AI projects and initiatives, the machine learning models
generate predictions. Usually, to trust the outcomes of a model, a detailed
explanation is required. Since many people are not comfortable explaining
the machine learning model outcomes, they cannot reason out the
decisions of a model, and thereby AI adoption is restricted. Explainability
is required from regulatory stand point as well as auditing and compliance
point of view. In high-risk use cases such as medical imaging and object
detection or pattern recognition, financial prediction and fraud detection,
etc., explainability is required to explain the decisions of the machine
learning model.
In this chapter, we set up the environment by installing various
explainable AI libraries. Machine learning model interpretability and
explainability are the key focuses of this book. We are going to use Python-
based libraries, frameworks, methods, classes, and functions to explain
the models.
In the next chapter, we are going to look at the linear models.
15
CHAPTER 2
Explainability for
Linear Supervised
Models
A supervised learning model is a model that is used to train an algorithm
to map input data to output data. A supervised learning model can be of
two types: regression or classification. In a regression scenario, the output
variable is numerical, whereas with classification, the output variable is
binary or multinomial. A binary output variable has two outcomes, such as
true and false, accept and reject, yes and no, etc. In the case of multinomial
output variables, the outcome can be more than two, such as high,
medium, and low. In this chapter, we are going to use explainable libraries
to explain a regression model and a classification model, while training a
linear model.
In the classical predictive modeling scenario, a function has been
identified, and the input data is usually fit to the function to produce
the output, where the function is usually predetermined. In a modern
predictive modeling scenario, the input data and output are both shown
to a group of functions, and the machine identifies the best function that
approximates well to the output given a particular set of input. There is a
need to explain the output of machine learning and deep learning models
when performing regression and classification tasks. Linear regression and
linear classification models are simpler to explain.
Solution
A regression model on all the numeric features is trained, and then
the trained model will be passed through SHAP to generate global
explanations and local explanations.
How It Works
Let’s take a look at the following script. The Shapely value can be called
the SHAP value. It is used to explain the model. It uses the impartial
distribution of predictions from a cooperative game theory to attribute
a feature to the model’s predictions. Input features from the dataset are
considered as players in the game. The models function is considered the
rules of the game. The Shapely value of a feature is computed based on the
following steps:
18
Chapter 2 Explainability for Linear Supervised Models
If the weight of the feature is 0.000, then we can conclude that the
feature is not important and has not joined the model. If it is not equal
to 0.000, then we can conclude that the feature has a role to play in the
prediction process.
We are going to use a dataset from the UCI machine learning
repository. The URL to access the dataset is as follows:
https://archive.ics.uci.edu/ml/datasets/Appliances+energy+
prediction
The objective is to predict the appliances’ energy use in Wh, using the
features from sensors. There are 27 features in the dataset, and here we are
trying to understand what features are important in predicting the energy
usage. See Table 2-1.
19
Chapter 2 Explainability for Linear Supervised Models
20
Chapter 2 Explainability for Linear Supervised Models
import pandas as pd
df_lin_reg = pd.read_csv('https://archive.ics.uci.edu/ml/
machine-learning-databases/00374/energydata_complete.csv')
del df_lin_reg['date']
df_lin_reg.info()
df_lin_reg.columns
Index(['Appliances', 'lights', 'T1', 'RH_1', 'T2', 'RH_2',
'T3', 'RH_3', 'T4', 'RH_4', 'T5', 'RH_5', 'T6', 'RH_6', 'T7',
'RH_7', 'T8', 'RH_8', 'T9', 'RH_9', 'T_out', 'Press_mm_hg',
'RH_out', 'Windspeed', 'Visibility', 'Tdewpoint', 'rv1',
'rv2'], dtype='object')
21
Chapter 2 Explainability for Linear Supervised Models
import pandas as pd
import shap
import sklearn
print("Model coefficients:\n")
for i in range(X.shape[1]):
print(X.columns[i], "=", model.coef_[i].round(5))
Model coefficients:
lights = 1.98971
T1 = -0.60374
RH_1 = 15.15362
T2 = -17.70602
RH_2 = -13.48062
T3 = 25.4064
RH_3 = 4.92457
T4 = -3.46525
RH_4 = -0.17891
T5 = -0.02784
RH_5 = 0.14096
T6 = 7.12616
RH_6 = 0.28795
22
Chapter 2 Explainability for Linear Supervised Models
T7 = 1.79463
RH_7 = -1.54968
T8 = 8.14656
RH_8 = -4.66968
T9 = -15.87243
RH_9 = -0.90102
T_out = -10.22819
Press_mm_hg = 0.13986
RH_out = -1.06375
Windspeed = 1.70364
Visibility = 0.15368
Tdewpoint = 5.0488
rv1 = -0.02078
rv2 = -0.02078
23
Chapter 2 Explainability for Linear Supervised Models
The SHAP value for a specific feature 𝑖 is just the difference between
the expected model output and the partial dependence plot at the feature’s
value 𝑥𝑖. One of the fundamental properties of Shapley values is that they
always sum up to the difference between the game outcome when all
players are present and the game outcome when no players are present.
For machine learning models, this means that SHAP values of all the
input features will always sum up to the difference between the baseline
(expected) model output and the current model output for the prediction
being explained.
SHAP values have three objects: (a) the SHAP value for each feature,
(b) the base value, and (c) the original training data. As there are 27
features, we can expect 27 shap values.
pd.DataFrame(np.round(shap_values.values,3)).head(3)
pd.DataFrame(np.round(shap_values.data,3)).head(3)
24
Chapter 2 Explainability for Linear Supervised Models
Solution
The solution to this problem is to use the partial dependency method
(partial_dependence_plot) from the model.
How It Works
Let’s take a look at the following example. There are two ways to get the
partial dependency plot, one with a particular data point superimposed
and the other without any reference to the data point. See Figure 2-1.
25
Chapter 2 Explainability for Linear Supervised Models
shap.partial_dependence_plot(
"lights", model.predict, X, ice=False,
model_expected_value=True, feature_expected_value=True
)
26
Other documents randomly have
different content
Whilst an ordinary curse readily develops into a prayer when the
name of a god is brought in for the purpose of giving magic efficacy
to the curse, a prayer may contrariwise assume a magic character by
being addressed to a god—just as a sacrifice becomes endowed with
magic energy in consequence of its contact or communion with the
supernatural being to which it is offered; and the constraining force
in the prayer or sacrifice may then be directed even against the god
himself. But there can be little doubt that the extreme importance
which the magic element in the cult attained among the nations of
ancient civilisation was chiefly due to the prevalence of a powerful
priesthood or class of persons well versed in sacred texts. A
successful incantation presupposes a certain knowledge in him who
utters it. The words of the formulæ are fixed and may not suffer the
slightest modification under penalty of losing their potency. Right
intonation is equally important.117 The Brahmanic mantras “must be
pronounced according to certain mystic forms and with absolute
accuracy, or their efficacy is destroyed”; nay, if in the repetition of a
mantra the slightest mistake is made, either by omission of a syllable
or defective pronunciation, the calamity which it was intended to
bring down on an enemy will inevitably recoil on the head of the
repeater.118 The potency of the incantation largely lies in the voice,
which is the magical instrument par excellence.119 A Buddhist priest
who was asked what advantage he could expect to derive from
merely repeating a number of words with the sense of which he was
entirely unacquainted, gave the answer that the advantage of often
repeating the sounds was incalculable, infinite;120 and a
Muhammedan writer argues that prayers which are offered in any
other language than Arabic are profane and useless, because “the
sounds of this language”—whether understood or not—“illuminate
the darkness of men” and “purify the hearts of the faithful.”121 Ideas
of this sort are of course most strongly advocated by those who
derive the greatest profit from them—priests or scribes. And it is
easy to understand that with their increasing influence among a
superstitious and credulous people the magic significance which is so
readily ascribed to a religious act also has a tendency to grow in
importance.
117 Maspero, Études, i. 109; Idem, Dawn of Civilization, pp. 146,
213 (ancient Egyptians). Sayce, Hibbert Lectures on the Religion
of the Andent Babylonians, p. 319. Darmesteter, Ormazd et
Ahriman, p. 9. Sell, Faith of Islám, pp. 53, 79, 334, 341.
119 Yasts, iv. 5. Maspero, Études, ii. 373 sq.; Idem, Dawn of
Civilization, p. 146 (ancient Egyptians). Sell, op. cit. p. 318
(Muhammedans).
Among all sins there is none which gods resent more severely
than disobedience to their commandments. Mr. Macdonald says of
the Efatese, in the New Hebrides, that no people under the sun is
more obedient to what they regard as divine mandates than these
savages, who believe that an offence against a spiritual being means
calamity and death.122 The Chaldeans had a lively sense of the risks
entailed upon the sinner by disobedience to the gods.123 According
to the Bible disobedience was the first sin committed by man, and
death was introduced into the world as its punishment. “Rebellion is
as the sin of witchcraft, and stubbornness is as iniquity and
idolatry.”124 On the history of morals this demand of obedience has
exercised considerable influence. It gives emphasis to moral rules
which are looked upon as divine injunctions, and it helps to preserve
such rules after the conditions from which they sprang have ceased
to exist. The fact that they have become meaningless does not
render them less binding; on the contrary, the mystery surrounding
them often increases their sanctity. The commandments of a god
must be obeyed independently of their contents, simply because
disobedience to him is a sin. Acts totally different in character, crimes
of the worst description and practices by themselves perfectly
harmless, are grouped together as almost equally offensive to the
deity because they have been forbidden by him.125 And moral
progress is hampered by a number of precepts which, though rooted
in obsolete superstitions or antiquated ideas about right and wrong,
have an obstinate tendency to persist on account of their supposed
divine origin.126
122 Macdonald, Oceania, p. 201.
124 1 Samuel, xv. 23. Schultz, Old Testament Theology, ii. 286.
For other instances see Rig-Veda, vii. 89. 5; Geiger, Civilization of
the Eastern Irānians, i. p. li.; Schmidt, Die Ethik der alten
Griechen, ii. 51 sq.
At the same time it may be not only in people’s own interests, but
in the interests of their fellow men as well, for them to be on friendly
terms with supernatural beings. These beings often visit the iniquity
of fathers or forefathers upon children or descendants, or punish the
community for the sins of one of its members;128 and, on the other
hand, they reward the whole family or group for the virtues of a
single individual.129 So also, when the members of a community join
in common acts of worship, each worshipper promotes not only his
own welfare, but the welfare of his people. In early religion it is of
the utmost importance for the tribe or nation that the established
cult should be strictly observed. This is a fact which cannot be too
much emphasised when we have to explain how conduct which is
pleasing to a god has come to be regarded as a moral duty; for, if
the latest stages of religious development be excepted, the relations
between men and their gods are communal rather than individual in
character. Ahura Mazda said, “If men sacrifice unto Verethraghna,
made by Ahura, if the due sacrifice and prayer is offered unto him
just as it ought to be performed in the perfection of holiness, never
will a hostile horde enter the Aryan countries, nor any plague, nor
leprosy, nor venomous plants, nor the chariot of a foe, nor the
uplifted spear of a foe!”130 Thus the duties to gods are at the same
time social duties of the first order, owing to the intensely social
character of religious relationships.
128 Supra, i. 48 sqq.
The Maoris of New Zealand regarded their deities as the causes of pain,
misery, and death, as mighty enemies from whom nobody ever thought of
getting any aid or good, but who were to be rendered harmless by means of
charms or spells or by sacrifices offered to appease their wrath.10 The
Tahitians “supposed their gods were powerful spiritual beings, in some degree
acquainted with the events of this world, and generally governing its affairs;
never exercising any thing like benevolence towards even their most devoted
followers, but requiring homage and obedience, with constant offerings;
denouncing their anger, and dispensing destruction on all who either refused
or hesitated to comply.”11 The Fijians “formed no idea of any voluntary
kindness on the part of their gods, except the planting of wild yams, and the
wrecking of strange canoes and foreign vessels on their coast”;12 and that
some of these beings were conceived as positively wicked is indicated by the
names given them—“the adulterer,” “the rioter,” “the murderer,” and so
forth.13 The people of Aneiteum, in the New Hebrides, maintained that “earth
and air and ocean were filled with natmasses, spiritual beings, but all
malignant, who ruled over everything that affected the human race…. Their
deities, like themselves, were all selfish and malignant; they breathed no spirit
of benevolence.”14
13 Ibid. p. 185.
The Santal of India believes in no god from whose benignity he may expect
favour, but in “a multitude of demons and evil spirits, whose spite he
endeavours by supplications to avert.” Even his family god “represents the
secret principle of evil, which no bolts can shut out, and which dwells in
unseen but eternally malignant presence beside every hearth.”15 The
Kamchadales do not seem to have hoped for anything good from their deities;
Kutka himself, the creator of the universe and the greatest of the gods, was
once caught in adultery and castrated.16
According to the beliefs of the Koksoagmyut, or Hudson Bay Eskimo, all the
minor spirits are under the control of the great spirit whose name is Tung ak,
and this being “is nothing more or less than death, which ever seeks to
torment and harass the lives of people that their spirits may go to dwell with
him.”17 Nay, even the special guardian spirit by which each person is
supposed to be attended is malignant in character and ever ready to seize
upon the least occasion to work harm upon the individual whom it
accompanies; its good offices can be obtained by propitiation only.18 Among
the Nenenot, or Indians of Hudson Bay, “the rule seems to be that all spirits
are by nature bad, and must be propitiated to secure their favour.”19 Of
various Brazilian tribes we are likewise told that they do not believe in the
existence of any benevolent spirits. Thus the Coroado Indian acknowledges
only an evil principle, which sometimes meets him in the form of a lizard or a
crocodile or an ounce or a man with the feet of a stag, sometimes transforms
itself into a swamp, and leads him astray, vexes him, brings him into difficulty
and danger, and even kills him.20 The Mundrucus of the Cuparí have no notion
of a good supreme being, but believe in an evil spirit, regarded merely as a
kind of hobgoblin, who is at the bottom of all their little failures and gives
them troubles in fishing, hunting, and so forth.21 The Uaupés, says Mr.
Wallace, “appear to have no definite idea of a God…. They have much more
definite ideas of a bad spirit, ‘Juruparí,’ or Devil, whom they fear and
endeavour through their pagés [or medicine men] to propitiate. When it
thunders, they say the ‘Juruparí’ is angry, and their idea of natural death is
that the ‘Juruparí’ kills them.”22
18 Ibid. p. 194.
34 Ellis, Tshi-speaking Peoples of the Gold Coast, pp. 17, 18, 77,
92. Idem, Ew̔ e-speaking Peoples of the Slave Coast, p. 75.
Wilson, Western Africa, p. 387 (Mpongwe). Tuckey, River Zaire, p.
375. Ellis, History of Madagascar, i. 395 sq. Ratzel, History of
Mankind, i. 321 (various South Sea Islanders). Turner, Samoa, p.
17 sq. Williams and Calvert, Fiji, p. 185 sq. Inglis, op. cit. p. 30
(people of Aneiteum). Christian, Caroline Islands, p. 75. Wilken,
Het Animisme, pp. 231 sqq. (Minahassers, Macassars, and Bugis
of Celebes), 243 (Javanese). Selenka, Sonnige Welten, p. 103 sq.
(Dyaks). Forbes, Insulinde, p. 203 (natives of Tenimber). von
Brenner, Besuch bei den Kannibalen Sumatras, p. 221 (Bataks).
Mason, ‘Religion, &c. among the Karens,’ in Jour. Asiatic Soc.
Bengal, xxxiv. 196. Hunter, Annals of Rural Bengal, i. 182, 186 sq.
(Santals). Hodgson, Miscellaneous Essays, i. 128 (Bódo and
Dhimáls). Bailey, ‘Veddahs of Ceylon,’ in Trans. Ethn. Soc. N.S. ii.
301; Nevill, ‘Vaeddas of Ceylon,’ in Taprobanian, i. 194. Schmidt,
Ceylon, p. 291 sq. (Tamils). Bergmann, Nomadische Streifereien
unter den Kalmüken, iii. 182 sq. Abercromby, Pre- and Proto-
historic Finns, i. 160 (Ostiaks). Buch, ‘Die Wotjaken,’ in Acta Soc.
Scient. Fennicæ, xii. 595 sq. Castrén, Nordiska resor och
forskningar, iii. 106, 107, 174 sq. (Finnish tribes). Boas, ‘Central
Eskimo,’ in Ann. Rep. Bur. Ethn. vi. 591. Turner, ibid. xi. 193 sq.
(Hudson Bay Eskimo), 272 (Hudson Bay Indians). Hoffman,
‘Menomini Indians,’ ibid. xiv. 65. McGee, ‘Siouan Indians,’ ibid. xv.
179; Parkman, op. cit. p. lxx; Dorman, op. cit. p. 227 (North
American Indians). Müller, Geschichte der Amerikanischen
Urreligionen, pp. 72 (North American Indians), 171 (Indians of
the Great Antilles). Couto de Magalhães, Trabalho preparatorio
para aproveitamento do selvagem no Brazil—O selvagem, p. 128
sqq. Tylor, op. cit. ii. 199 sqq.
40 Supra, i. 578.
In Australia, especially in New South Wales and Victoria but also in other
parts of the continent, many of the native tribes have the notion of an “All-
father,” called Baiame, Daramulun, Mungan-ngalla, Bunjil, Nurelli, Nurundere,
or by some other name.45 He is represented as an anthropomorphic,
supernatural being and as the father of the race or the maker of everything,
who at one time dwelt on the earth but afterwards ascended to a land beyond
the sky, where he still remains. He is of a kindly disposition, and requires no
worship; in a very few cases only we meet with some faint traces of a cult
offered him.46 He is frequently believed to have instituted the initiation
ceremonies,47 and to have given the people their laws.48 Thus Nurundere is
said to have taught the Narrinyeri all the rites and ceremonies whether
connected with life or death; on inquiry why they adhere to any custom, the
reply is that Nurundere commanded it.49 At the boorah, or initiation, of the
Euahlayi tribe, Byamee is proclaimed as “Father of All, whose laws the tribes
are now obeying”; and in one of their myths he is described as the original
source of all the totems and of the law that persons of the same totem may
not intermarry.50 Bunjil taught the Kulin the arts of life, and told them to
divide themselves into two intermarrying classes so as to prevent marriages
between kindred.51 Daramulun instructed the Yuin what to do and gave them
laws which the old people have handed down from father to son to the
present time.52 And in several instances the Australian “All-father” is
represented as a guardian of morality who punishes the wicked and rewards
the good. Bunjil “very frequently sent his sons to destroy bad men and bad
women … who had killed and eaten blacks.”53 Daramulun, or Tharamulun,
who from his residence in the sky watches the actions of men, “is very angry
when they do things that they ought not to do, as when they eat forbidden
food.”54 The natives of the Herbert River, in Queensland, believe that anybody
who takes a wife from the prohibited sub-class, or who does not wear the
morning necklace for the prescribed period, or who eats forbidden food, will
sooner or later die in consequence, since his behaviour is offensive to Kohin, a
supernatural being who is supposed to have his dwelling in the Milky Way but
to roam about at night on earth as a gigantic warrior killing those whom he
meets.55 Most commonly, however, the retribution is said to come after death.
The tribes about Maryborough, in Queensland, maintain that the ghosts of
those who are good or those who have a high degree of excellence in any
particular line—fishing, hunting, fighting, dancing, and so forth—are directed
by Birral to an island in the Far North, where he resides.56 Among the Cape
River tribes, “when a Blackfellow dies whose actions during life have been
what they hold to be good, he is said to ascend to Boorala (i.e., to the
Creator, literally ‘good’), where he lives much as he did on earth, less the
usual terrestrial discomforts”; whereas to the man who has led a bad life
death is thought to be simple annihilation.57 The Kulin said that when they die
they will be subjected to a sort of trial by Binbeal, “the good being rewarded
in a better land, the bad driven away, but where they seemed to have no
idea.”58 According to another account, again, Binbeal, after he has subjected
the spirits of the deceased to an ordeal of fire to try whether they are good or
bad, liberates the good at once, whereas the bad are confined and
punished.59 The Illawarra, who lived from thirty to a hundred miles south of
Sidney, believed that when people die they are brought up to a large tree
where Mirirul, the supreme ruler, examines and judges them. The good he
takes up to the sky, the bad he sends to another place to be punished. The
women said to their children when they were naughty, “Mirirul will not allow
it.”60 Among the Wathiwathi, in New South Wales, the belief prevails that if
the spirit of a bad man escapes the traps which are set for it on its course in
the sky, it is sure to fall into the hell of fire. The good spirit, on the other
hand, is received by two old women who take care of it till it becomes
accustomed to its new abode; and after a time the great God, Tha-tha-puli,
comes with a host of spirits to see the newcomer and try his strength.61
According to a report written by Archdeacon Günther in 1839, Baiame is
supposed to like the blacks who are good; and “there is also an idea
entertained by the more thoughtful that good natives will go to Baiame when
they die.”62 Later authorities state that Baiame is believed not only to reward
the good after death, but also to punish the wicked—that is, persons who tell
lies or kill men by striking them secretly or who are unkind towards the old
and sick or, generally, who break his laws.63 A very elaborate theory of
retribution is communicated by Mr. Manning, whose notes date from 1844 or
1845. Boyma (Baiame) is said to be seated far away in the north-east on an
immense throne made of transparent crystal and standing in a great lake. He
has a son, Grogoragally, equal with him in omniscience, who acts as mediator
for the souls to the Great God. His office is to watch over the actions of
mankind and to bring to life the dead to appear before the judgment-seat of
his Father, who alone pronounces the judgment of eternal happiness in
heaven or eternal misery in a hell of everlasting fire. Women and boys dying
before the initiation, however, do not go to heaven; the men have a vague
idea that another world is reserved for them. There is also a third person, half
human, half divine, called Moodgeegally, who makes Boyma’s will known to
mankind and is the avowed enemy of all wicked people, transmitting their
misdeeds to Grogoragally.64
47 Manning, loc. cit. p. 165; Ridley, op. cit. pp. 141, 155;
Langloh Parker, op. cit. p. 7 (Boyma, Baiame, Byamee). Howitt,
op. cit. p. 495 (Daramulun). M’Kinlay, quoted ibid. p. 496. Mr.
Threlkeld says (op. cit. p. 47) that Koin, an imaginary male being
who has the appearance of a black, is supposed to precede the
coming of the natives from distant parts when they assemble to
celebrate certain of their ceremonies.
52 Ibid. p. 495.
55 Ibid. p. 499.
60 Ibid. p. 137.
63 Ridley, op. cit. pp. 135, 136, 140. Langloh Parker, op. cit. p.
70.
ebookmasss.com