
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/ensemble/plot_forest_importances_faces.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_ensemble_plot_forest_importances_faces.py>`
        to download the full example code or to run this example in your browser via Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_ensemble_plot_forest_importances_faces.py:


=================================================
Pixel importances with a parallel forest of trees
=================================================

This example shows the use of a forest of trees to evaluate the impurity
based importance of the pixels in an image classification task on the faces
dataset. The hotter the pixel, the more important it is.

The code below also illustrates how the construction and the computation
of the predictions can be parallelized within multiple jobs.

.. GENERATED FROM PYTHON SOURCE LINES 16-24

Loading the data and model fitting
----------------------------------
First, we load the olivetti faces dataset and limit the dataset to contain
only the first five classes. Then we train a random forest on the dataset
and evaluate the impurity-based feature importance. One drawback of this
method is that it cannot be evaluated on a separate test set. For this
example, we are interested in representing the information learned from
the full dataset. Also, we'll set the number of cores to use for the tasks.

.. GENERATED FROM PYTHON SOURCE LINES 24-26

.. code-block:: default

    from sklearn.datasets import fetch_olivetti_faces








.. GENERATED FROM PYTHON SOURCE LINES 27-29

We select the number of cores to use to perform parallel fitting of
the forest model. `-1` means use all available cores.

.. GENERATED FROM PYTHON SOURCE LINES 29-31

.. code-block:: default

    n_jobs = -1








.. GENERATED FROM PYTHON SOURCE LINES 32-33

Load the faces dataset

.. GENERATED FROM PYTHON SOURCE LINES 33-36

.. code-block:: default

    data = fetch_olivetti_faces()
    X, y = data.data, data.target








.. GENERATED FROM PYTHON SOURCE LINES 37-38

Limit the dataset to 5 classes.

.. GENERATED FROM PYTHON SOURCE LINES 38-42

.. code-block:: default

    mask = y < 5
    X = X[mask]
    y = y[mask]








.. GENERATED FROM PYTHON SOURCE LINES 43-44

A random forest classifier will be fitted to compute the feature importances.

.. GENERATED FROM PYTHON SOURCE LINES 44-50

.. code-block:: default

    from sklearn.ensemble import RandomForestClassifier

    forest = RandomForestClassifier(n_estimators=750, n_jobs=n_jobs, random_state=42)

    forest.fit(X, y)






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <style>#sk-container-id-24 {color: black;}#sk-container-id-24 pre{padding: 0;}#sk-container-id-24 div.sk-toggleable {background-color: white;}#sk-container-id-24 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-24 label.sk-toggleable__label-arrow:before {content: "▸";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-24 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-24 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-24 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-24 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-24 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-24 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: "▾";}#sk-container-id-24 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-24 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-24 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-24 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-24 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-24 div.sk-parallel-item::after {content: "";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-24 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-24 div.sk-serial::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-24 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-24 div.sk-item {position: relative;z-index: 1;}#sk-container-id-24 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-24 div.sk-item::before, #sk-container-id-24 div.sk-parallel-item::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-24 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-24 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-24 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-24 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-24 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-24 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-24 div.sk-label-container {text-align: center;}#sk-container-id-24 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-24 div.sk-text-repr-fallback {display: none;}</style><div id="sk-container-id-24" class="sk-top-container"><div class="sk-text-repr-fallback"><pre>RandomForestClassifier(n_estimators=750, n_jobs=-1, random_state=42)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class="sk-container" hidden><div class="sk-item"><div class="sk-estimator sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-104" type="checkbox" checked><label for="sk-estimator-id-104" class="sk-toggleable__label sk-toggleable__label-arrow">RandomForestClassifier</label><div class="sk-toggleable__content"><pre>RandomForestClassifier(n_estimators=750, n_jobs=-1, random_state=42)</pre></div></div></div></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 51-61

Feature importance based on mean decrease in impurity (MDI)
-----------------------------------------------------------
Feature importances are provided by the fitted attribute
`feature_importances_` and they are computed as the mean and standard
deviation of accumulation of the impurity decrease within each tree.

.. warning::
    Impurity-based feature importances can be misleading for **high
    cardinality** features (many unique values). See
    :ref:`permutation_importance` as an alternative.

.. GENERATED FROM PYTHON SOURCE LINES 61-77

.. code-block:: default

    import time

    import matplotlib.pyplot as plt

    start_time = time.time()
    img_shape = data.images[0].shape
    importances = forest.feature_importances_
    elapsed_time = time.time() - start_time

    print(f"Elapsed time to compute the importances: {elapsed_time:.3f} seconds")
    imp_reshaped = importances.reshape(img_shape)
    plt.matshow(imp_reshaped, cmap=plt.cm.hot)
    plt.title("Pixel importances using impurity values")
    plt.colorbar()
    plt.show()




.. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_forest_importances_faces_001.png
   :alt: Pixel importances using impurity values
   :srcset: /auto_examples/ensemble/images/sphx_glr_plot_forest_importances_faces_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Elapsed time to compute the importances: 0.076 seconds




.. GENERATED FROM PYTHON SOURCE LINES 78-79

Can you still recognize a face?

.. GENERATED FROM PYTHON SOURCE LINES 81-90

The limitations of MDI is not a problem for this dataset because:

 1. All features are (ordered) numeric and will thus not suffer the
    cardinality bias
 2. We are only interested to represent knowledge of the forest acquired
    on the training set.

If these two conditions are not met, it is recommended to instead use
the :func:`~sklearn.inspection.permutation_importance`.


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.207 seconds)


.. _sphx_glr_download_auto_examples_ensemble_plot_forest_importances_faces.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example


    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.3.X?urlpath=lab/tree/notebooks/auto_examples/ensemble/plot_forest_importances_faces.ipynb
        :alt: Launch binder
        :width: 150 px



    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_forest_importances_faces.py <plot_forest_importances_faces.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_forest_importances_faces.ipynb <plot_forest_importances_faces.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
