Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC Add links to decomposition examples in docstrings and user guide #26932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 11, 2024
Merged
3 changes: 3 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@
"auto_examples/ensemble/plot_adaboost_hastie_10_2": (
"auto_examples/ensemble/plot_adaboost_multiclass"
),
"auto_examples/decomposition/plot_pca_3d": (
"auto_examples/decomposition/plot_pca_iris"
),
}
html_context["redirects"] = redirects
for old_link in redirects:
Expand Down
1 change: 1 addition & 0 deletions doc/modules/decomposition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ data based on the amount of variance it explains. As such it implements a

.. topic:: Examples:

* :ref:`sphx_glr_auto_examples_decomposition_plot_pca_iris.py`
* :ref:`sphx_glr_auto_examples_decomposition_plot_pca_vs_lda.py`
* :ref:`sphx_glr_auto_examples_decomposition_plot_pca_vs_fa_model_selection.py`

Expand Down
76 changes: 41 additions & 35 deletions doc/tutorial/statistical_inference/unsupervised_learning.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,51 +204,57 @@ Decompositions: from a signal to components and loadings
Principal component analysis: PCA
-----------------------------------

:ref:`PCA` selects the successive components that
explain the maximum variance in the signal.
:ref:`PCA` selects the successive components that explain the maximum variance in the
signal. Let's create a synthetic 3-dimensional dataset.

.. |pca_3d_axis| image:: /auto_examples/decomposition/images/sphx_glr_plot_pca_3d_001.png
:target: ../../auto_examples/decomposition/plot_pca_3d.html
:scale: 70

.. |pca_3d_aligned| image:: /auto_examples/decomposition/images/sphx_glr_plot_pca_3d_002.png
:target: ../../auto_examples/decomposition/plot_pca_3d.html
:scale: 70
.. np.random.seed(0)

.. rst-class:: centered
::

|pca_3d_axis| |pca_3d_aligned|
>>> # Create a signal with only 2 useful dimensions
>>> x1 = np.random.normal(size=(100, 1))
>>> x2 = np.random.normal(size=(100, 1))
>>> x3 = x1 + x2
>>> X = np.concatenate([x1, x2, x3], axis=1)

The point cloud spanned by the observations above is very flat in one
direction: one of the three univariate features can almost be exactly
computed using the other two. PCA finds the directions in which the data is
not *flat*
direction: one of the three univariate features (i.e. z-axis) can almost be exactly
computed using the other two.

When used to *transform* data, PCA can reduce the dimensionality of the
data by projecting on a principal subspace.
.. plot::
:context: close-figs
:align: center

.. np.random.seed(0)
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111, projection='3d')
>>> ax.scatter(X[:, 0], X[:, 1], X[:, 2])
<...>
>>> _ = ax.set(xlabel="x", ylabel="y", zlabel="z")


PCA finds the directions in which the data is not *flat*.

::

>>> # Create a signal with only 2 useful dimensions
>>> x1 = np.random.normal(size=100)
>>> x2 = np.random.normal(size=100)
>>> x3 = x1 + x2
>>> X = np.c_[x1, x2, x3]

>>> from sklearn import decomposition
>>> pca = decomposition.PCA()
>>> pca.fit(X)
PCA()
>>> print(pca.explained_variance_) # doctest: +SKIP
[ 2.18565811e+00 1.19346747e+00 8.43026679e-32]

>>> # As we can see, only the 2 first components are useful
>>> pca.n_components = 2
>>> X_reduced = pca.fit_transform(X)
>>> X_reduced.shape
(100, 2)
>>> from sklearn import decomposition
>>> pca = decomposition.PCA()
>>> pca.fit(X)
PCA()
>>> print(pca.explained_variance_) # doctest: +SKIP
[ 2.18565811e+00 1.19346747e+00 8.43026679e-32]

Looking at the explained variance, we see that only the first two components
are useful. PCA can be used to reduce dimensionality while preserving
most of the information. It will project the data on the principal subspace.

::

>>> pca.set_params(n_components=2)
PCA(n_components=2)
>>> X_reduced = pca.fit_transform(X)
>>> X_reduced.shape
(100, 2)

.. Eigenfaces here?

Expand Down
99 changes: 0 additions & 99 deletions examples/decomposition/plot_pca_3d.py

This file was deleted.

3 changes: 3 additions & 0 deletions sklearn/decomposition/_incremental_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class IncrementalPCA(_BasePCA):
computations to get the principal components, versus 1 large SVD of
complexity ``O(n_samples * n_features ** 2)`` for PCA.

For a usage example, see
:ref:`sphx_glr_auto_examples_decomposition_plot_incremental_pca.py`.

Read more in the :ref:`User Guide <IncrementalPCA>`.

.. versionadded:: 0.16
Expand Down
3 changes: 3 additions & 0 deletions sklearn/decomposition/_kernel_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class KernelPCA(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator
components to extract. It can also use a randomized truncated SVD by the
method proposed in [3]_, see `eigen_solver`.

For a usage example, see
:ref:`sphx_glr_auto_examples_decomposition_plot_kernel_pca.py`.

Read more in the :ref:`User Guide <kernel_PCA>`.

Parameters
Expand Down
3 changes: 3 additions & 0 deletions sklearn/decomposition/_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ class PCA(_BasePCA):
Notice that this class does not support sparse input. See
:class:`TruncatedSVD` for an alternative with sparse data.

For a usage example, see
:ref:`sphx_glr_auto_examples_decomposition_plot_pca_iris.py`

Read more in the :ref:`User Guide <PCA>`.

Parameters
Expand Down
3 changes: 3 additions & 0 deletions sklearn/decomposition/_sparse_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ class MiniBatchSparsePCA(_BaseSparsePCA):
the data. The amount of sparseness is controllable by the coefficient
of the L1 penalty, given by the parameter alpha.

For an example comparing sparse PCA to PCA, see
:ref:`sphx_glr_auto_examples_decomposition_plot_faces_decomposition.py`

Read more in the :ref:`User Guide <SparsePCA>`.

Parameters
Expand Down