
.. 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

        Click :ref:`here <sphx_glr_download_auto_examples_ensemble_plot_forest_importances_faces.py>`
        to download the full example code

.. 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 forests of trees to evaluate the impurity-based
importance of the pixels in an image classification task (faces).
The hotter the pixel, the more important.

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 13-49


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

.. code-block:: pytb

    Traceback (most recent call last):
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/examples/ensemble/plot_forest_importances_faces.py", line 25, in <module>
        data = fetch_olivetti_faces()
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/utils/validation.py", line 72, in inner_f
        return f(**kwargs)
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_olivetti_faces.py", line 111, in fetch_olivetti_faces
        mat_path = _fetch_remote(FACES, dirname=data_home)
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_base.py", line 1181, in _fetch_remote
        urlretrieve(remote.url, file_path)
      File "/usr/lib/python3.10/urllib/request.py", line 241, in urlretrieve
        with contextlib.closing(urlopen(url, data)) as fp:
      File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/lib/python3.10/urllib/request.py", line 519, in open
        response = self._open(req, data)
      File "/usr/lib/python3.10/urllib/request.py", line 536, in _open
        result = self._call_chain(self.handle_open, protocol, protocol +
      File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain
        result = func(*args)
      File "/usr/lib/python3.10/urllib/request.py", line 1391, in https_open
        return self.do_open(http.client.HTTPSConnection, req,
      File "/usr/lib/python3.10/urllib/request.py", line 1351, in do_open
        raise URLError(err)
    urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>






|

.. code-block:: default

    print(__doc__)

    from time import time
    import matplotlib.pyplot as plt

    from sklearn.datasets import fetch_olivetti_faces
    from sklearn.ensemble import ExtraTreesClassifier

    # Number of cores to use to perform parallel fitting of the forest model
    n_jobs = 1

    # Load the faces dataset
    data = fetch_olivetti_faces()
    X, y = data.data, data.target

    mask = y < 5  # Limit to 5 classes
    X = X[mask]
    y = y[mask]

    # Build a forest and compute the pixel importances
    print("Fitting ExtraTreesClassifier on faces data with %d cores..." % n_jobs)
    t0 = time()
    forest = ExtraTreesClassifier(n_estimators=1000,
                                  max_features=128,
                                  n_jobs=n_jobs,
                                  random_state=0)

    forest.fit(X, y)
    print("done in %0.3fs" % (time() - t0))
    importances = forest.feature_importances_
    importances = importances.reshape(data.images[0].shape)

    # Plot pixel importances
    plt.matshow(importances, cmap=plt.cm.hot)
    plt.title("Pixel importances with forests of trees")
    plt.show()


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

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


.. _sphx_glr_download_auto_examples_ensemble_plot_forest_importances_faces.py:


.. only :: html

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



  .. 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>`_
