Data ScienceMachine LearningPython

LIME: How to Interpret Machine Learning Models With Python

Black-box models aren’t cool anymore. It’s easy to build great models nowadays, but what’s going on inside? That’s what Explainable AI and LIME try to uncover. 

Don’t feel like reading? Check out my video on the topic:

Knowing why the model makes predictions the way it does is essential for tweaking. Just think about it – if you don’t know what’s going on inside, how the hell will you improve it? 

LIME isn’t the only option for machine learning model interpretation. The alternative is SHAP. You can learn more about it here:

SHAP: How to Interpret Machine Learning Models With Python

Today we also want to train the model ASAP and focus on interpretation. Because of that, the identical dataset and modeling process is used.

After reading this article, you shouldn’t have any problems with explainable machine learning. Interpreting models and the importance of each predictor should become second nature.

The article is structured as follows:

What is LIME?

The acronym LIME stands for Local Interpretable Model-agnostic Explanations. The project is about explaining what machine learning models are doing (source). LIME supports explanations for tabular models, text classifiers, and image classifiers (currently).

To install LIME, execute the following line from the Terminal:

pip install lime

In a nutshell, LIME is used to explain predictions of your machine learning model. The explanations should help you to understand why the model behaves the way it does. If the model isn’t behaving as expected, there’s a good chance you did something wrong in the data preparation phase.

You’ll now train a simple model and then begin with the interpretations.

Model training

You can’t interpret a model before you train it, so that’s the first step. The Wine quality dataset is easy to train on and comes with a bunch of interpretable features. Here’s how to load it into Python:

The first couple of rows look like this:

Wine quality dataset head

Image 1 – Wine quality dataset head (image by author)

All attributes are numeric, and there are no missing values, so you can cross data preparation from the list. 

Train/Test split is the next step. The column quality is the target variable, with possible values of good and bad. Set the random_state parameter to 42 if you want to get the same split:

Model training is the only thing left to do. RandomForestClassifier from ScikitLearn will do the job, and you’ll have to fit it on the training set. You’ll get an 80% accurate classifier out of the box (score):

 

And that’s all you need to start with model interpretation. You’ll learn how in the next section.

Model interpretation

To start explaining the model, you first need to import the LIME library and create a tabular explainer object. It expects the following parameters:

  • training_data – our training data generated with train/test split. It must be in a Numpy array format.
  • feature_names – column names from the training set
  • class_names – distinct classes from the target variable
  • mode – type of problem you’re solving (classification in this case)

Here’s the code:

And that’s it – you can start interpreting! A bad wine comes in first. The second row of the test set represents wine classified as bad. You can call the explain_instance function of the explainer object to, well, explain the prediction. The following parameters are required:

  • data_row – a single observation from the dataset
  • predict_fn – a function used to make predictions. The predict_proba from the model is a great option because it shows probabilities

Here’s the code:

The show_in_notebook function shows the prediction interpretation in the notebook environment:

Interpretation for a bad wine

Image 2 – LIME interpretation for a bad wine (image by author)

The model is 81% confident this is a bad wine. The values of alcoholsulphates, and total sulfur dioxide increase wine’s chance to be classified as bad. The volatile acidity is the only one that decreases it.

Let’s take a look at a good wine next. You can find one at the fifth row of the test set:

Here’s the corresponding interpretation:

Interpretation for a good wine

Image 3 – LIME interpretation for a good wine (image by author)

Now that’s the wine I’d like to try. The model is 100% confident it’s a good wine, and the top three predictors show it. 

That’s how LIME works in a nutshell. There are different visualizations available, and you are not limited to interpreting only a single instance, but this is enough to get you started. Let’s wrap things up in the next section.

Conclusion

Interpreting machine learning models is simple. It provides you with a great way of explaining what’s going on below the surface to non-technical folks. You don’t have to worry about data visualization, as the LIME library handles that for you.

This article should serve you as a basis for more advanced interpretations and visualizations. You can always learn further on your own.

What are your thoughts on LIME? Do you want to see a comparison between LIME and SHAP? Please let me know.

Join my private email list for more helpful insights.

Dario Radečić
Data scientist, blogger, and enthusiast. Passionate about deep learning, computer vision, and data-driven decision making.

You may also like

26 Comments

  1. Would love to perpetully get updated outstanding web blog!

  2. Fabulous, what a web site it is! This web siute provides useful
    data to us, keep it up.

  3. I’m very happy to discover this page. I need to to thank you for
    ones time just for this fantastic read! I definitely really liked every little bit of it
    and I have you bookmarked to check out new things in your blog.

  4. There is visibly a bunch to realize about this. I suppose you made certain good points in features also. Devondra Cart Vittoria

  5. I am really grateful to the owner of this web page who has shared this fantastic piece of writing at at this time. Addy Ransell Latt

  6. I got what you intend,bookmarked, very decent website. Golda Torin Felise

  7. Good post. I will want some time to absorb your article.. Janetta Nowell Drooff

  8. Muchos Gracias for your article post. Really looking forward to read more. Keep writing. Muire Gregor Artina

  9. I am sure this post has touched all the internet people, its really really fastidious article on building up new website. Tate Wainwright Sheaff

  10. I believe this presents really good reading material. You make the material interesting and entertaining. Laney Grantley Theran

  11. I enjoyed reading what you had to say. Great post! Great post! This information is magnificent. Susanetta Karlens Ware

  12. Yes! Finally someone writes about Massage In Mumbai. Alverta Ruttger Calendra

  13. Say, you got a nice blog. Really thank you! Want more. Freddi Isiahi Delaine

  14. This piece of writing presents clear idea for the new users of blogging, that really how to do blogging. Albertine John Guild

  15. I consider something really special in this website. Caria Hadrian Treva

  16. Wow, this article is fastidious, my sister is analyzing such things, therefore I am going to convey her. Zara Tannie Wichman

  17. Excellent article! We will be linking to this particularly great post on our site. Keep up the great writing. Leshia Duane Melburn

  18. Wohh precisely what I was searching for, thank you for posting. Valentine Cull Rama

  19. Greetings I am so glad I found your web site, I really found you by mistake, while I was browsing on Aol for something else, Anyhow I am here now and would just like to say thanks for a fantastic post and a all round interesting blog (I also love the theme/design), I don’t have time to
    read through it all at the minute but I have bookmarked it and also added
    in your RSS feeds, so when I have time I will be back to read
    a lot more, Please do keep up the fantastic jo.

  20. These are genuinely enormous ideas in concerning blogging.
    You have touched some pleasant factors here. Any way keep up wrinting.

  21. Everything is very open with a very clear description of the issues.
    It was really informative. Your site is very helpful.

    Thank you for sharing!

  22. Good info. Lucky me I discovered your blog by
    chance (stumbleupon). I’ve saved it for later!

  23. This paragraph will help the internet viewers for building
    up new web site or even a weblog from start to end.

  24. Some truly good content on this internet site , thanks for contribution. Kizzie Tam Standley

  25. Exсellent blog! Ɗo you have any recommendations for aѕpiring
    wгiters? I’m planning to stаrt my own blog soon but I’m a little lost on everything.
    Would you suggest starting with a freе platfοrm like Wordpreѕs or go for a paid option? There are so many options out tһeгe that I’m completely confused ..
    Any suggeѕtіons? Apprecіаte it!

    1. Hi,
      it depends what your goal is. If you want to build a brand, go with WordPress. You can set it up in a weekend if you have no prior experience.
      If you want to earn some money on the side, try Medium – it’s also free.
      Or, combine both – that seems like the best option.

Comments are closed.

More in Data Science