GithubHelp home page GithubHelp logo

idroz / lime Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thomasp85/lime

0.0 2.0 0.0 66 KB

Local Interpretable Model-Agnostic Explanations (R port of original Python package)

License: MIT License

R 100.00%

lime's Introduction

lime

Travis-CI Build Status AppVeyor Build Status CRAN_Release_Badge CRAN_Download_Badge

This is an R port of the Python lime package (https://github.com/marcotcr/lime) developed by the authors of the lime (Local Interpretable Model-agnostic Explanations) approach for black-box model explanations. All credits goes to the original developers.

The purpose of lime is to explain the predictions of black box classifiers. What this means is that for any given prediction and any given classifier it is able to determine a small set of features in the original data that has driven the outcome of the prediction. To learn more about the methodology of lime read the paper and visit the repository of the original implementation.

The lime package for R does not aim to be a line-by-line port of its Python counterpart. Instead it takes the ideas laid out in the original code and implements them in an API that is idiomatic to R.

An example

The only requirement for the classifier that lime imposes is that it must implement a predict() method accepting a type = 'prob' argument (which outputs the probability for each class). Conveniently, this covers all classifiers available through caret, and if your model of choice is not covered you have the possibility of writing your own predict method.

The following shows how a random forest model is trained on the iris data set and how lime is then used to explain a set of new observations:

library(caret)
library(lime)

# Split up the data set
iris_test <- iris[1:5, 1:4]
iris_train <- iris[-(1:5), 1:4]
iris_lab <- iris[[5]][-(1:5)]

# Create Random Forest model on iris data
model <- train(iris_train, iris_lab, method = 'rf')

# Create explanation function
explain <- lime(iris_train, model)

# Explain new observation
explanation <- explain(iris_test, n_labels = 1, n_features = 2)

# The output is provided in a nice tidy format
tibble::glimpse(explanation)
#> Observations: 10
#> Variables: 11
#> $ case            <chr> "1", "1", "2", "2", "3", "3", "4", "4", "5", "5"
#> $ label           <chr> "setosa", "setosa", "setosa", "setosa", "setos...
#> $ label_prob      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> $ model_r2        <dbl> 0.7315530, 0.7315530, 0.7410118, 0.7410118, 0....
#> $ model_intercept <dbl> 0.10223879, 0.10223879, 0.09760455, 0.09760455...
#> $ feature         <chr> "Petal.Width", "Petal.Length", "Petal.Width", ...
#> $ feature_value   <dbl> 0.2, 1.4, 0.2, 1.4, 0.2, 1.3, 0.2, 1.5, 0.2, 1.4
#> $ feature_weight  <dbl> 0.4414165, 0.4404250, 0.4447930, 0.4449105, 0....
#> $ feature_desc    <chr> "Petal.Width <= 0.4", "Petal.Length <= 1.6", "...
#> $ data            <list> [[5.1, 3.5, 1.4, 0.2], [5.1, 3.5, 1.4, 0.2], ...
#> $ prediction      <list> [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], ...

# An can be visualised directly
plot_features(explanation)

Installation

lime is still a work in progress and is thus not available on CRAN yet. In order to try it out install it directly from GitHub:

# install.packages('devtools')
devtools::install_github('thomasp85/lime')

Scope

In addition to standard tabular data the Python implementation also provides specialized explainers for text and image data. Furthermore, the article also discusses how lime can be used for explaining models rather than predictions, using an approach called submodular picks. Lastly, there's obvious extensions to the approach such as using different algorithms for the local fit step, supporting regressors in addition to classifiers, and providing additional feature selection algorithms. All of the above will hopefully find its way to this port in due time...

lime's People

Contributors

thomasp85 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.