Interpretable Machine Learning with Python

Book description

A deep and detailed dive into the key aspects and challenges of machine learning interpretability, complete with the know-how on how to overcome and leverage them to build fairer, safer, and more reliable models

Key Features

  • Learn how to extract easy-to-understand insights from any machine learning model
  • Become well-versed with interpretability techniques to build fairer, safer, and more reliable models
  • Mitigate risks in AI systems before they have broader implications by learning how to debug black-box models

Book Description

Do you want to gain a deeper understanding of your models and better mitigate poor prediction risks associated with machine learning interpretation? If so, then Interpretable Machine Learning with Python deserves a place on your bookshelf.

We’ll be starting off with the fundamentals of interpretability, its relevance in business, and exploring its key aspects and challenges.

As you progress through the chapters, you'll then focus on how white-box models work, compare them to black-box and glass-box models, and examine their trade-off. You’ll also get you up to speed with a vast array of interpretation methods, also known as Explainable AI (XAI) methods, and how to apply them to different use cases, be it for classification or regression, for tabular, time-series, image or text.

In addition to the step-by-step code, this book will also help you interpret model outcomes using examples. You’ll get hands-on with tuning models and training data for interpretability by reducing complexity, mitigating bias, placing guardrails, and enhancing reliability. The methods you’ll explore here range from state-of-the-art feature selection and dataset debiasing methods to monotonic constraints and adversarial retraining.

By the end of this book, you'll be able to understand ML models better and enhance them through interpretability tuning.

What you will learn

  • Recognize the importance of interpretability in business
  • Study models that are intrinsically interpretable such as linear models, decision trees, and Naïve Bayes
  • Become well-versed in interpreting models with model-agnostic methods
  • Visualize how an image classifier works and what it learns
  • Understand how to mitigate the influence of bias in datasets
  • Discover how to make models more reliable with adversarial robustness
  • Use monotonic constraints to make fairer and safer models

Who this book is for

This book is primarily written for data scientists, machine learning developers, and data stewards who find themselves under increasing pressures to explain the workings of AI systems, their impacts on decision making, and how they identify and manage bias. It’s also a useful resource for self-taught ML enthusiasts and beginners who want to go deeper into the subject matter, though a solid grasp on the Python programming language and ML fundamentals is needed to follow along.

Table of contents

  1. Interpretable Machine Learning with Python
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  7. Section 1: Introduction to Machine Learning Interpretation
  8. Chapter 1: Interpretation, Interpretability, and Explainability; and Why Does It All Matter?
    1. Technical requirements
    2. What is machine learning interpretation?
      1. Understanding a simple weight prediction model
    3. Understanding the difference between interpretability and explainability
      1. What is interpretability?
        1. Beware of complexity
        2. When does interpretability matter?
        3. What are black-box models?
        4. What are white-box models?
      2. What is explainability?
        1. Why and when does explainability matter?
    4. A business case for interpretability
      1. Better decisions
      2. More trusted brands
      3. More ethical
      4. More profitable
    5. Summary
    6. Image sources
    7. Further reading
  9. Chapter 2: Key Concepts of Interpretability
    1. Technical requirements
    2. The mission
      1. Details about CVD
    3. The approach
    4. Preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Data preparation
    5. Learning about interpretation method types and scopes
      1. Model interpretability method types
      2. Model interpretability scopes
      3. Interpreting individual predictions with logistic regression
    6. Appreciating what hinders machine learning interpretability
      1. Non-linearity
      2. Interactivity
      3. Non-monotonicity
    7. Mission accomplished
    8. Summary
    9. Further reading
  10. Chapter 3: Interpretation Challenges
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Data preparation
    5. Reviewing traditional model interpretation methods
      1. Predicting minutes delayed with various regression methods
      2. Classifying flights as delayed or not delayed with various classification methods
        1. Training and evaluating the classification models
      3. Visualizing delayed flights with dimensionality reduction methods
    6. Understanding limitations of traditional model interpretation methods
    7. Studying intrinsically interpretable (white-box) models
      1. Generalized Linear Models (GLMs)
        1. Linear regression
        2. Ridge regression
        3. Polynomial regression
        4. Logistic regression
      2. Decision trees
        1. CART decision trees
      3. RuleFit
      4. Nearest neighbors
        1. k-Nearest Neighbors
      5. Naïve Bayes
        1. Gaussian Naïve Bayes
    8. Recognizing the trade-off between performance and interpretability
      1. Special model properties
        1. The key property: explainability
        2. The remedial property: regularization
      2. Assessing performance
    9. Discovering newer interpretable (glass-box) models
      1. Explainable Boosting Machine (EBM)
        1. Global interpretation
        2. Local interpretation
        3. Performance
      2. Skoped Rules
        1. Global interpretation
        2. Local interpretation
        3. Performance
    10. Mission accomplished
    11. Summary
    12. Dataset sources
    13. Further reading
  11. Section 2: Mastering Interpretation Methods
  12. Chapter 4: Fundamentals of Feature Importance and Impact
    1. Technical requirements
    2. The mission
      1. Personality and birth order
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Data preparation
    5. Measuring the impact of a feature on the outcome
      1. Feature importance for tree-based models
      2. Feature importance for Logistic Regression
      3. Feature importance for LDA
      4. Feature importance for the Multi-layer Perceptron
    6. Practicing PFI
      1. Disadvantages of PFI
    7. Interpreting PDPs
      1. Interaction PDPs
      2. Disadvantages of PDP
    8. Explaining ICE plots
      1. Disadvantages of ICE
    9. Mission accomplished
    10. Summary
    11. Dataset sources
    12. Further reading
  13. Chapter 5: Global Model-Agnostic Interpretation Methods
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Data preparation
        3. Modeling and performance evaluation for deep neural networks
        4. Modeling and performance evaluation for XGBoost
    5. Learning about Shapley values
    6. Interpreting SHAP summary and dependence plots
      1. Generating SHAP summary plots
      2. Understanding interactions
      3. SHAP dependence plots
      4. SHAP force plots
    7. Accumulated Local Effects (ALE) plots
    8. Global surrogates
      1. Fitting surrogates
        1. Evaluating surrogates
        2. Interpreting surrogates
    9. Mission accomplished
    10. Summary
    11. Further reading
  14. Chapter 6: Local Model-Agnostic Interpretation Methods
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Data preparation
    5. Leveraging SHAP's KernelExplainer for local interpretations with SHAP values
      1. Training a C-SVC model
        1. Computing SHAP values using KernelExplainer
        2. Local interpretation for a group of predictions using decision plots
        3. Local interpretation for a single prediction at a time using a force plot
    6. Employing LIME
      1. What is LIME?
        1. Local interpretation for a single prediction at a time using LimeTabularExplainer
    7. Using LIME for NLP
      1. Training a LightGBM model
        1. Local interpretation for a single prediction at a time using LimeTextExplainer
    8. Trying SHAP for NLP
    9. Comparing SHAP with LIME
    10. Mission accomplished
    11. Summary
    12. Dataset sources
    13. Further reading
  15. Chapter 7: Anchor and Counterfactual Explanations
    1. Technical requirements
    2. The mission
      1. Unfair bias in recidivisim risk assessments
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Examining predictive bias with confusion matrices
        3. Data preparation
        4. Modeling
        5. Getting acquainted with our "instance of interest"
    5. Understanding anchor explanations
      1. Preparations for anchor and counterfactual explanations with alibi
      2. Local interpretations for anchor explanations
    6. Exploring counterfactual explanations
      1. Counterfactual explanations guided by prototypes
      2. Counterfactual instances and much more with the What-If Tool (WIT)
        1. Configuring WIT
        2. Datapoint editor
        3. Performance & Fairness
    7. Comparing with CEM
    8. Mission accomplished
    9. Summary
    10. Dataset sources
    11. Further reading
  16. Chapter 8: Visualizing Convolutional Neural Networks
    1. Technical requirements
    2. The mission
    3. The approach
    4. Preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. Loading the CNN model
      3. Assessing the CNN classifier with traditional interpretation methods
        1. Determining what misclassifications to focus on
    5. Visualizing the learning process with activation-based methods
      1. Intermediate activations
      2. Activation maximization
    6. Evaluating misclassifications with gradient-based attribution methods
      1. Saliency maps
      2. Grad-CAM
        1. Creating GradCam++ maps
      3. Integrated gradients
        1. Using the explainer
      4. Tying it all together
    7. Understanding classifications with perturbation-based attribution methods
      1. Occlusion sensitivity
      2. LIME's ImageExplainer
        1. 1) Initializing and creating explanations
        2. 2) Extracting an image and mask from the explanation
        3. 3) Plotting explanations
      3. CEM
      4. Tying it all together
      5. Bonus method: SHAP's DeepExplainer
    8. Mission accomplished
    9. Summary
    10. Dataset and image sources
    11. Further reading
  17. Chapter 9: Interpretation Methods for Multivariate Forecasting and Sensitivity Analysis
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparation
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Understanding the data
        3. Data preparation
        4. Loading the LSTM models
    5. Assessing time series models with traditional interpretation methods
      1. Using standard regression metrics
    6. Generating LSTM attributions with integrated gradients
    7. Computing global and local attributions with SHAP's KernelExplainer
    8. Identifying influential features with factor prioritization
      1. Computing Morris sensitivity indices
        1. Analyzing the elementary effects
    9. Quantifying uncertainty and cost sensitivity with factor fixing
      1. Generating and predicting on Salteli samples
        1. Performing Sobol sensitivity analysis
        2. Incorporating a realistic cost function
    10. Mission accomplished
    11. Summary
    12. Dataset and image sources
    13. References
  18. Section 3:Tuning for Interpretability
  19. Chapter 10: Feature Selection and Engineering for Interpretability
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
    5. Understanding the effect of irrelevant features
      1. Creating a base model
        1. Evaluating the model
        2. Training the base model at different max depths
    6. Reviewing filter-based feature selection methods
      1. Basic filter-based methods
        1. Constant features with a variance threshold
        2. Quasi-constant features with Value-Counts
        3. Duplicating features
        4. Removing unnecessary features
      2. Correlation filter-based methods
      3. Ranking filter-based methods
      4. Comparing filter-based methods
    7. Exploring embedded feature selection methods
    8. Discovering wrapper, hybrid, and advanced feature selection methods
      1. Wrapper methods
        1. Sequential forward selection (SFS)
        2. Sequential Backward Selection (SBS)
      2. Hybrid methods
        1. Recursive feature elimination
      3. Advanced methods
        1. Dimensionality reduction
        2. Model-agnostic feature importance
        3. Genetic algorithms
      4. Evaluating all feature-selected models
    9. Considering feature engineering
    10. Mission accomplished
    11. Summary
    12. Dataset sources
    13. Further reading
  20. Chapter 11: Bias Mitigation and Causal Inference Methods
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. The data dictionary
        2. Data preparation
    5. Detecting bias
      1. Visualizing dataset bias
      2. Quantifying dataset bias
      3. Quantifying model bias
    6. Mitigating bias
      1. Pre-processing bias mitigation methods
        1. Reweighing method
        2. Disparate impact remover method
      2. In-processing bias mitigation methods
        1. Prejudice remover method
        2. Gerry fair classifier method
      3. Post-processing bias mitigation methods
        1. Equalized odds post-processing method
        2. Calibrated equalized odds postprocessing method
      4. Tying it all together!
    7. Creating a causal model
      1. Understanding the results of the experiment
      2. Understanding causal models
      3. Initializing the linear doubly robust learner
      4. Fitting the causal model
    8. Understanding heterogeneous treatment effects
      1. Choosing policies
    9. Testing estimate robustness
      1. Adding random common cause
      2. Replacing treatment with a random variable
    10. Mission accomplished
    11. Summary
    12. Dataset sources
    13. Further reading
  21. Chapter 12: Monotonic Constraints and Model Tuning for Interpretability
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
        1. Verifying the sampling balance
    5. Placing guardrails with feature engineering
      1. Ordinalization
      2. Discretization
      3. Interaction terms and non-linear transformations
      4. Categorical encoding
      5. Other preparations
    6. Tuning models for interpretability
      1. Tuning a Keras neural network
        1. Defining the model and parameters to tune
        2. Running the hyperparameter tuning
        3. Examining the results
        4. Evaluating the best model
      2. Tuning other popular model classes
        1. A quick introduction to relevant model parameters
        2. Batch hyperparameter tuning models
        3. Evaluating models by precision
        4. Assessing fairness for the highest-performing model
      3. Optimizing for fairness with Bayesian hyperparameter tuning and custom metrics
        1. Designing a custom metric
        2. Running Bayesian hyperparameter tuning
        3. Fitting and evaluating a model with the best parameters
        4. Examining racial bias through feature importance
    7. Implementing model constraints
      1. Constraints for XGBoost
        1. Constraints for TensorFlow Lattice
    8. Mission accomplished
    9. Summary
    10. Dataset sources
    11. Further reading
  22. Chapter 13: Adversarial Robustness
    1. Technical requirements
    2. The mission
    3. The approach
    4. The preparations
      1. Loading the libraries
      2. Understanding and preparing the data
      3. Loading the CNN base model
      4. Assessing the CNN base classifier
    5. Learning about evasion attacks
      1. Fast Gradient Sign Method attack
        1. C&W infinity-norm attack
        2. Targeted AP attack
    6. Defending against targeted attacks with preprocessing
    7. Shielding against any evasion attack via adversarial training of a robust classifier
    8. Evaluating and certifying adversarial robustness
      1. Comparing model robustness with attack strength
      2. Certifying robustness with randomized smoothing
    9. Mission accomplished
    10. Summary
    11. Dataset sources
    12. Further reading
  23. Chapter 14: What's Next for Machine Learning Interpretability?
    1. Understanding the current landscape of ML interpretability
      1. Tying everything together!
      2. Current trends
    2. Speculating on the future of ML interpretability
      1. A new vision for ML
      2. A multidisciplinary approach
      3. Adequate standardization 
      4. Enforcing regulation
      5. Seamless machine learning automation with built-in interpretation
      6. Tighter integration with MLOps engineers
    3. Further reading
    4. Why subscribe?
  24. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Interpretable Machine Learning with Python
  • Author(s): Serg Masís
  • Release date: March 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800203907