2011-08-12 14:30:29 +08:00
|
|
|
|
.. _decompositions:
|
|
|
|
|
|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2011-01-27 07:59:51 +08:00
|
|
|
|
=================================================================
|
2010-11-01 22:11:56 +08:00
|
|
|
|
Decomposing signals in components (matrix factorization problems)
|
2011-01-27 07:59:51 +08:00
|
|
|
|
=================================================================
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
|
.. currentmodule:: sklearn.decomposition
|
2011-04-04 19:31:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
.. _PCA:
|
|
|
|
|
|
|
2012-10-12 02:30:47 +08:00
|
|
|
|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
Principal component analysis (PCA)
|
2011-01-27 07:59:51 +08:00
|
|
|
|
==================================
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2011-01-27 07:59:51 +08:00
|
|
|
|
Exact PCA and probabilistic interpretation
|
|
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
PCA is used to decompose a multivariate dataset in a set of successive
|
|
|
|
|
|
orthogonal components that explain a maximum amount of the variance. In
|
2014-07-06 19:33:20 +08:00
|
|
|
|
scikit-learn, :class:`PCA` is implemented as a *transformer* object
|
|
|
|
|
|
that learns :math:`n` components in its ``fit`` method, and can be used on new
|
|
|
|
|
|
data to project it on these components.
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
The optional parameter ``whiten=True`` parameter make it possible to
|
2011-01-27 07:59:51 +08:00
|
|
|
|
project the data onto the singular space while scaling each component
|
|
|
|
|
|
to unit variance. This is often useful if the models down-stream make
|
|
|
|
|
|
strong assumptions on the isotropy of the signal: this is for example
|
|
|
|
|
|
the case for Support Vector Machines with the RBF kernel and the K-Means
|
2014-06-18 00:55:52 +08:00
|
|
|
|
clustering algorithm.
|
2011-01-27 07:59:51 +08:00
|
|
|
|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
Below is an example of the iris dataset, which is comprised of 4
|
|
|
|
|
|
features, projected on the 2 dimensions that explain most variance:
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_pca_vs_lda_001.png
|
2011-04-10 23:53:15 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_pca_vs_lda.html
|
2010-11-01 22:11:56 +08:00
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-06 04:20:38 +08:00
|
|
|
|
The :class:`PCA` object also provides a
|
|
|
|
|
|
probabilistic interpretation of the PCA that can give a likelihood of
|
|
|
|
|
|
data based on the amount of variance it explains. As such it implements a
|
|
|
|
|
|
`score` method that can be used in cross-validation:
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_pca_vs_fa_model_selection_001.png
|
2013-09-06 04:20:38 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_pca_vs_fa_model_selection.html
|
|
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
2011-01-27 07:59:51 +08:00
|
|
|
|
|
2013-09-06 14:51:06 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
|
|
|
|
|
* :ref:`example_decomposition_plot_pca_vs_lda.py`
|
|
|
|
|
|
* :ref:`example_decomposition_plot_pca_vs_fa_model_selection.py`
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-18 00:55:52 +08:00
|
|
|
|
.. _IncrementalPCA:
|
|
|
|
|
|
|
|
|
|
|
|
Incremental PCA
|
|
|
|
|
|
---------------
|
|
|
|
|
|
|
|
|
|
|
|
The :class:`PCA` object is very useful, but has certain limitations for
|
|
|
|
|
|
large datasets. The biggest limitation is that :class:`PCA` only supports
|
|
|
|
|
|
batch processing, which means all of the data to be processed must fit in main
|
|
|
|
|
|
memory. The :class:`IncrementalPCA` object uses a different form of
|
|
|
|
|
|
processing and allows for partial computations which almost
|
|
|
|
|
|
exactly match the results of :class:`PCA` while processing the data in a
|
|
|
|
|
|
minibatch fashion. :class:`IncrementalPCA` makes it possible to implement
|
|
|
|
|
|
out-of-core Principal Component Analysis either by:
|
|
|
|
|
|
|
|
|
|
|
|
* Using its ``partial_fit`` method on chunks of data fetched sequentially
|
|
|
|
|
|
from the local hard drive or a network database.
|
|
|
|
|
|
|
|
|
|
|
|
* Calling its fit method on a memory mapped file using ``numpy.memmap``.
|
|
|
|
|
|
|
|
|
|
|
|
:class:`IncrementalPCA` only stores estimates of component and noise variances,
|
|
|
|
|
|
in order update ``explained_variance_ratio_`` incrementally. This is why
|
|
|
|
|
|
memory usage depends on the number of samples per batch, rather than the
|
|
|
|
|
|
number of samples to be processed in the dataset.
|
|
|
|
|
|
|
|
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_incremental_pca_001.png
|
|
|
|
|
|
:target: ../auto_examples/decomposition/plot_incremental_pca.html
|
|
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
|
|
|
|
|
|
|
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_incremental_pca_002.png
|
|
|
|
|
|
:target: ../auto_examples/decomposition/plot_incremental_pca.html
|
|
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
|
|
|
|
|
* :ref:`example_decomposition_plot_incremental_pca.py`
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
.. _RandomizedPCA:
|
|
|
|
|
|
|
2011-01-27 07:59:51 +08:00
|
|
|
|
Approximate PCA
|
|
|
|
|
|
---------------
|
|
|
|
|
|
|
2013-07-27 19:36:14 +08:00
|
|
|
|
It is often interesting to project data to a lower-dimensional
|
|
|
|
|
|
space that preserves most of the variance, by dropping the singular vector
|
2011-01-27 07:59:51 +08:00
|
|
|
|
of components associated with lower singular values.
|
|
|
|
|
|
|
2013-07-27 19:36:14 +08:00
|
|
|
|
For instance, if we work with 64x64 pixel gray-level pictures
|
|
|
|
|
|
for face recognition,
|
|
|
|
|
|
the dimensionality of the data is 4096 and it is slow to train an
|
|
|
|
|
|
RBF support vector machine on such wide data. Furthermore we know that
|
|
|
|
|
|
the intrinsic dimensionality of the data is much lower than 4096 since all
|
|
|
|
|
|
pictures of human faces look somewhat alike.
|
|
|
|
|
|
The samples lie on a manifold of much lower
|
2011-01-27 07:59:51 +08:00
|
|
|
|
dimension (say around 200 for instance). The PCA algorithm can be used
|
|
|
|
|
|
to linearly transform the data while both reducing the dimensionality
|
|
|
|
|
|
and preserve most of the explained variance at the same time.
|
|
|
|
|
|
|
|
|
|
|
|
The class :class:`RandomizedPCA` is very useful in that case: since we
|
|
|
|
|
|
are going to drop most of the singular vectors it is much more efficient
|
|
|
|
|
|
to limit the computation to an approximated estimate of the singular
|
|
|
|
|
|
vectors we will keep to actually perform the transform.
|
|
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
For instance, the following shows 16 sample portraits (centered around
|
|
|
|
|
|
0.0) from the Olivetti dataset. On the right hand side are the first 16
|
|
|
|
|
|
singular vectors reshaped as portraits. Since we only require the top
|
|
|
|
|
|
16 singular vectors of a dataset with size :math:`n_{samples} = 400`
|
2011-07-27 02:07:33 +08:00
|
|
|
|
and :math:`n_{features} = 64 \times 64 = 4096`, the computation time it
|
|
|
|
|
|
less than 1s:
|
2011-07-27 02:07:18 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |orig_img| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_001.png
|
2011-07-27 02:07:18 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2011-07-27 02:07:18 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |pca_img| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_002.png
|
2011-07-27 02:07:18 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2011-07-27 02:07:18 +08:00
|
|
|
|
|
|
|
|
|
|
.. centered:: |orig_img| |pca_img|
|
|
|
|
|
|
|
2011-01-27 07:59:51 +08:00
|
|
|
|
:class:`RandomizedPCA` can hence be used as a drop in replacement for
|
2013-07-27 19:36:14 +08:00
|
|
|
|
:class:`PCA` with the exception that we need to give it the size of
|
2014-07-06 19:33:20 +08:00
|
|
|
|
the lower-dimensional space ``n_components`` as a mandatory input parameter.
|
2011-01-27 07:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
If we note :math:`n_{max} = max(n_{samples}, n_{features})` and
|
|
|
|
|
|
:math:`n_{min} = min(n_{samples}, n_{features})`, the time complexity
|
|
|
|
|
|
of :class:`RandomizedPCA` is :math:`O(n_{max}^2 \cdot n_{components})`
|
|
|
|
|
|
instead of :math:`O(n_{max}^2 \cdot n_{min})` for the exact method
|
|
|
|
|
|
implemented in :class:`PCA`.
|
|
|
|
|
|
|
|
|
|
|
|
The memory footprint of :class:`RandomizedPCA` is also proportional to
|
|
|
|
|
|
:math:`2 \cdot n_{max} \cdot n_{components}` instead of :math:`n_{max}
|
|
|
|
|
|
\cdot n_{min}` for the exact method.
|
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
Note: the implementation of ``inverse_transform`` in :class:`RandomizedPCA`
|
|
|
|
|
|
is not the exact inverse transform of ``transform`` even when
|
|
|
|
|
|
``whiten=False`` (default).
|
2011-01-27 07:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2012-04-08 21:51:18 +08:00
|
|
|
|
* :ref:`example_applications_face_recognition.py`
|
2011-07-22 20:51:41 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_faces_decomposition.py`
|
2011-01-27 07:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: References:
|
|
|
|
|
|
|
|
|
|
|
|
* `"Finding structure with randomness: Stochastic algorithms for
|
|
|
|
|
|
constructing approximate matrix decompositions"
|
|
|
|
|
|
<http://arxiv.org/abs/0909.4061>`_
|
|
|
|
|
|
Halko, et al., 2009
|
|
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
|
2011-05-10 15:06:06 +08:00
|
|
|
|
.. _kernel_PCA:
|
|
|
|
|
|
|
2011-04-01 18:46:29 +08:00
|
|
|
|
Kernel PCA
|
|
|
|
|
|
----------
|
|
|
|
|
|
|
2011-04-11 06:15:08 +08:00
|
|
|
|
:class:`KernelPCA` is an extension of PCA which achieves non-linear
|
2014-11-30 21:12:55 +08:00
|
|
|
|
dimensionality reduction through the use of kernels (see :ref:`metrics`). It
|
|
|
|
|
|
has many applications including denoising, compression and structured
|
|
|
|
|
|
prediction (kernel dependency estimation). :class:`KernelPCA` supports both
|
2014-07-06 19:33:20 +08:00
|
|
|
|
``transform`` and ``inverse_transform``.
|
2011-04-01 18:46:29 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_kernel_pca_001.png
|
2011-04-10 23:53:15 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_kernel_pca.html
|
2011-04-01 18:46:29 +08:00
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
|
|
|
|
|
|
2011-04-10 23:53:15 +08:00
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2011-04-11 06:15:08 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_kernel_pca.py`
|
2011-04-10 23:53:15 +08:00
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
|
2011-06-14 03:31:27 +08:00
|
|
|
|
.. _SparsePCA:
|
|
|
|
|
|
|
2013-07-27 19:36:14 +08:00
|
|
|
|
Sparse principal components analysis (SparsePCA and MiniBatchSparsePCA)
|
2011-07-27 02:18:26 +08:00
|
|
|
|
-----------------------------------------------------------------------
|
2011-06-14 03:31:27 +08:00
|
|
|
|
|
2011-07-16 02:17:40 +08:00
|
|
|
|
:class:`SparsePCA` is a variant of PCA, with the goal of extracting the
|
|
|
|
|
|
set of sparse components that best reconstruct the data.
|
2011-06-14 03:31:27 +08:00
|
|
|
|
|
2013-07-27 19:36:14 +08:00
|
|
|
|
Mini-batch sparse PCA (:class:`MiniBatchSparsePCA`) is a variant of
|
2011-07-27 02:18:26 +08:00
|
|
|
|
:class:`SparsePCA` that is faster but less accurate. The increased speed is
|
|
|
|
|
|
reached by iterating over small chunks of the set of features, for a given
|
|
|
|
|
|
number of iterations.
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-26 23:44:51 +08:00
|
|
|
|
Principal component analysis (:class:`PCA`) has the disadvantage that the
|
2011-07-16 00:01:53 +08:00
|
|
|
|
components extracted by this method have exclusively dense expressions, i.e.
|
|
|
|
|
|
they have non-zero coefficients when expressed as linear combinations of the
|
|
|
|
|
|
original variables. This can make interpretation difficult. In many cases,
|
2011-07-14 06:04:26 +08:00
|
|
|
|
the real underlying components can be more naturally imagined as sparse
|
2011-07-16 00:01:53 +08:00
|
|
|
|
vectors; for example in face recognition, components might naturally map to
|
2011-07-14 06:04:26 +08:00
|
|
|
|
parts of faces.
|
2011-06-14 03:31:27 +08:00
|
|
|
|
|
2011-07-16 00:03:07 +08:00
|
|
|
|
Sparse principal components yields a more parsimonious, interpretable
|
|
|
|
|
|
representation, clearly emphasizing which of the original features contribute
|
|
|
|
|
|
to the differences between samples.
|
2011-06-15 05:08:38 +08:00
|
|
|
|
|
2011-07-22 21:22:32 +08:00
|
|
|
|
The following example illustrates 16 components extracted using sparse PCA from
|
|
|
|
|
|
the Olivetti faces dataset. It can be seen how the regularization term induces
|
|
|
|
|
|
many zeros. Furthermore, the natural structure of the data causes the non-zero
|
2011-07-16 00:03:07 +08:00
|
|
|
|
coefficients to be vertically adjacent. The model does not enforce this
|
2011-07-22 21:22:32 +08:00
|
|
|
|
mathematically: each component is a vector :math:`h \in \mathbf{R}^{4096}`, and
|
|
|
|
|
|
there is no notion of vertical adjacency except during the human-friendly
|
|
|
|
|
|
visualization as 64x64 pixel images. The fact that the components shown below
|
|
|
|
|
|
appear local is the effect of the inherent structure of the data, which makes
|
|
|
|
|
|
such local patterns minimize reconstruction error. There exist sparsity-inducing
|
2014-07-06 19:33:20 +08:00
|
|
|
|
norms that take into account adjacency and different kinds of structure; see
|
|
|
|
|
|
[Jen09]_ for a review of such methods.
|
|
|
|
|
|
For more details on how to use Sparse PCA, see the Examples section, below.
|
2011-07-14 06:04:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |spca_img| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_005.png
|
2011-07-22 20:51:41 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2011-06-14 03:31:27 +08:00
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
.. centered:: |pca_img| |spca_img|
|
2011-07-14 06:04:26 +08:00
|
|
|
|
|
2011-07-16 02:17:40 +08:00
|
|
|
|
Note that there are many different formulations for the Sparse PCA
|
|
|
|
|
|
problem. The one implemented here is based on [Mrl09]_ . The optimization
|
|
|
|
|
|
problem solved is a PCA problem (dictionary learning) with an
|
|
|
|
|
|
:math:`\ell_1` penalty on the components:
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
(U^*, V^*) = \underset{U, V}{\operatorname{arg\,min\,}} & \frac{1}{2}
|
|
|
|
|
|
||X-UV||_2^2+\alpha||V||_1 \\
|
|
|
|
|
|
\text{subject to\,} & ||U_k||_2 = 1 \text{ for all }
|
|
|
|
|
|
0 \leq k < n_{components}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-27 19:36:14 +08:00
|
|
|
|
The sparsity-inducing :math:`\ell_1` norm also prevents learning
|
2011-07-16 02:17:40 +08:00
|
|
|
|
components from noise when few training samples are available. The degree
|
|
|
|
|
|
of penalization (and thus sparsity) can be adjusted through the
|
2014-07-06 19:33:20 +08:00
|
|
|
|
hyperparameter ``alpha``. Small values lead to a gently regularized
|
2011-07-16 02:17:40 +08:00
|
|
|
|
factorization, while larger values shrink many coefficients to zero.
|
2011-07-14 06:04:26 +08:00
|
|
|
|
|
2011-07-27 02:18:26 +08:00
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
|
|
While in the spirit of an online algorithm, the class
|
2014-07-06 19:33:20 +08:00
|
|
|
|
:class:`MiniBatchSparsePCA` does not implement ``partial_fit`` because
|
2011-07-27 02:18:26 +08:00
|
|
|
|
the algorithm is online along the features direction, not the samples
|
|
|
|
|
|
direction.
|
2011-07-14 06:04:26 +08:00
|
|
|
|
|
2011-06-14 03:31:27 +08:00
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2011-07-22 20:51:41 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_faces_decomposition.py`
|
2011-06-14 03:31:27 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: References:
|
|
|
|
|
|
|
2011-12-19 18:40:41 +08:00
|
|
|
|
.. [Mrl09] `"Online Dictionary Learning for Sparse Coding"
|
2011-06-14 03:31:27 +08:00
|
|
|
|
<http://www.di.ens.fr/sierra/pdfs/icml09.pdf>`_
|
|
|
|
|
|
J. Mairal, F. Bach, J. Ponce, G. Sapiro, 2009
|
2011-12-19 18:40:41 +08:00
|
|
|
|
.. [Jen09] `"Structured Sparse Principal Component Analysis"
|
2011-07-14 06:04:26 +08:00
|
|
|
|
<www.di.ens.fr/~fbach/sspca_AISTATS2010.pdf>`_
|
|
|
|
|
|
R. Jenatton, G. Obozinski, F. Bach, 2009
|
|
|
|
|
|
|
2011-07-22 20:51:41 +08:00
|
|
|
|
|
2013-01-05 22:06:32 +08:00
|
|
|
|
.. _LSA:
|
|
|
|
|
|
|
|
|
|
|
|
Truncated singular value decomposition and latent semantic analysis
|
|
|
|
|
|
===================================================================
|
|
|
|
|
|
|
|
|
|
|
|
:class:`TruncatedSVD` implements a variant of singular value decomposition
|
|
|
|
|
|
(SVD) that only computes the :math:`k` largest singular values,
|
|
|
|
|
|
where :math:`k` is a user-specified parameter.
|
|
|
|
|
|
|
|
|
|
|
|
When truncated SVD is applied to term-document matrices
|
|
|
|
|
|
(as returned by ``CountVectorizer`` or ``TfidfVectorizer``),
|
|
|
|
|
|
this transformation is known as
|
|
|
|
|
|
`latent semantic analysis <http://nlp.stanford.edu/IR-book/pdf/18lsi.pdf>`_
|
|
|
|
|
|
(LSA), because it transforms such matrices
|
|
|
|
|
|
to a "semantic" space of low dimensionality.
|
|
|
|
|
|
In particular, LSA is known to combat the effects of synonymy and polysemy
|
|
|
|
|
|
(both of which roughly mean there are multiple meanings per word),
|
|
|
|
|
|
which cause term-document matrices to be overly sparse
|
|
|
|
|
|
and exhibit poor similarity under measures such as cosine similarity.
|
|
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
LSA is also known as latent semantic indexing, LSI,
|
|
|
|
|
|
though strictly that refers to its use in persistent indexes
|
|
|
|
|
|
for information retrieval purposes.
|
|
|
|
|
|
|
|
|
|
|
|
Mathematically, truncated SVD applied to training samples :math:`X`
|
|
|
|
|
|
produces a low-rank approximation :math:`X`:
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
X \approx X_k = U_k \Sigma_k V_k^\top
|
|
|
|
|
|
|
|
|
|
|
|
After this operation, :math:`U_k \Sigma_k^\top`
|
|
|
|
|
|
is the transformed training set with :math:`k` features
|
|
|
|
|
|
(called ``n_components`` in the API).
|
|
|
|
|
|
|
|
|
|
|
|
To also transform a test set :math:`X`, we multiply it with :math:`V_k`:
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
2015-05-15 17:26:57 +08:00
|
|
|
|
X' = X V_k
|
2013-01-05 22:06:32 +08:00
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
Most treatments of LSA in the natural language processing (NLP)
|
|
|
|
|
|
and information retrieval (IR) literature
|
2015-05-19 21:27:04 +08:00
|
|
|
|
swap the axes of the matrix :math:`X` so that it has shape
|
2013-01-05 22:06:32 +08:00
|
|
|
|
``n_features`` × ``n_samples``.
|
|
|
|
|
|
We present LSA in a different way that matches the scikit-learn API better,
|
|
|
|
|
|
but the singular values found are the same.
|
|
|
|
|
|
|
|
|
|
|
|
:class:`TruncatedSVD` is very similar to :class:`PCA`, but differs
|
|
|
|
|
|
in that it works on sample matrices :math:`X` directly
|
|
|
|
|
|
instead of their covariance matrices.
|
|
|
|
|
|
When the columnwise (per-feature) means of :math:`X`
|
|
|
|
|
|
are subtracted from the feature values,
|
|
|
|
|
|
truncated SVD on the resulting matrix is equivalent to PCA.
|
|
|
|
|
|
In practical terms, this means
|
|
|
|
|
|
that the :class:`TruncatedSVD` transformer accepts ``scipy.sparse``
|
|
|
|
|
|
matrices without the need to densify them,
|
|
|
|
|
|
as densifying may fill up memory even for medium-sized document collections.
|
|
|
|
|
|
|
|
|
|
|
|
While the :class:`TruncatedSVD` transformer
|
|
|
|
|
|
works with any (sparse) feature matrix,
|
|
|
|
|
|
using it on tf–idf matrices is recommended over raw frequency counts
|
|
|
|
|
|
in an LSA/document processing setting.
|
|
|
|
|
|
In particular, sublinear scaling and inverse document frequency
|
|
|
|
|
|
should be turned on (``sublinear_tf=True, use_idf=True``)
|
|
|
|
|
|
to bring the feature values closer to a Gaussian distribution,
|
|
|
|
|
|
compensating for LSA's erroneous assumptions about textual data.
|
|
|
|
|
|
|
2013-05-28 17:56:32 +08:00
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2013-07-28 23:39:38 +08:00
|
|
|
|
* :ref:`example_text_document_clustering.py`
|
2013-05-28 17:56:32 +08:00
|
|
|
|
|
2013-01-05 22:06:32 +08:00
|
|
|
|
.. topic:: References:
|
|
|
|
|
|
|
|
|
|
|
|
* Christopher D. Manning, Prabhakar Raghavan and Hinrich Schütze (2008),
|
|
|
|
|
|
*Introduction to Information Retrieval*, Cambridge University Press,
|
|
|
|
|
|
chapter 18: `Matrix decompositions & latent semantic indexing
|
|
|
|
|
|
<http://nlp.stanford.edu/IR-book/pdf/18lsi.pdf>`_
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-18 00:50:14 +08:00
|
|
|
|
.. _DictionaryLearning:
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary Learning
|
|
|
|
|
|
===================
|
|
|
|
|
|
|
2011-12-08 03:15:42 +08:00
|
|
|
|
.. _SparseCoder:
|
|
|
|
|
|
|
2011-12-08 02:31:06 +08:00
|
|
|
|
Sparse coding with a precomputed dictionary
|
|
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
2011-12-08 03:15:42 +08:00
|
|
|
|
The :class:`SparseCoder` object is an estimator that can be used to transform signals
|
2011-12-08 02:31:06 +08:00
|
|
|
|
into sparse linear combination of atoms from a fixed, precomputed dictionary
|
|
|
|
|
|
such as a discrete wavelet basis. This object therefore does not
|
2014-07-06 19:33:20 +08:00
|
|
|
|
implement a ``fit`` method. The transformation amounts
|
2011-12-08 02:31:06 +08:00
|
|
|
|
to a sparse coding problem: finding a representation of the data as a linear
|
|
|
|
|
|
combination of as few dictionary atoms as possible. All variations of
|
|
|
|
|
|
dictionary learning implement the following transform methods, controllable via
|
2014-07-06 19:33:20 +08:00
|
|
|
|
the ``transform_method`` initialization parameter:
|
2011-12-08 02:31:06 +08:00
|
|
|
|
|
|
|
|
|
|
* Orthogonal matching pursuit (:ref:`omp`)
|
|
|
|
|
|
|
|
|
|
|
|
* Least-angle regression (:ref:`least_angle_regression`)
|
|
|
|
|
|
|
|
|
|
|
|
* Lasso computed by least-angle regression
|
|
|
|
|
|
|
|
|
|
|
|
* Lasso using coordinate descent (:ref:`lasso`)
|
|
|
|
|
|
|
|
|
|
|
|
* Thresholding
|
|
|
|
|
|
|
|
|
|
|
|
Thresholding is very fast but it does not yield accurate reconstructions.
|
|
|
|
|
|
They have been shown useful in literature for classification tasks. For image
|
|
|
|
|
|
reconstruction tasks, orthogonal matching pursuit yields the most accurate,
|
|
|
|
|
|
unbiased reconstruction.
|
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
The dictionary learning objects offer, via the ``split_code`` parameter, the
|
2011-12-08 02:31:06 +08:00
|
|
|
|
possibility to separate the positive and negative values in the results of
|
|
|
|
|
|
sparse coding. This is useful when dictionary learning is used for extracting
|
|
|
|
|
|
features that will be used for supervised learning, because it allows the
|
|
|
|
|
|
learning algorithm to assign different weights to negative loadings of a
|
|
|
|
|
|
particular atom, from to the corresponding positive loading.
|
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
The split code for a single sample has length ``2 * n_components``
|
2011-12-08 02:31:06 +08:00
|
|
|
|
and is constructed using the following rule: First, the regular code of length
|
2014-07-06 19:33:20 +08:00
|
|
|
|
``n_components`` is computed. Then, the first ``n_components`` entries of the
|
|
|
|
|
|
``split_code`` are
|
2011-12-08 02:31:06 +08:00
|
|
|
|
filled with the positive part of the regular code vector. The second half of
|
|
|
|
|
|
the split code is filled with the negative part of the code vector, only with
|
|
|
|
|
|
a positive sign. Therefore, the split_code is non-negative.
|
|
|
|
|
|
|
2011-12-20 20:54:09 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
|
|
|
|
|
* :ref:`example_decomposition_plot_sparse_coding.py`
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-18 00:50:14 +08:00
|
|
|
|
Generic dictionary learning
|
|
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary learning (:class:`DictionaryLearning`) is a matrix factorization
|
|
|
|
|
|
problem that amounts to finding a (usually overcomplete) dictionary that will
|
|
|
|
|
|
perform good at sparsely encoding the fitted data.
|
|
|
|
|
|
|
|
|
|
|
|
Representing data as sparse combinations of atoms from an overcomplete
|
|
|
|
|
|
dictionary is suggested to be the way the mammal primary visual cortex works.
|
2011-11-30 01:32:05 +08:00
|
|
|
|
Consequently, dictionary learning applied on image patches has been shown to
|
2011-09-18 00:50:14 +08:00
|
|
|
|
give good results in image processing tasks such as image completion,
|
|
|
|
|
|
inpainting and denoising, as well as for supervised recognition tasks.
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary learning is an optimization problem solved by alternatively updating
|
|
|
|
|
|
the sparse code, as a solution to multiple Lasso problems, considering the
|
|
|
|
|
|
dictionary fixed, and then updating the dictionary to best fit the sparse code.
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
(U^*, V^*) = \underset{U, V}{\operatorname{arg\,min\,}} & \frac{1}{2}
|
|
|
|
|
|
||X-UV||_2^2+\alpha||U||_1 \\
|
|
|
|
|
|
\text{subject to\,} & ||V_k||_2 = 1 \text{ for all }
|
|
|
|
|
|
0 \leq k < n_{atoms}
|
|
|
|
|
|
|
2011-09-19 18:31:36 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |pca_img2| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_002.png
|
2011-09-19 18:31:36 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
|
|
|
|
|
:scale: 60%
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |dict_img2| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_006.png
|
2011-09-19 18:31:36 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
|
|
|
|
|
:scale: 60%
|
|
|
|
|
|
|
2011-12-20 20:20:32 +08:00
|
|
|
|
.. centered:: |pca_img2| |dict_img2|
|
2011-09-19 18:31:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-08 02:31:06 +08:00
|
|
|
|
After using such a procedure to fit the dictionary, the transform is simply a
|
|
|
|
|
|
sparse coding step that shares the same implementation with all dictionary
|
2011-12-08 03:15:42 +08:00
|
|
|
|
learning objects (see :ref:`SparseCoder`).
|
2011-09-18 00:50:14 +08:00
|
|
|
|
|
|
|
|
|
|
The following image shows how a dictionary learned from 4x4 pixel image patches
|
|
|
|
|
|
extracted from part of the image of Lena looks like.
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_image_denoising_001.png
|
2011-09-19 17:43:51 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_image_denoising.html
|
2011-09-18 00:50:14 +08:00
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 50%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2011-09-19 17:43:51 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_image_denoising.py`
|
2011-09-18 00:50:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: References:
|
|
|
|
|
|
|
2011-11-30 01:32:05 +08:00
|
|
|
|
* `"Online dictionary learning for sparse coding"
|
2011-09-18 00:50:14 +08:00
|
|
|
|
<http://www.di.ens.fr/sierra/pdfs/icml09.pdf>`_
|
|
|
|
|
|
J. Mairal, F. Bach, J. Ponce, G. Sapiro, 2009
|
|
|
|
|
|
|
2011-12-19 23:24:38 +08:00
|
|
|
|
.. _MiniBatchDictionaryLearning:
|
2011-09-18 00:50:14 +08:00
|
|
|
|
|
|
|
|
|
|
Mini-batch dictionary learning
|
2011-12-19 17:22:44 +08:00
|
|
|
|
------------------------------
|
2011-09-18 00:50:14 +08:00
|
|
|
|
|
|
|
|
|
|
:class:`MiniBatchDictionaryLearning` implements a faster, but less accurate
|
|
|
|
|
|
version of the dictionary learning algorithm that is better suited for large
|
2011-11-30 01:32:05 +08:00
|
|
|
|
datasets.
|
2011-09-18 00:50:14 +08:00
|
|
|
|
|
|
|
|
|
|
By default, :class:`MiniBatchDictionaryLearning` divides the data into
|
|
|
|
|
|
mini-batches and optimizes in an online manner by cycling over the mini-batches
|
|
|
|
|
|
for the specified number of iterations. However, at the moment it does not
|
|
|
|
|
|
implement a stopping condition.
|
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
The estimator also implements ``partial_fit``, which updates the dictionary by
|
2011-09-18 00:50:14 +08:00
|
|
|
|
iterating only once over a mini-batch. This can be used for online learning
|
|
|
|
|
|
when the data is not readily available from the start, or for when the data
|
|
|
|
|
|
does not fit into the memory.
|
|
|
|
|
|
|
2013-01-08 18:58:57 +08:00
|
|
|
|
.. currentmodule:: sklearn.cluster
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. image:: ../auto_examples/cluster/images/plot_dict_face_patches_001.png
|
2013-01-08 18:58:57 +08:00
|
|
|
|
:target: ../auto_examples/cluster/plot_dict_face_patches.html
|
|
|
|
|
|
:scale: 50%
|
|
|
|
|
|
:align: right
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: **Clustering for dictionary learning**
|
|
|
|
|
|
|
|
|
|
|
|
Note that when using dictionary learning to extract a representation
|
|
|
|
|
|
(e.g. for sparse coding) clustering can be a good proxy to learn the
|
2013-01-08 19:01:41 +08:00
|
|
|
|
dictionary. For instance the :class:`MiniBatchKMeans` estimator is
|
2014-07-06 19:33:20 +08:00
|
|
|
|
computationally efficient and implements on-line learning with a
|
|
|
|
|
|
``partial_fit`` method.
|
2013-01-08 18:58:57 +08:00
|
|
|
|
|
|
|
|
|
|
Example: :ref:`example_cluster_plot_dict_face_patches.py`
|
|
|
|
|
|
|
|
|
|
|
|
.. currentmodule:: sklearn.decomposition
|
|
|
|
|
|
|
2012-10-13 17:46:07 +08:00
|
|
|
|
.. _FA:
|
|
|
|
|
|
|
|
|
|
|
|
Factor Analysis
|
|
|
|
|
|
===============
|
2012-10-14 01:16:05 +08:00
|
|
|
|
|
2012-10-13 17:46:07 +08:00
|
|
|
|
In unsupervised learning we only have a dataset :math:`X = \{x_1, x_2, \dots, x_n
|
|
|
|
|
|
\}`. How can this dataset be described mathematically? A very simple
|
|
|
|
|
|
`continuous latent variabel` model for :math:`X` is
|
|
|
|
|
|
|
|
|
|
|
|
.. math:: x_i = W h_i + \mu + \epsilon
|
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
The vector :math:`h_i` is called "latent" because it is unobserved. :math:`\epsilon` is
|
2012-10-13 17:46:07 +08:00
|
|
|
|
considered a noise term distributed according to a Gaussian with mean 0 and
|
|
|
|
|
|
covariance :math:`\Psi` (i.e. :math:`\epsilon \sim \mathcal{N}(0, \Psi)`), :math:`\mu` is some
|
2014-07-06 19:33:20 +08:00
|
|
|
|
arbitrary offset vector. Such a model is called "generative" as it describes
|
2012-10-13 17:46:07 +08:00
|
|
|
|
how :math:`x_i` is generated from :math:`h_i`. If we use all the :math:`x_i`'s as columns to form
|
|
|
|
|
|
a matrix :math:`\mathbf{X}` and all the :math:`h_i`'s as columns of a matrix :math:`\mathbf{H}`
|
|
|
|
|
|
then we can write (with suitably defined :math:`\mathbf{M}` and :math:`\mathbf{E}`):
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
\mathbf{X} = W \mathbf{H} + \mathbf{M} + \mathbf{E}
|
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
In other words, we *decomposed* matrix :math:`\mathbf{X}`.
|
2012-10-13 17:46:07 +08:00
|
|
|
|
|
|
|
|
|
|
If :math:`h_i` is given, the above equation automatically implies the following
|
|
|
|
|
|
probabilistic interpretation:
|
|
|
|
|
|
|
|
|
|
|
|
.. math:: p(x_i|h_i) = \mathcal{N}(Wh_i + \mu, \Psi)
|
|
|
|
|
|
|
|
|
|
|
|
For a complete probabilistic model we also need a prior distribution for the
|
|
|
|
|
|
latent variable :math:`h`. The most straightforward assumption (based on the nice
|
|
|
|
|
|
properties of the Gaussian distribution) is :math:`h \sim \mathcal{N}(0,
|
|
|
|
|
|
\mathbf{I})`. This yields a Gaussian as the marginal distribution of :math:`x`:
|
|
|
|
|
|
|
|
|
|
|
|
.. math:: p(x) = \mathcal{N}(\mu, WW^T + \Psi)
|
|
|
|
|
|
|
|
|
|
|
|
Now, without any further assumptions the idea of having a latent variable :math:`h`
|
|
|
|
|
|
would be superfluous -- :math:`x` can be completely modelled with a mean
|
|
|
|
|
|
and a covariance. We need to impose some more specific structure on one
|
|
|
|
|
|
of these two parameters. A simple additional assumption regards the
|
|
|
|
|
|
structure of the error covariance :math:`\Psi`:
|
|
|
|
|
|
|
|
|
|
|
|
* :math:`\Psi = \sigma^2 \mathbf{I}`: This assumption leads to
|
2013-08-30 17:30:48 +08:00
|
|
|
|
the probabilistic model of :class:`PCA`.
|
2012-10-13 17:46:07 +08:00
|
|
|
|
|
2015-06-03 12:24:04 +08:00
|
|
|
|
* :math:`\Psi = diag(\psi_1, \psi_2, \dots, \psi_n)`: This model is called
|
|
|
|
|
|
:class:`FactorAnalysis`, a classical statistical model. The matrix W is
|
|
|
|
|
|
sometimes called the "factor loading matrix".
|
2012-10-13 17:46:07 +08:00
|
|
|
|
|
|
|
|
|
|
Both model essentially estimate a Gaussian with a low-rank covariance matrix.
|
2013-06-27 21:09:16 +08:00
|
|
|
|
Because both models are probabilistic they can be integrated in more complex
|
2012-10-13 17:46:07 +08:00
|
|
|
|
models, e.g. Mixture of Factor Analysers. One gets very different models (e.g.
|
2013-01-12 04:00:46 +08:00
|
|
|
|
:class:`FastICA`) if non-Gaussian priors on the latent variables are assumed.
|
2012-10-13 17:46:07 +08:00
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
|
Factor analysis *can* produce similar components (the columns of its loading
|
2012-10-13 17:46:07 +08:00
|
|
|
|
matrix) to :class:`PCA`. However, one can not make any general statements
|
|
|
|
|
|
about these components (e.g. whether they are orthogonal):
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |pca_img3| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_002.png
|
2012-10-13 17:46:07 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
|
|
|
|
|
:scale: 60%
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |fa_img3| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_009.png
|
2012-10-13 17:46:07 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
|
|
|
|
|
:scale: 60%
|
|
|
|
|
|
|
2012-10-14 21:30:46 +08:00
|
|
|
|
.. centered:: |pca_img3| |fa_img3|
|
|
|
|
|
|
|
2013-08-30 17:30:48 +08:00
|
|
|
|
The main advantage for Factor Analysis (over :class:`PCA` is that
|
2013-09-06 04:20:38 +08:00
|
|
|
|
it can model the variance in every direction of the input space independently
|
|
|
|
|
|
(heteroscedastic noise):
|
2012-10-13 17:46:07 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_faces_decomposition_008.png
|
2012-10-13 17:46:07 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2012-10-14 21:30:46 +08:00
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
2011-09-18 00:50:14 +08:00
|
|
|
|
|
2013-09-06 04:20:38 +08:00
|
|
|
|
This allows better model selection than probabilistic PCA in the presence
|
|
|
|
|
|
of heteroscedastic noise:
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_pca_vs_fa_model_selection_002.png
|
2013-09-06 04:20:38 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_pca_vs_fa_model_selection.html
|
|
|
|
|
|
:align: center
|
|
|
|
|
|
:scale: 75%
|
|
|
|
|
|
|
2013-09-06 14:51:06 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
|
|
|
|
|
* :ref:`example_decomposition_plot_pca_vs_fa_model_selection.py`
|
|
|
|
|
|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
.. _ICA:
|
|
|
|
|
|
|
|
|
|
|
|
Independent component analysis (ICA)
|
2011-01-27 07:59:51 +08:00
|
|
|
|
====================================
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2011-06-19 21:37:44 +08:00
|
|
|
|
Independent component analysis separates a multivariate signal into
|
|
|
|
|
|
additive subcomponents that are maximally independent. It is
|
|
|
|
|
|
implemented in scikit-learn using the :class:`Fast ICA <FastICA>`
|
2013-07-28 20:25:26 +08:00
|
|
|
|
algorithm. Typically, ICA is not used for reducing dimensionality but
|
|
|
|
|
|
for separating superimposed signals. Since the ICA model does not include
|
2013-07-28 20:28:23 +08:00
|
|
|
|
a noise term, for the model to be correct, whitening must be applied.
|
|
|
|
|
|
This can be done internally using the whiten argument or manually using one
|
|
|
|
|
|
of the PCA variants.
|
2011-06-19 21:37:44 +08:00
|
|
|
|
|
|
|
|
|
|
It is classically used to separate mixed signals (a problem known as
|
|
|
|
|
|
*blind source separation*), as in the example below:
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. figure:: ../auto_examples/decomposition/images/plot_ica_blind_source_separation_001.png
|
2011-04-10 23:53:15 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_ica_blind_source_separation.html
|
2010-11-01 22:11:56 +08:00
|
|
|
|
:align: center
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
ICA can also be used as yet another non linear decomposition that finds
|
|
|
|
|
|
components with some sparsity:
|
|
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |pca_img4| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_002.png
|
2011-07-27 02:07:18 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2011-07-27 02:07:18 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |ica_img4| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_004.png
|
2011-07-27 02:07:18 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2012-10-14 21:30:46 +08:00
|
|
|
|
.. centered:: |pca_img4| |ica_img4|
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2011-04-10 23:53:15 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_ica_blind_source_separation.py`
|
|
|
|
|
|
* :ref:`example_decomposition_plot_ica_vs_pca.py`
|
2011-07-27 02:07:18 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_faces_decomposition.py`
|
2010-11-01 22:11:56 +08:00
|
|
|
|
|
2011-04-04 19:31:48 +08:00
|
|
|
|
|
2011-04-02 06:36:48 +08:00
|
|
|
|
.. _NMF:
|
|
|
|
|
|
|
2011-07-27 02:07:18 +08:00
|
|
|
|
Non-negative matrix factorization (NMF or NNMF)
|
|
|
|
|
|
===============================================
|
2011-04-02 06:36:48 +08:00
|
|
|
|
|
2011-04-02 20:51:03 +08:00
|
|
|
|
:class:`NMF` is an alternative approach to decomposition that assumes that the
|
|
|
|
|
|
data and the components are non-negative. :class:`NMF` can be plugged in
|
|
|
|
|
|
instead of :class:`PCA` or its variants, in the cases where the data matrix
|
|
|
|
|
|
does not contain negative values.
|
2013-09-20 18:09:11 +08:00
|
|
|
|
It finds a decomposition of samples :math:`X`
|
2015-03-09 21:19:01 +08:00
|
|
|
|
into two matrices :math:`W` and :math:`H` of non-negative elements,
|
2014-06-23 16:56:58 +08:00
|
|
|
|
by optimizing for the squared Frobenius norm:
|
2013-09-20 18:09:11 +08:00
|
|
|
|
|
|
|
|
|
|
.. math::
|
2015-06-11 00:46:53 +08:00
|
|
|
|
\arg\min_{W,H} \frac{1}{2} ||X - WH||_{Fro}^2 = \frac{1}{2} \sum_{i,j} (X_{ij} - {WH}_{ij})^2
|
2013-09-20 18:09:11 +08:00
|
|
|
|
|
2015-06-11 00:46:53 +08:00
|
|
|
|
This norm is an obvious extension of the Euclidean norm to matrices. (Other
|
|
|
|
|
|
optimization objectives have been suggested in the NMF literature, in
|
|
|
|
|
|
particular Kullback-Leibler divergence, but these are not currently
|
|
|
|
|
|
implemented.)
|
2011-04-02 20:51:03 +08:00
|
|
|
|
|
|
|
|
|
|
Unlike :class:`PCA`, the representation of a vector is obtained in an additive
|
2013-04-12 02:51:28 +08:00
|
|
|
|
fashion, by superimposing the components, without subtracting. Such additive
|
2011-04-02 20:51:03 +08:00
|
|
|
|
models are efficient for representing images and text.
|
|
|
|
|
|
|
|
|
|
|
|
It has been observed in [Hoyer, 04] that, when carefully constrained,
|
2011-04-03 20:57:04 +08:00
|
|
|
|
:class:`NMF` can produce a parts-based representation of the dataset,
|
|
|
|
|
|
resulting in interpretable models. The following example displays 16
|
2011-07-22 21:22:32 +08:00
|
|
|
|
sparse components found by :class:`NMF` from the images in the Olivetti
|
|
|
|
|
|
faces dataset, in comparison with the PCA eigenfaces.
|
2011-04-03 20:57:04 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |pca_img5| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_002.png
|
2011-07-22 20:51:41 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2011-04-03 20:57:04 +08:00
|
|
|
|
|
2014-07-02 22:33:28 +08:00
|
|
|
|
.. |nmf_img5| image:: ../auto_examples/decomposition/images/plot_faces_decomposition_003.png
|
2011-07-22 20:51:41 +08:00
|
|
|
|
:target: ../auto_examples/decomposition/plot_faces_decomposition.html
|
2011-08-12 14:30:29 +08:00
|
|
|
|
:scale: 60%
|
2011-04-03 20:57:04 +08:00
|
|
|
|
|
2012-10-14 21:30:46 +08:00
|
|
|
|
.. centered:: |pca_img5| |nmf_img5|
|
2011-04-03 20:57:04 +08:00
|
|
|
|
|
2011-04-02 20:51:03 +08:00
|
|
|
|
|
2011-04-02 22:16:29 +08:00
|
|
|
|
The :attr:`init` attribute determines the initialization method applied, which
|
2011-05-10 15:06:06 +08:00
|
|
|
|
has a great impact on the performance of the method. :class:`NMF` implements
|
2011-04-02 22:16:29 +08:00
|
|
|
|
the method Nonnegative Double Singular Value Decomposition. NNDSVD is based on
|
2011-05-10 15:06:06 +08:00
|
|
|
|
two SVD processes, one approximating the data matrix, the other approximating
|
|
|
|
|
|
positive sections of the resulting partial SVD factors utilizing an algebraic
|
2011-04-02 22:16:29 +08:00
|
|
|
|
property of unit rank matrices. The basic NNDSVD algorithm is better fit for
|
2011-05-10 15:06:06 +08:00
|
|
|
|
sparse factorization. Its variants NNDSVDa (in which all zeros are set equal to
|
|
|
|
|
|
the mean of all elements of the data), and NNDSVDar (in which the zeros are set
|
|
|
|
|
|
to random perturbations less than the mean of the data divided by 100) are
|
2011-04-02 22:16:29 +08:00
|
|
|
|
recommended in the dense case.
|
|
|
|
|
|
|
2015-06-11 00:46:53 +08:00
|
|
|
|
:class:`NMF` can also be initialized with correctly scaled random non-negative
|
|
|
|
|
|
matrices by setting :attr:`init="random"`. An integer seed or a
|
|
|
|
|
|
``RandomState`` can also be passed to :attr:`random_state` to control
|
|
|
|
|
|
reproducibility.
|
2011-04-02 20:59:47 +08:00
|
|
|
|
|
2015-06-11 00:46:53 +08:00
|
|
|
|
In :class:`NMF`, L1 and L2 priors can be added to the loss function in order
|
|
|
|
|
|
to regularize the model. The L2 prior uses the Frobenius norm, while the L1
|
|
|
|
|
|
prior uses an elementwise L1 norm. As in :class:`ElasticNet`, we control the
|
|
|
|
|
|
combination of L1 and L2 with the :attr:`l1_ratio` (:math:`\rho`) parameter,
|
|
|
|
|
|
and the intensity of the regularization with the :attr:`alpha`
|
|
|
|
|
|
(:math:`\alpha`) parameter. Then the priors terms are:
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
\alpha \rho ||W||_1 + \alpha \rho ||H||_1
|
|
|
|
|
|
+ \frac{\alpha(1-\rho)}{2} ||W||_{Fro} ^ 2
|
|
|
|
|
|
+ \frac{\alpha(1-\rho)}{2} ||H||_{Fro} ^ 2
|
|
|
|
|
|
|
|
|
|
|
|
and the regularized objective function is:
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
\frac{1}{2}||X - WH||_{Fro}^2
|
|
|
|
|
|
+ \alpha \rho ||W||_1 + \alpha \rho ||H||_1
|
|
|
|
|
|
+ \frac{\alpha(1-\rho)}{2} ||W||_{Fro} ^ 2
|
|
|
|
|
|
+ \frac{\alpha(1-\rho)}{2} ||H||_{Fro} ^ 2
|
|
|
|
|
|
|
|
|
|
|
|
:class:`NMF` regularizes both W and H. The public function
|
|
|
|
|
|
:func:`non_negative_factorization` allows a finer control through the
|
|
|
|
|
|
:attr:`regularization` attribute, and may regularize only W, only H, or both.
|
2011-04-02 20:51:03 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2011-07-22 20:51:41 +08:00
|
|
|
|
* :ref:`example_decomposition_plot_faces_decomposition.py`
|
2015-06-24 12:03:48 +08:00
|
|
|
|
* :ref:`example_applications_topics_extraction_with_nmf_lda.py`
|
2011-04-02 20:51:03 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: References:
|
|
|
|
|
|
|
|
|
|
|
|
* `"Learning the parts of objects by non-negative matrix factorization"
|
2015-03-09 21:19:01 +08:00
|
|
|
|
<http://hebb.mit.edu/people/seung/papers/ls-lponm-99.pdf>`_
|
2011-04-02 20:51:03 +08:00
|
|
|
|
D. Lee, S. Seung, 1999
|
|
|
|
|
|
|
|
|
|
|
|
* `"Non-negative Matrix Factorization with Sparseness Constraints"
|
2015-03-09 21:19:01 +08:00
|
|
|
|
<http://www.jmlr.org/papers/volume5/hoyer04a/hoyer04a.pdf>`_
|
2011-04-02 20:51:03 +08:00
|
|
|
|
P. Hoyer, 2004
|
|
|
|
|
|
|
|
|
|
|
|
* `"Projected gradient methods for non-negative matrix factorization"
|
|
|
|
|
|
<http://www.csie.ntu.edu.tw/~cjlin/nmf/>`_
|
|
|
|
|
|
C.-J. Lin, 2007
|
2011-04-02 06:36:48 +08:00
|
|
|
|
|
2011-04-02 20:51:03 +08:00
|
|
|
|
* `"SVD based initialization: A head start for nonnegative
|
|
|
|
|
|
matrix factorization"
|
2012-11-15 15:33:09 +08:00
|
|
|
|
<http://scgroup.hpclab.ceid.upatras.gr/faculty/stratis/Papers/HPCLAB020107.pdf>`_
|
2011-05-10 15:06:06 +08:00
|
|
|
|
C. Boutsidis, E. Gallopoulos, 2008
|
2015-05-26 11:51:49 +08:00
|
|
|
|
|
2015-06-11 00:46:53 +08:00
|
|
|
|
* `"Fast local algorithms for large scale nonnegative matrix and tensor
|
|
|
|
|
|
factorizations."
|
|
|
|
|
|
<http://www.bsp.brain.riken.jp/publications/2009/Cichocki-Phan-IEICE_col.pdf>`_
|
|
|
|
|
|
A. Cichocki, P. Anh-Huy, 2009
|
|
|
|
|
|
|
2015-05-26 11:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
.. _LatentDirichletAllocation:
|
|
|
|
|
|
|
|
|
|
|
|
Latent Dirichlet Allocation (LDA)
|
|
|
|
|
|
=================================
|
|
|
|
|
|
|
2015-06-25 06:33:04 +08:00
|
|
|
|
Latent Dirichlet Allocation is a generative probabilistic model for collections of
|
2015-08-10 03:22:00 +08:00
|
|
|
|
discrete dataset such as text corpora. It is also a topic model that is used for
|
2015-06-25 06:33:04 +08:00
|
|
|
|
discovering abstract topics from a collection of documents.
|
|
|
|
|
|
|
|
|
|
|
|
The graphical model of LDA is a three-level Bayesian model:
|
|
|
|
|
|
|
|
|
|
|
|
.. image:: ../images/lda_model_graph.png
|
|
|
|
|
|
:align: center
|
|
|
|
|
|
|
|
|
|
|
|
When modeling text corpora, the model assumes the following generative process for
|
|
|
|
|
|
a corpus with :math:`D` documents and :math:`K` topics:
|
|
|
|
|
|
|
|
|
|
|
|
1. For each topic :math:`k`, draw :math:`\beta_k \sim Dirichlet(\eta),\: k =1...K`
|
|
|
|
|
|
|
|
|
|
|
|
2. For each document :math:`d`, draw :math:`\theta_d \sim Dirichlet(\alpha), \: d=1...D`
|
|
|
|
|
|
|
|
|
|
|
|
3. For each word :math:`i` in document :math:`d`:
|
|
|
|
|
|
a. Draw a topic index :math:`z_{di} \sim Multinomial(\theta_d)`
|
|
|
|
|
|
b. Draw the observed word :math:`w_{ij} \sim Multinomial(beta_{z_{di}}.)`
|
|
|
|
|
|
|
|
|
|
|
|
For parameter estimation, the posterior distribution is:
|
|
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
p(z, \theta, \beta |w, \alpha, \eta) =
|
|
|
|
|
|
\frac{p(z, \theta, \beta|\alpha, \eta)}{p(w|\alpha, \eta)}
|
|
|
|
|
|
|
|
|
|
|
|
Since the posterior is intractable, variational Bayesian method
|
|
|
|
|
|
uses a simpler distribution :math:`q(z,\theta,\beta | \lambda, \phi, \gamma)`
|
|
|
|
|
|
to approximate it, and those variational parameters :math:`\lambda`, :math:`\phi`,
|
2015-08-10 03:22:00 +08:00
|
|
|
|
:math:`\gamma` are optimized to maximize the Evidence Lower Bound (ELBO):
|
2015-06-25 06:33:04 +08:00
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
log\: P(w | \alpha, \eta) \geq L(w,\phi,\gamma,\lambda) \overset{\triangle}{=}
|
|
|
|
|
|
E_{q}[log\:p(w,z,\theta,\beta|\alpha,\eta)] - E_{q}[log\:q(z, \theta, \beta)]
|
|
|
|
|
|
|
|
|
|
|
|
Maximizing ELBO is equivalent to minimizing the Kullback-Leibler(KL) divergence
|
|
|
|
|
|
between :math:`q(z,\theta,\beta)` and the true posterior
|
|
|
|
|
|
:math:`p(z, \theta, \beta |w, \alpha, \eta)`.
|
2015-05-26 11:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
:class:`LatentDirichletAllocation` implements online variational Bayes algorithm and supports
|
|
|
|
|
|
both online and batch update method.
|
2015-05-31 23:55:54 +08:00
|
|
|
|
While batch method updates variational variables after each full pass through the data,
|
|
|
|
|
|
online method updates variational variables from mini-batch data points. Therefore,
|
|
|
|
|
|
online method usually converges faster than batch method.
|
2015-05-26 11:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
|
|
Although online method is guaranteed to converge to a local optimum point, the quality of
|
|
|
|
|
|
the optimum point and the speed of convergence may depend on mini-batch size and
|
|
|
|
|
|
attributes related to learning rate setting.
|
|
|
|
|
|
|
|
|
|
|
|
When :class:`LatentDirichletAllocation` is applied on a "document-term" matrix, the matrix
|
|
|
|
|
|
will be decomposed into a "topic-term" matrix and a "document-topic" matrix. While
|
|
|
|
|
|
"topic-term" matrix is stored as :attr:`components_` in the model, "document-topic" matrix
|
|
|
|
|
|
can be calculated from ``transform`` method.
|
|
|
|
|
|
|
|
|
|
|
|
:class:`LatentDirichletAllocation` also implements ``partial_fit`` method. This is used
|
|
|
|
|
|
when data can be fetched sequentially.
|
|
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
|
2015-06-24 12:03:48 +08:00
|
|
|
|
* :ref:`example_applications_topics_extraction_with_nmf_lda.py`
|
2015-05-26 11:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
.. topic:: References:
|
|
|
|
|
|
|
|
|
|
|
|
* `"Latent Dirichlet Allocation"
|
|
|
|
|
|
<https://www.cs.princeton.edu/~blei/papers/BleiNgJordan2003.pdf>`_
|
|
|
|
|
|
D. Blei, A. Ng, M. Jordan, 2003
|
|
|
|
|
|
|
|
|
|
|
|
* `"Online Learning for Latent Dirichlet Allocation”
|
|
|
|
|
|
<https://www.cs.princeton.edu/~blei/papers/HoffmanBleiBach2010b.pdf>`_
|
|
|
|
|
|
M. Hoffman, D. Blei, F. Bach, 2010
|
|
|
|
|
|
|
|
|
|
|
|
* `"Stochastic Variational Inference"
|
|
|
|
|
|
<http://www.columbia.edu/~jwp2128/Papers/HoffmanBleiWangPaisley2013.pdf>`_
|
|
|
|
|
|
M. Hoffman, D. Blei, C. Wang, J. Paisley, 2013
|