Package to return sorted local feature importances for a model call
Project description
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!
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
File details
Details for the file model_interpreter-1.0.0.tar.gz
.
File metadata
- Download URL: model_interpreter-1.0.0.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 020fbdc775697af71af078d26afb8373fc18338656ffe471c05e91bae681a224 |
|
MD5 | 510aec603617f0a3589fb3a1a9e2010f |
|
BLAKE2b-256 | d5f5550222eb48b2bfa8c288b881a479b4812db26e424ff51082402f5bffe131 |
File details
Details for the file model_interpreter-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: model_interpreter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 459c4765ae1db170c0df5d8ae13443014fa8f5b3cdb438811754019dec116244 |
|
MD5 | 095c91b04ea9db433a18f7aac9af8708 |
|
BLAKE2b-256 | 846a05bfc21cb72500a511b1ffed848c107d28d3fc8385795df59e2ae5ea1cd1 |