Python Libraries for Interpretable Machine Learning
In the following post, I am going to give a brief guide to four of the most established packages for interpreting and explaining machine learning models.
By Rebecca Vickery, Data Scientist
As concerns regarding bias in artificial intelligence become more prominent it is becoming more and more important for businesses to be able to explain both the predictions their models are producing and how the models themselves work. Fortunately, there is an increasing number of python libraries being developed that attempt to solve this problem. In the following post, I am going to give a brief guide to four of the most established packages for interpreting and explaining machine learning models.
The following libraries are all pip installable, come with good documentation and have an emphasis on visual interpretation.
This library is essentially an extension of the scikit-learn library and provides some really useful and pretty looking visualisations for machine learning models. The visualiser objects, the core interface, are scikit-learn estimators and so if you are used to working with scikit-learn the workflow should be quite familiar.
The visualisations that can be rendered cover model selection, feature importances and model performance analysis.
Let’s walk through a few brief examples.
The library can be installed via pip.
To illustrate a few features I am going to be using a scikit-learn dataset called the wine recognition set. This dataset has 13 features and 3 target classes and can be loaded directly from the scikit-learn library. In the below code I am importing the dataset and converting it to a data frame. The data can be used in a classifier without any additional preprocessing.
I am also using scikit-learn to further split the data set into test and train.
Next, let’s use the Yellowbricks visualiser to view correlations between features in the data set.
Let’s now fit a RandomForestClassifier and evaluate the performance with another visualiser.
ELI5 is another visualisation library that is useful for debugging machine learning models and explaining the predictions they have produced. It works with the most common python machine learning libraries including scikit-learn, XGBoost and Keras.
Let’s use ELI5 to inspect the feature importance for the model we trained above.
By default the
show_weights method uses
gain to calculate the weight but you can specify other types by adding the
You can also use
show_prediction to inspect the reasons for individual predictions.
LIME (local interpretable model-agnostic explanations) is a package for explaining the predictions made by machine learning algorithms. Lime supports explanations for individual predictions from a wide range of classifiers, and support for scikit-learn is built in.
Let’s use Lime to interpret some predictions from the model we trained earlier.
Lime can be installed via pip.
First, we build the explainer. This takes a training dataset as an array, the names of the features used in the model and the names of the classes in the target variable.
Next, we create a lambda function that uses the model to predict on a sample of the data. This is borrowed from this excellent, more in-depth, tutorial on Lime.
We then use the explainer to explain the prediction on a selected example. The result is shown below. Lime produces a visualisation showing how the features have contributed to this particular prediction.
This library contains a host of helper functions for machine learning. This covers things like stacking and voting classifiers, model evaluation, feature extraction and engineering and plotting. In addition to the documentation, this paper is a good resource for a more detailed understanding of the package.
Let’s use MLxtend to compare the decision boundaries for a voting classifier against its constituent classifiers.
Again it can be installed via pip.
The imports I am using are shown below.
The following visualisation only works with two features at a time so we will first create an array containing the features
color_intensity. I have chosen these as they had the highest weighting from all the features we inspected earlier using ELI5.
Next, we create the classifiers, fit them to the training data and visualise the decision boundaries using MLxtend. The output is shown below the code.
This is by no means an exhaustive list of libraries for interpreting, visualising and explaining machine learning models. This excellent post contains a long list of other useful libraries to try out.
Thanks for reading!
Bio: Rebecca Vickery is learning data science through self study. Data Scientist @ Holiday Extras. Co-Founder of alGo.
Original. Reposted with permission.
- Opening Black Boxes: How to leverage Explainable Machine Learning
- A Data Science Playbook for explainable ML/xAI
- Command Line Basics Every Data Scientist Should Know