Skip to main content

Implementation of R-LIME, a novel method for interpreting the behavior of ML models.

Project description

R-LIME

R-LIME is a novel method that explains behavior of black-box classifiers such as deep neural networks or ensemble models. It linearly approximizes a decision boundary of the black-box classifier in a local rectangular region, and maximizes the region as long as the approximation accuracy is higher than a given threshold. Then, it provides contribution of each feature to the prediction and rule that restricted the approximation region.

Installation

Install the package from this repo:

pip install rlime

Usage

Example code:

from rlime.rlime import HyperParam, explain_instance
from rlime.utils import load_dataset, get_trg_sample
from sklearn.ensemble import RandomForestClassifier

# Load dataset and sample a focal point
dataset = load_dataset("recidivism", balance=True)
focal_point, _, _ = get_trg_sample(0, dataset)

# Train black-box model (random forest)
black_box = RandomForestClassifier(n_jobs=-1)
black_box.fit(dataset.train, dataset.labels_train)

# Generate explanation
hyper_param = HyperParam()
hyper_param.tau=0.70
rlime_exp = explain_instance(focal_point, dataset, black_box.predict, hyper_param)

# Print generated explanation if found
if rlime_exp is None:
  print("No explanation found")
else:
  rule, arm = rlime_exp
  print(f"Rule: {rule}")
  print("Weights:")
  weights = arm.surrogate_model["LogisticRegression"].weights.values()
  sum_weights = sum([abs(w) for w in weights])
  weights = [w / sum_weights for w in weights]
  top_5 = sorted(enumerate(weights), key=lambda x: abs(x[1]), reverse=True)[:5]
  for i, w in top_5:
    print(f"  {dataset.feature_names[i]:^16}: {w:+.4f}")
  print(f"Accuracy: {arm.n_rewards / arm.n_samples:.4f}")
  print(f"Coverage: {arm.coverage:.4f}")

You will get output:

Rule: ['Priors = 1']
Weights:
  PrisonViolations: +0.1789
        Age       : -0.1526
    MonthsServed  : +0.1267
        Race      : -0.1191
      Married     : -0.1010
Accuracy: 0.7054
Coverage: 0.4396

Licence

MIT

Author

Genji Ohara

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rlime-0.1.1.tar.gz (10.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rlime-0.1.1-py3-none-any.whl (10.8 MB view details)

Uploaded Python 3

File details

Details for the file rlime-0.1.1.tar.gz.

File metadata

  • Download URL: rlime-0.1.1.tar.gz
  • Upload date:
  • Size: 10.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for rlime-0.1.1.tar.gz
Algorithm Hash digest
SHA256 43838d3fd73db1e0f0bd7b02ce544ec5518b9629b87e2cd2497250bf14cc4028
MD5 3612da3927f2f1e9f2234d3f9f2d3eb5
BLAKE2b-256 6d986d5753f11cce41c5bddeba88e6c4b10984abb84e488f3e4e46334f9b3f6b

See more details on using hashes here.

File details

Details for the file rlime-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rlime-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for rlime-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02e11022c8c54140e73fdd0be21b21b53727e4e93a1c34ad67b7d022e8a74c3d
MD5 79df48eadc3859afa98e3070cb3d1f1d
BLAKE2b-256 60b3f89d6980b65ba8985e17ade9b0841d36cd9e874005963187ecf6e690d98c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page