Skip to main content

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.

Metadata

Release files for swarm-explainer 0.0.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 swarm-explainer 0.0.1
File Size Uploaded
swarm-explainer-0.0.1.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swarm-explainer 0.0.1
File Interpreter ABI Platform
swarm_explainer-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.1 kB

Release files / swarm-explainer-0.0.1.tar.gz

Download URL swarm-explainer-0.0.1.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
91bb9901e432144f0e85d631401e8f374a4497f6d03cbfcdd48153e568312dd6
BLAKE2b-256 checksum
How to use checksums
60136ca2515d09d9bda3b7d3d3b2f9eb0773d82887765e00eedf6f5b013ccba4
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / swarm_explainer-0.0.1-py3-none-any.whl

Download URL swarm_explainer-0.0.1-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bcfab0d0c4eee27397522d1ab109d0881caeb2014067ab7dc73940c7e5ec3d7e
BLAKE2b-256 checksum
How to use checksums
a781373db632a3574898e9cf36e8f9948cae2829355bc04f83ffdb57e015f5e3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.1 This release

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