
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/decomposition/plot_pca_3d.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_decomposition_plot_pca_3d.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_decomposition_plot_pca_3d.py:


=========================================================
Principal components analysis (PCA)
=========================================================

These figures aid in illustrating how a point cloud
can be very flat in one direction--which is where PCA
comes in to choose a direction that is not flat.

.. GENERATED FROM PYTHON SOURCE LINES 11-17

.. code-block:: default


    # Authors: Gael Varoquaux
    #          Jaques Grobler
    #          Kevin Hughes
    # License: BSD 3 clause








.. GENERATED FROM PYTHON SOURCE LINES 18-20

Create the data
---------------

.. GENERATED FROM PYTHON SOURCE LINES 20-50

.. code-block:: default


    import numpy as np
    from scipy import stats

    e = np.exp(1)
    np.random.seed(4)


    def pdf(x):
        return 0.5 * (stats.norm(scale=0.25 / e).pdf(x) + stats.norm(scale=4 / e).pdf(x))


    y = np.random.normal(scale=0.5, size=(30000))
    x = np.random.normal(scale=0.5, size=(30000))
    z = np.random.normal(scale=0.1, size=len(x))

    density = pdf(x) * pdf(y)
    pdf_z = pdf(5 * z)

    density *= pdf_z

    a = x + y
    b = 2 * y
    c = a - b + z

    norm = np.sqrt(a.var() + b.var())
    a /= norm
    b /= norm









.. GENERATED FROM PYTHON SOURCE LINES 51-53

Plot the figures
----------------

.. GENERATED FROM PYTHON SOURCE LINES 53-100

.. code-block:: default


    import matplotlib.pyplot as plt

    # unused but required import for doing 3d projections with matplotlib < 3.2
    import mpl_toolkits.mplot3d  # noqa: F401

    from sklearn.decomposition import PCA


    def plot_figs(fig_num, elev, azim):
        fig = plt.figure(fig_num, figsize=(4, 3))
        plt.clf()
        ax = fig.add_subplot(111, projection="3d", elev=elev, azim=azim)
        ax.set_position([0, 0, 0.95, 1])

        ax.scatter(a[::10], b[::10], c[::10], c=density[::10], marker="+", alpha=0.4)
        Y = np.c_[a, b, c]

        # Using SciPy's SVD, this would be:
        # _, pca_score, Vt = scipy.linalg.svd(Y, full_matrices=False)

        pca = PCA(n_components=3)
        pca.fit(Y)
        V = pca.components_.T

        x_pca_axis, y_pca_axis, z_pca_axis = 3 * V
        x_pca_plane = np.r_[x_pca_axis[:2], -x_pca_axis[1::-1]]
        y_pca_plane = np.r_[y_pca_axis[:2], -y_pca_axis[1::-1]]
        z_pca_plane = np.r_[z_pca_axis[:2], -z_pca_axis[1::-1]]
        x_pca_plane.shape = (2, 2)
        y_pca_plane.shape = (2, 2)
        z_pca_plane.shape = (2, 2)
        ax.plot_surface(x_pca_plane, y_pca_plane, z_pca_plane)
        ax.xaxis.set_ticklabels([])
        ax.yaxis.set_ticklabels([])
        ax.zaxis.set_ticklabels([])


    elev = -40
    azim = -80
    plot_figs(1, elev, azim)

    elev = 30
    azim = 20
    plot_figs(2, elev, azim)

    plt.show()



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_pca_3d_001.png
         :alt: plot pca 3d
         :srcset: /auto_examples/decomposition/images/sphx_glr_plot_pca_3d_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_pca_3d_002.png
         :alt: plot pca 3d
         :srcset: /auto_examples/decomposition/images/sphx_glr_plot_pca_3d_002.png
         :class: sphx-glr-multi-img






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

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


.. _sphx_glr_download_auto_examples_decomposition_plot_pca_3d.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/decomposition/plot_pca_3d.ipynb
        :alt: Launch binder
        :width: 150 px



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

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

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

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


.. only:: html

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

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