Skip to main content

Model-agnostic explanations using feature perturbations

Project description

pypi_version pypi_downloads

SwarmExplainer

SwarmExplainer is a model-agnostic technique to explain machine learning results using visualization of feature perturbations generated by a nature-inspired algorithm. Read the preprint for further details.

Installation

You can install SwarmExplainer using pip:

pip install swarm-explainer

Usage examples

SwarmExplainer uses feature perturbations to explain how a trained model rects to changes on the features.

Training a model

In its current version, SwarmExplainer handles sklearn-based classification models.

iris = load_iris()
X, y = iris.data, iris.target
feature_names = iris.feature_names

# preprocess the dataset so feature perturbation will take effect
X = swarm_explainer.utils.preprocess(X)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
model = LogisticRegression()
model.fit(X_train, y_train)

Computing explanations

swarm = SwarmExplainer(
    max_it=100,
    N=10,
    m=X_test.shape[1],
    model=model,
    feature_names=feature_names,
    n_classes=len(np.unique(y)),
    verbose=True
)

Although there are other few parameters you can tune, to explain a model using SwarmExplainer you should control the following:

  • max_it: The number of iterations of the PSO algorithm;

  • N: The number of particles searching for the feature perturbations;

  • m: The dimensionality of the dataset;

  • model: The model to be explained;

  • feature_names: The feature names for generating the visualization;

  • n_classes: The number of classes;

  • verbose: Controls the verbosity of the technique.

Interpreting the results

After computing the explanations, you can generate graphs to interpret the results.

Suppose you want to explain class 0 in terms of the feature perturbations:

swarm.plot_importance(0, X, y)

The code produces the following plot:

Explanations for the class 0

The above visualization shows the particle weights (perturbations) and their correspondent change on the performance of the model.

  • 1. The features are organized according to their importance for the model. Most important on top;

  • 2. The optimal weight for each feature is indicated by a red dot.

  • 3. The best weights are the ones that reduce the performance and are close to one.

The visualization also shows the distribution of values to help interpreting the results, and a summary importance value.

In summary, the model learned that classify instances as in class 0 when they have high sepal width and low sepal and petal length.

Retrieving feature importance

To retrieve a numerical representation of the feature importance, SwarmExplainer offers two methods. You can retrieve the feature importance within a class or among all classes.

swarm.important_features(klass=0)
swarm.important_features()

There is a complete example in the notebooks/ folder.

Citation

Please, use the following reference to further details and to cite ClusterShapley in your work:

@misc{MarcilioJr2021_SwarmExplainer,
  title={Model-agnostic interpretation by visualization of feature perturbations},
  author={Wilson E. Marcílio-Jr and Danilo M. Eler and Fabrício Breve},
  year={2021},
  eprint={2101.10502},
  archivePrefix={arXiv},
  primaryClass={cs.LG}
}

Support

Please, if you have any questions feel free to contact me at wilson_jr at outlook dot com.

License

SwarmExplainer follows the 3-clause BSD license.

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

swarm-explainer-0.0.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

swarm_explainer-0.0.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file swarm-explainer-0.0.1.tar.gz.

File metadata

  • Download URL: swarm-explainer-0.0.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9

File hashes

Hashes for swarm-explainer-0.0.1.tar.gz
Algorithm Hash digest
SHA256 91bb9901e432144f0e85d631401e8f374a4497f6d03cbfcdd48153e568312dd6
MD5 60b5d63f5623967700e7de7e2410654d
BLAKE2b-256 60136ca2515d09d9bda3b7d3d3b2f9eb0773d82887765e00eedf6f5b013ccba4

See more details on using hashes here.

File details

Details for the file swarm_explainer-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: swarm_explainer-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9

File hashes

Hashes for swarm_explainer-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bcfab0d0c4eee27397522d1ab109d0881caeb2014067ab7dc73940c7e5ec3d7e
MD5 1d815103d368a52af6ecc66f9b44ae7b
BLAKE2b-256 a781373db632a3574898e9cf36e8f9948cae2829355bc04f83ffdb57e015f5e3

See more details on using hashes here.

Supported by

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