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 git+https://github.com/g-ohara/rlime.git
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(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
Author
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rlime-0.1.0.tar.gz.
File metadata
- Download URL: rlime-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14aec04b146a407497b8e6932d6aa9ac3660e041870a7d8c50cdcf32dce0936b
|
|
| MD5 |
263f71d02890b071608179b1804ea4bb
|
|
| BLAKE2b-256 |
1ba6d5d87faef08523a468b89c75629c217b39e03941d5c4a56400312ea5af76
|
File details
Details for the file rlime-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rlime-0.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e217d84fb43c0241dbf41c8d2336febbadc355d0cb5e8f79efd7222c80e2598
|
|
| MD5 |
6cd7710e1fbae34b39c33eae6af16a33
|
|
| BLAKE2b-256 |
3205b48dc6d6818f6abad2b3f623a26ad3cf87b9fd92eec197a7d1b0d57ce55c
|