.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_sphinx_gallery_auto_transparency_xmpl_transparency_lime.py: ==================== Using LIME Explainer ==================== This example illustrates how to use the LIME tabular explainer to explain a prediction. This example shows how to use the tabular LIME implementation -- :class:`fatf.transparency.predictions.surrogate_explainers.TabularBlimeyLime` -- to explain a prediction of a black-box probabilistic model. .. image:: /sphinx_gallery_auto/transparency/images/sphx_glr_xmpl_transparency_lime_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'setosa': {'*petal length (cm)* <= 1.60': 0.9307866199252784, '*petal width (cm)* <= 0.30': 0.04259129805843401, '*sepal length (cm)* <= 5.10': -0.0027663756624473936, '3.00 < *sepal width (cm)* <= 3.30': -0.011263216273426888}, 'versicolor': {'*petal length (cm)* <= 1.60': -0.5593822883064358, '*petal width (cm)* <= 0.30': 0.07065024115814264, '*sepal length (cm)* <= 5.10': 0.025538640983435418, '3.00 < *sepal width (cm)* <= 3.30': 0.0663293593939103}, 'virginica': {'*petal length (cm)* <= 1.60': -0.37140433161884245, '*petal width (cm)* <= 0.30': -0.11324153921657666, '*sepal length (cm)* <= 5.10': -0.022772265320988014, '3.00 < *sepal width (cm)* <= 3.30': -0.05506614312048342}} | .. code-block:: default # Author: Kacper Sokol # License: new BSD from pprint import pprint import fatf import fatf.utils.data.datasets as fatf_datasets import fatf.utils.models as fatf_models import fatf.transparency.predictions.surrogate_explainers as fatf_surrogates import fatf.vis.lime as fatf_vis_lime print(__doc__) # Fix random seed fatf.setup_random_seed(42) # Load data iris_data_dict = fatf_datasets.load_iris() iris_X = iris_data_dict['data'] iris_y = iris_data_dict['target'] iris_feature_names = iris_data_dict['feature_names'].tolist() iris_class_names = iris_data_dict['target_names'].tolist() # Train a model clf = fatf_models.KNN() clf.fit(iris_X, iris_y) # Create a LIME explainer lime = fatf_surrogates.TabularBlimeyLime( iris_X, clf, feature_names=iris_feature_names, class_names=iris_class_names) # Choose an index of the instance to be explained index_to_explain = 42 # Explain an instance lime_explanation = lime.explain_instance( iris_X[index_to_explain, :], samples_number=500) # Display the textual explanation pprint(lime_explanation) # Plot the explanation fatf_vis_lime.plot_lime(lime_explanation) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.705 seconds) .. _sphx_glr_download_sphinx_gallery_auto_transparency_xmpl_transparency_lime.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: https://mybinder.org/badge_logo.svg :target: https://mybinder.org/v2/gh/fat-forensics/fat-forensics-doc/master?filepath=notebooks/sphinx_gallery_auto/transparency/xmpl_transparency_lime.ipynb :width: 150 px .. container:: sphx-glr-download :download:`Download Python source code: xmpl_transparency_lime.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: xmpl_transparency_lime.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_