Skip to main content

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

Metadata

Release files for rlime 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rlime 0.1.1
File Size Uploaded
rlime-0.1.1.tar.gz 10.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for rlime 0.1.1
File Interpreter ABI Platform
rlime-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 MB

Release files / rlime-0.1.1.tar.gz

Download URL rlime-0.1.1.tar.gz
Size 10.4 MB
Tags Source
SHA-256 checksum
How to use checksums
43838d3fd73db1e0f0bd7b02ce544ec5518b9629b87e2cd2497250bf14cc4028
BLAKE2b-256 checksum
How to use checksums
6d986d5753f11cce41c5bddeba88e6c4b10984abb84e488f3e4e46334f9b3f6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / rlime-0.1.1-py3-none-any.whl

Download URL rlime-0.1.1-py3-none-any.whl
Size 10.8 MB
Tags Python 3
SHA-256 checksum
How to use checksums
02e11022c8c54140e73fdd0be21b21b53727e4e93a1c34ad67b7d022e8a74c3d
BLAKE2b-256 checksum
How to use checksums
60b3f89d6980b65ba8985e17ade9b0841d36cd9e874005963187ecf6e690d98c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page