Book Image

Interpretable Machine Learning with Python - Second Edition

By : Serg Masís
4 (4)
Book Image

Interpretable Machine Learning with Python - Second Edition

4 (4)
By: Serg Masís

Overview of this book

Interpretable Machine Learning with Python, Second Edition, brings to light the key concepts of interpreting machine learning models by analyzing real-world data, providing you with a wide range of skills and tools to decipher the results of even the most complex models. Build your interpretability toolkit with several use cases, from flight delay prediction to waste classification to COMPAS risk assessment scores. This book is full of useful techniques, introducing them to the right use case. Learn traditional methods, such as feature importance and partial dependence plots to integrated gradients for NLP interpretations and gradient-based attribution methods, such as saliency maps. In addition to the step-by-step code, you’ll get hands-on with tuning models and training data for interpretability by reducing complexity, mitigating bias, placing guardrails, and enhancing reliability. By the end of the book, you’ll be confident in tackling interpretability challenges with black-box models using tabular, language, image, and time series data.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

To get the most out of this book

  • You will need a Jupyter environment with Python 3.9+. You can do either of the following:
    • Install one on your machine locally via Anaconda Navigator or from scratch with pip.
    • Use a cloud-based one, such as Google Colaboratory, Kaggle Notebooks, Azure Notebooks, or Amazon Sagemaker.
  • The instructions on how to get started will vary accordingly, so we strongly suggest that you search online for the latest instructions for setting them up.
  • For instructions on installing the many packages employed throughout the book, please go to the GitHub repository, which will have the updated instructions in the README.MD file. We expect these to change over time, given how often packages change. We also tested the code with specific versions detailed in the README.MD, so should anything fail with later versions, please install the specific version instead.
  • Individual chapters have instructions on how to check that the right packages are installed.
  • But depending on the way Jupyter was set up, installing packages might be best done through the command line or using conda, so we suggest you adapt these installation instructions to suit your needs.
  • If you are using the digital version of this book, type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
  • If you are not a machine learning practitioner or are a beginner, it is best to read the book sequentially since many concepts are only explained in great detail in earlier chapters. Practitioners skilled in machine learning but not acquainted with interpretability can skim the first three chapters to get the ethical context and concept definitions required to make sense of the rest, but read the rest of the chapters in order. As for advanced practitioners with foundations in interpretability, reading the book in any order should be fine.
  • As for the code, you can read the book without running the code simultaneously or strictly for the theory. But if you plan to run the code, it is best to do it with the book as a guide to assist with the interpretation of outcomes and strengthen your understanding of the theory.
  • While reading the book, think of ways you could use the tools learned, and by the end of it, hopefully, you will be inspired to put this newly gained knowledge into action!

Download the example code files

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Interpretable-Machine-Learning-with-Python-2E/. In case there’s an update to the code, it will be updated on the existing GitHub repository. You can also find the hardware and software list of requirements on the repository in the README.MD file.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781803235424.

Conventions used

There are several text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter/X handles. For example: “Next, let’s define a device variable because if you have a CUDA-enabled GPU model, inference will perform quicker.”

A block of code is set as follows:

def predict(self, dataset):
    self.model.eval() 
    device = torch.device("cuda" if torch.cuda.is_available()\
                          else "cpu")
    with torch.no_grad(): 
        loader = torch.utils.data.DataLoader(dataset, batch_size = 32)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

def predict(self, dataset):
    self.model.eval()
    device = torch.device("cuda" if torch.cuda.is_available()\
                          else "cpu")
    with torch.no_grad(): 
        loader = torch.utils.data.DataLoader(dataset, batch_size = 32)

Any command-line input or output is written as follows:

pip install torch

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “The Predictions tab is selected, and this tab has a Data Table to the left where you can select and pin individual data points and a pane with Classification Results to the left.”

Warnings or important notes appear like this.

Tips and tricks appear like this.