MODEL INTERPRETER
model_interpreter returns feature importance values for a single row model prediction with functionality to:
- handle regression and binary / multiclass classification models
- sort features by importance
- map feature names to more interpretable names
- aggregate feature importance's across features
- handle categorical data within the input features
'model_interpreter' uses SHAP to calculate the single row feature importance values.
The package tries to fit one of the three SHAP explainers below in the following order;
- TreeExplainer
- LinearExplainer
- KernelExplainer
Here is a simple example of generating single row feature importance's for a classification model;
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from model_interpreter.interpreter import ModelInterpreter
# generate a classification dataset
X, y = make_classification(
n_samples=1000,
n_features=4, n_informative=2,
n_redundant=0, random_state=0,
shuffle=False
)
# fit a model
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)
# fit model interpreter to model
feature_names = ["feature1", "feature2", "feature3", "feature4"]
model_interpreter = ModelInterpreter(feature_names)
model_interpreter.fit(clf)
# return feature contribution importances for a single row
single_row = X.head(1)
contribution_list = single_model_contribution.transform(
single_row, return_type="name_value_dicts"
)
print(contribution_list)
Which will return the following output:
[{'Name': 'feature2', 'Value': -0.349129583}, {'Name': 'feature1', 'Value': -0.0039231513}, {'Name': 'feature4', 'Value': 0.0031653932}, {'Name': 'feature3', 'Value': 0.0013787609}]
Installation
The easiest way to get model_interpreter is directly from pypi with;
pip install model_interpreter
Examples
To help get started there are example notebooks in the examples folder in the repo that show how to use each transformer.
To open the example notebooks in binder click here or click on the launch binder shield above and then click on the directory button in the side bar to the left to navigate to the specific notebook.
Issues
For bugs and feature requests please open an issue.
Build and test
The test framework we are using for this project is pytest. To build the package locally and run the tests follow the steps below.
First clone the repo and move to the root directory;
git clone https://github.com/lvgig/model_interpreter.git
cd model_interpreter
Next install model_interpreter and development dependencies;
pip install . -r requirements-dev.txt
Finally run the test suite with pytest;
pytest
Contribute
model_interpreter is under active development, we're super excited if you're interested in contributing!
Metadata
Release files for model-interpreter 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| model_interpreter-1.1.0.tar.gz | 14.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| model_interpreter-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.1 kB
Release files / model_interpreter-1.1.0.tar.gz
| Download URL | model_interpreter-1.1.0.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9aa24cb02ea2575fb0de534fc0c0b68de7f6ec2920eee5b310984d66f031bf8f
|
|
BLAKE2b-256 checksum How to use checksums |
b379c329bf370940068f94377b9568060b76531696600999ef2d3578b59fd566
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2024.
Transparency logRelease files / model_interpreter-1.1.0-py3-none-any.whl
| Download URL | model_interpreter-1.1.0-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
19293609df282be588779f3cb3f9b727f891557c4a0183363a8067e567fdf13e
|
|
BLAKE2b-256 checksum How to use checksums |
172fed44f1a43c3f8e60d8ced8148148e5db92c2c093a63e2b2203df7e48d761
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2024.
Transparency logSecurity for model-interpreter 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for model_interpreter-1.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2024.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.