Interpretable Machine Learning with Python: Build explainable, fair and robust high-performance models [Second Edition] 9781803235424

A deep dive into the key aspects and challenges of machine learning interpretability using a comprehensive toolkit, incl

1,095 107 44MB

English Pages 607 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Interpretable Machine Learning with Python: Build explainable, fair and robust high-performance models [Second Edition]
 9781803235424

Table of contents :
Preface
Who this book is for
What this book covers
To get the most out of this book
Get in touch
Interpretation, Interpretability, and Explainability; and Why Does It All Matter?
Technical requirements
What is machine learning interpretation?
Understanding a simple weight prediction model
Understanding the difference between interpretability and explainability
What is interpretability?
Beware of complexity
When does interpretability matter?
What are black-box models?
What are white-box models?
What is explainability?
Why and when does explainability matter?
A business case for interpretability
Better decisions
More trusted brands
More ethical
More profitable
Summary
Image sources
Dataset sources
Further reading
Key Concepts of Interpretability
Technical requirements
The mission
Details about CVD
The approach
Preparations
Loading the libraries
Understanding and preparing the data
The data dictionary
Data preparation
Interpretation method types and scopes
Model interpretability method types
Model interpretability scopes
Interpreting individual predictions with logistic regression
Appreciating what hinders machine learning interpretability
Non-linearity
Interactivity
Non-monotonicity
Mission accomplished
Summary
Further reading
Interpretation Challenges
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
The data dictionary
Data preparation
Reviewing traditional model interpretation methods
Predicting minutes delayed with various regression methods
Classifying flights as delayed or not delayed with various classification methods
Training and evaluating the classification models
Understanding limitations of traditional model interpretation methods
Studying intrinsically interpretable (white-box) models
Generalized Linear Models (GLMs)
Linear regression
Ridge regression
Polynomial regression
Logistic regression
Decision trees
CART decision trees
RuleFit
Interpretation and feature importance
Nearest neighbors
k-Nearest Neighbors
Naïve Bayes
Gaussian Naïve Bayes
Recognizing the trade-off between performance and interpretability
Special model properties
The key property: explainability
The remedial property: regularization
Assessing performance
Discovering newer interpretable (glass-box) models
Explainable Boosting Machine (EBM)
Global interpretation
Local interpretation
Performance
GAMI-Net
Global interpretation
Local interpretation
Performance
Mission accomplished
Summary
Dataset sources
Further reading
Global Model-Agnostic Interpretation Methods
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Data preparation
Model training and evaluation
What is feature importance?
Assessing feature importance with model-agnostic methods
Permutation feature importance
SHAP values
Comprehensive explanations with KernelExplainer
Faster explanations with TreeExplainer
Visualize global explanations
SHAP bar plot
SHAP beeswarm plot
Feature summary explanations
Partial dependence plots
SHAP scatter plot
ALE plots
Feature interactions
SHAP bar plot with clustering
2D ALE plots
PDP interactions plots
Mission accomplished
Summary
Further reading
Local Model-Agnostic Interpretation Methods
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
The data dictionary
Data preparation
Leveraging SHAP’s KernelExplainer for local interpretations with SHAP values
Training a C-SVC model
Computing SHAP values using KernelExplainer
Local interpretation for a group of predictions using decision plots
Local interpretation for a single prediction at a time using a force plot
Employing LIME
What is LIME?
Local interpretation for a single prediction at a time using LimeTabularExplainer
Using LIME for NLP
Training a LightGBM model
Local interpretation for a single prediction at a time using LimeTextExplainer
Trying SHAP for NLP
Comparing SHAP with LIME
Mission accomplished
Summary
Dataset sources
Further reading
Anchors and Counterfactual Explanations
Technical requirements
The mission
Unfair bias in recidivism risk assessments
The approach
The preparations
Loading the libraries
Understanding and preparing the data
The data dictionary
Examining predictive bias with confusion matrices
Data preparation
Modeling
Getting acquainted with our “instance of interest”
Understanding anchor explanations
Preparations for anchor and counterfactual explanations with alibi
Local interpretations for anchor explanations
Exploring counterfactual explanations
Counterfactual explanations guided by prototypes
Counterfactual instances and much more with WIT
Configuring WIT
Datapoint editor
Performance & Fairness
Mission accomplished
Summary
Dataset sources
Further reading
Visualizing Convolutional Neural Networks
Technical requirements
The mission
The approach
Preparations
Loading the libraries
Understanding and preparing the data
Data preparation
Inspect data
The CNN models
Load the CNN model
Assessing the CNN classifier with traditional interpretation methods
Determining what misclassifications to focus on
Visualizing the learning process with activation-based methods
Intermediate activations
Evaluating misclassifications with gradient-based attribution methods
Saliency maps
Guided Grad-CAM
Integrated gradients
Bonus method: DeepLIFT
Tying it all together
Understanding classifications with perturbation-based attribution methods
Feature ablation
Occlusion sensitivity
Shapley value sampling
KernelSHAP
Tying it all together
Mission accomplished
Summary
Further reading
Interpreting NLP Transformers
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
The data dictionary
Loading the model
Visualizing attention with BertViz
Plotting all attention with the model view
Diving into layer attention with the head view
Interpreting token attributions with integrated gradients
LIME, counterfactuals, and other possibilities with the LIT
Mission accomplished
Summary
Further reading
Interpretation Methods for Multivariate Forecasting and Sensitivity Analysis
Technical requirements
The mission
The approach
The preparation
Loading the libraries
Understanding and preparing the data
The data dictionary
Understanding the data
Data preparation
Loading the LSTM model
Assessing time series models with traditional interpretation methods
Using standard regression metrics
Predictive error aggregations
Evaluating the model like a classification problem
Generating LSTM attributions with integrated gradients
Computing global and local attributions with SHAP’s KernelExplainer
Why use KernelExplainer?
Defining a strategy to get it to work with a multivariate time series model
Laying the groundwork for the permutation approximation strategy
Computing the SHAP values
Identifying influential features with factor prioritization
Computing Morris sensitivity indices
Analyzing the elementary effects
Quantifying uncertainty and cost sensitivity with factor fixing
Generating and predicting on Saltelli samples
Performing Sobol sensitivity analysis
Incorporating a realistic cost function
Mission accomplished
Summary
Dataset and image sources
Further reading
Feature Selection and Engineering for Interpretability
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
Understanding the effect of irrelevant features
Creating a base model
Evaluating the model
Training the base model at different max depths
Reviewing filter-based feature selection methods
Basic filter-based methods
Constant features with a variance threshold
Quasi-constant features with value_counts
Duplicating features
Removing unnecessary features
Correlation filter-based methods
Ranking filter-based methods
Comparing filter-based methods
Exploring embedded feature selection methods
Discovering wrapper, hybrid, and advanced feature selection methods
Wrapper methods
Sequential forward selection (SFS)
Hybrid methods
Recursive Feature Elimination (RFE)
Advanced methods
Model-agnostic feature importance
Genetic algorithms
Evaluating all feature-selected models
Considering feature engineering
Mission accomplished
Summary
Dataset sources
Further reading
Bias Mitigation and Causal Inference Methods
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
The data dictionary
Data preparation
Detecting bias
Visualizing dataset bias
Quantifying dataset bias
Quantifying model bias
Mitigating bias
Preprocessing bias mitigation methods
The Reweighing method
The disparate impact remover method
In-processing bias mitigation methods
The exponentiated gradient reduction method
The gerry fair classifier method
Post-processing bias mitigation methods
The equalized odds post-processing method
The calibrated equalized odds postprocessing method
Tying it all together!
Creating a causal model
Understanding the results of the experiment
Understanding causal models
Initializing the linear doubly robust learner
Fitting the causal model
Understanding heterogeneous treatment effects
Choosing policies
Testing estimate robustness
Adding a random common cause
Replacing the treatment variable with a random variable
Mission accomplished
Summary
Dataset sources
Further reading
Monotonic Constraints and Model Tuning for Interpretability
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
Verifying the sampling balance
Placing guardrails with feature engineering
Ordinalization
Discretization
Interaction terms and non-linear transformations
Categorical encoding
Other preparations
Tuning models for interpretability
Tuning a Keras neural network
Defining the model and parameters to tune
Running the hyperparameter tuning
Examining the results
Evaluating the best model
Tuning other popular model classes
A quick introduction to relevant model parameters
Batch hyperparameter tuning models
Evaluating models by precision
Assessing fairness for the highest-performing model
Optimizing for fairness with Bayesian hyperparameter tuning and custom metrics
Designing a custom metric
Running Bayesian hyperparameter tuning
Fitting and evaluating a model with the best parameters
Examining racial bias through feature importance
Implementing model constraints
Constraints for XGBoost
Setting regularization and constraint parameters
Training and evaluating the constrained model
Examining constraints
Constraints for TensorFlow Lattice
Initializing the model and Lattice inputs
Building a Keras model with TensorFlow Lattice layers
Training and evaluating the model
Mission accomplished
Summary
Dataset sources
Further reading
Adversarial Robustness
Technical requirements
The mission
The approach
The preparations
Loading the libraries
Understanding and preparing the data
Loading the CNN base model
Assessing the CNN base classifier
Learning about evasion attacks
Fast gradient sign method attack
Carlini and Wagner infinity norm attack
Targeted adversarial patch attack
Defending against targeted attacks with preprocessing
Shielding against any evasion attack by adversarial training of a robust classifier
Evaluating adversarial robustness
Comparing model robustness with attack strength
Mission accomplished
Summary
Dataset sources
Further reading
What’s Next for Machine Learning Interpretability?
Understanding the current landscape of ML interpretability
Tying everything together!
Current trends
Speculating on the future of ML interpretability
A new vision for ML
A multidisciplinary approach
Adequate standardization
Enforcing regulation
Seamless machine learning automation with built-in interpretation
Tighter integration with MLOps engineers
Summary
Further reading
Other Books You May Enjoy
Index

Polecaj historie