2020-08-22 23:30:45 +08:00
|
|
|
"""Matrix factorization with Sparse PCA."""
|
2011-06-15 07:44:18 +08:00
|
|
|
# Author: Vlad Niculae, Gael Varoquaux, Alexandre Gramfort
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-06-15 07:44:18 +08:00
|
|
|
|
2011-04-29 21:26:31 +08:00
|
|
|
import numpy as np
|
2011-06-12 21:22:54 +08:00
|
|
|
|
2020-10-08 21:47:24 +08:00
|
|
|
from ..utils import check_random_state
|
2014-12-29 21:56:02 +08:00
|
|
|
from ..utils.validation import check_is_fitted
|
2011-08-24 21:10:21 +08:00
|
|
|
from ..linear_model import ridge_regression
|
2021-10-27 19:08:37 +08:00
|
|
|
from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin
|
2019-10-28 22:46:16 +08:00
|
|
|
from ._dict_learning import dict_learning, dict_learning_online
|
2011-07-22 08:15:07 +08:00
|
|
|
|
2011-08-25 00:57:05 +08:00
|
|
|
|
2021-10-27 19:08:37 +08:00
|
|
|
class SparsePCA(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator):
|
2020-08-22 23:30:45 +08:00
|
|
|
"""Sparse Principal Components Analysis (SparsePCA).
|
2011-05-26 07:08:39 +08:00
|
|
|
|
2011-07-27 04:30:07 +08:00
|
|
|
Finds the set of sparse components that can optimally reconstruct
|
|
|
|
|
the data. The amount of sparseness is controllable by the coefficient
|
|
|
|
|
of the L1 penalty, given by the parameter alpha.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
2015-06-03 12:24:04 +08:00
|
|
|
Read more in the :ref:`User Guide <SparsePCA>`.
|
|
|
|
|
|
2011-05-26 18:15:52 +08:00
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
n_components : int, default=None
|
2021-10-24 00:35:23 +08:00
|
|
|
Number of sparse atoms to extract. If None, then ``n_components``
|
2021-10-25 23:41:27 +08:00
|
|
|
is set to ``n_features``.
|
2011-06-15 05:08:38 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
alpha : float, default=1
|
2011-07-27 04:30:07 +08:00
|
|
|
Sparsity controlling parameter. Higher values lead to sparser
|
|
|
|
|
components.
|
2011-06-15 05:08:38 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
ridge_alpha : float, default=0.01
|
2011-07-31 20:33:45 +08:00
|
|
|
Amount of ridge shrinkage to apply in order to improve
|
|
|
|
|
conditioning when calling the transform method.
|
|
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
max_iter : int, default=1000
|
2011-07-16 04:37:33 +08:00
|
|
|
Maximum number of iterations to perform.
|
2011-06-15 05:08:38 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
tol : float, default=1e-8
|
2011-07-16 04:37:33 +08:00
|
|
|
Tolerance for the stopping condition.
|
2011-06-15 05:08:38 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
method : {'lars', 'cd'}, default='lars'
|
2021-07-13 18:55:55 +08:00
|
|
|
Method to be used for optimization.
|
2011-09-19 17:11:33 +08:00
|
|
|
lars: uses the least angle regression method to solve the lasso problem
|
2011-08-25 00:57:05 +08:00
|
|
|
(linear_model.lars_path)
|
2011-09-19 17:11:33 +08:00
|
|
|
cd: uses the coordinate descent method to compute the
|
2011-07-16 20:16:27 +08:00
|
|
|
Lasso solution (linear_model.Lasso). Lars will be faster if
|
|
|
|
|
the estimated components are sparse.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
n_jobs : int, default=None
|
2011-07-16 04:37:33 +08:00
|
|
|
Number of parallel jobs to run.
|
2018-08-18 17:57:10 +08:00
|
|
|
``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
|
|
|
|
|
``-1`` means using all processors. See :term:`Glossary <n_jobs>`
|
|
|
|
|
for more details.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
U_init : ndarray of shape (n_samples, n_components), default=None
|
2021-01-23 02:06:01 +08:00
|
|
|
Initial values for the loadings for warm restart scenarios. Only used
|
|
|
|
|
if `U_init` and `V_init` are not None.
|
2011-07-09 18:20:25 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
V_init : ndarray of shape (n_components, n_features), default=None
|
2021-01-23 02:06:01 +08:00
|
|
|
Initial values for the components for warm restart scenarios. Only used
|
|
|
|
|
if `U_init` and `V_init` are not None.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
verbose : int or bool, default=False
|
2017-07-12 00:42:10 +08:00
|
|
|
Controls the verbosity; the higher, the more messages. Defaults to 0.
|
2011-06-15 05:08:38 +08:00
|
|
|
|
2020-08-22 23:30:45 +08:00
|
|
|
random_state : int, RandomState instance or None, default=None
|
2020-01-09 01:53:01 +08:00
|
|
|
Used during dictionary learning. Pass an int for reproducible results
|
|
|
|
|
across multiple function calls.
|
|
|
|
|
See :term:`Glossary <random_state>`.
|
2011-07-09 18:20:25 +08:00
|
|
|
|
2011-05-26 18:15:52 +08:00
|
|
|
Attributes
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
components_ : ndarray of shape (n_components, n_features)
|
2011-07-16 04:37:33 +08:00
|
|
|
Sparse components extracted from the data.
|
2011-06-13 21:41:50 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
error_ : ndarray
|
2011-07-16 04:37:33 +08:00
|
|
|
Vector of errors at each iteration.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
2020-04-28 05:32:19 +08:00
|
|
|
n_components_ : int
|
|
|
|
|
Estimated number of components.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.23
|
|
|
|
|
|
2014-07-28 17:04:59 +08:00
|
|
|
n_iter_ : int
|
2014-07-09 21:37:22 +08:00
|
|
|
Number of iterations run.
|
|
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
mean_ : ndarray of shape (n_features,)
|
2018-07-21 06:00:15 +08:00
|
|
|
Per-feature empirical mean, estimated from the training set.
|
|
|
|
|
Equal to ``X.mean(axis=0)``.
|
|
|
|
|
|
2021-06-09 22:58:03 +08:00
|
|
|
n_features_in_ : int
|
|
|
|
|
Number of features seen during :term:`fit`.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.24
|
|
|
|
|
|
2021-08-26 19:44:45 +08:00
|
|
|
feature_names_in_ : ndarray of shape (`n_features_in_`,)
|
|
|
|
|
Names of features seen during :term:`fit`. Defined only when `X`
|
|
|
|
|
has feature names that are all strings.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 1.0
|
|
|
|
|
|
2021-07-13 18:55:55 +08:00
|
|
|
See Also
|
|
|
|
|
--------
|
|
|
|
|
PCA : Principal Component Analysis implementation.
|
|
|
|
|
MiniBatchSparsePCA : Mini batch variant of `SparsePCA` that is faster but less
|
|
|
|
|
accurate.
|
|
|
|
|
DictionaryLearning : Generic dictionary learning problem using a sparse code.
|
|
|
|
|
|
2018-08-19 00:50:10 +08:00
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> from sklearn.datasets import make_friedman1
|
|
|
|
|
>>> from sklearn.decomposition import SparsePCA
|
|
|
|
|
>>> X, _ = make_friedman1(n_samples=200, n_features=30, random_state=0)
|
2019-05-29 18:06:50 +08:00
|
|
|
>>> transformer = SparsePCA(n_components=5, random_state=0)
|
2019-06-01 16:53:45 +08:00
|
|
|
>>> transformer.fit(X)
|
2018-08-19 00:50:10 +08:00
|
|
|
SparsePCA(...)
|
|
|
|
|
>>> X_transformed = transformer.transform(X)
|
|
|
|
|
>>> X_transformed.shape
|
|
|
|
|
(200, 5)
|
|
|
|
|
>>> # most values in the components_ are zero (sparsity)
|
2019-06-01 16:53:45 +08:00
|
|
|
>>> np.mean(transformer.components_ == 0)
|
2018-08-19 00:50:10 +08:00
|
|
|
0.9666...
|
2011-05-10 19:43:06 +08:00
|
|
|
"""
|
2021-06-18 02:21:09 +08:00
|
|
|
|
2020-03-31 16:28:36 +08:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
n_components=None,
|
|
|
|
|
*,
|
|
|
|
|
alpha=1,
|
|
|
|
|
ridge_alpha=0.01,
|
2018-08-03 18:34:25 +08:00
|
|
|
max_iter=1000,
|
|
|
|
|
tol=1e-8,
|
|
|
|
|
method="lars",
|
|
|
|
|
n_jobs=None,
|
2020-05-21 04:04:14 +08:00
|
|
|
U_init=None,
|
|
|
|
|
V_init=None,
|
|
|
|
|
verbose=False,
|
|
|
|
|
random_state=None,
|
|
|
|
|
):
|
2011-05-10 19:43:06 +08:00
|
|
|
self.n_components = n_components
|
|
|
|
|
self.alpha = alpha
|
2011-07-31 20:33:45 +08:00
|
|
|
self.ridge_alpha = ridge_alpha
|
2011-05-10 19:43:06 +08:00
|
|
|
self.max_iter = max_iter
|
|
|
|
|
self.tol = tol
|
|
|
|
|
self.method = method
|
|
|
|
|
self.n_jobs = n_jobs
|
|
|
|
|
self.U_init = U_init
|
|
|
|
|
self.V_init = V_init
|
2011-06-15 05:08:38 +08:00
|
|
|
self.verbose = verbose
|
2011-07-09 18:20:25 +08:00
|
|
|
self.random_state = random_state
|
2011-05-10 19:43:06 +08:00
|
|
|
|
2011-08-23 21:21:05 +08:00
|
|
|
def fit(self, X, y=None):
|
2011-05-26 18:15:52 +08:00
|
|
|
"""Fit the model from data in X.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
X : array-like of shape (n_samples, n_features)
|
2021-09-02 19:35:03 +08:00
|
|
|
Training vector, where `n_samples` is the number of samples
|
|
|
|
|
and `n_features` is the number of features.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
2017-09-04 15:47:48 +08:00
|
|
|
y : Ignored
|
2021-07-13 18:55:55 +08:00
|
|
|
Not used, present here for API consistency by convention.
|
2017-09-01 17:10:01 +08:00
|
|
|
|
2011-05-26 18:15:52 +08:00
|
|
|
Returns
|
|
|
|
|
-------
|
2011-07-15 17:16:48 +08:00
|
|
|
self : object
|
|
|
|
|
Returns the instance itself.
|
2011-05-26 18:15:52 +08:00
|
|
|
"""
|
2013-02-10 22:39:23 +08:00
|
|
|
random_state = check_random_state(self.random_state)
|
2020-02-29 22:05:11 +08:00
|
|
|
X = self._validate_data(X)
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2019-05-29 18:06:50 +08:00
|
|
|
self.mean_ = X.mean(axis=0)
|
|
|
|
|
X = X - self.mean_
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2012-06-08 01:24:21 +08:00
|
|
|
if self.n_components is None:
|
|
|
|
|
n_components = X.shape[1]
|
2012-06-27 01:00:51 +08:00
|
|
|
else:
|
|
|
|
|
n_components = self.n_components
|
2011-07-29 03:38:41 +08:00
|
|
|
code_init = self.V_init.T if self.V_init is not None else None
|
|
|
|
|
dict_init = self.U_init.T if self.U_init is not None else None
|
2020-05-19 15:07:25 +08:00
|
|
|
Vt, _, E, self.n_iter_ = dict_learning(
|
|
|
|
|
X.T,
|
|
|
|
|
n_components,
|
|
|
|
|
alpha=self.alpha,
|
2014-07-09 21:37:22 +08:00
|
|
|
tol=self.tol,
|
|
|
|
|
max_iter=self.max_iter,
|
|
|
|
|
method=self.method,
|
|
|
|
|
n_jobs=self.n_jobs,
|
|
|
|
|
verbose=self.verbose,
|
|
|
|
|
random_state=random_state,
|
|
|
|
|
code_init=code_init,
|
2014-07-20 22:17:28 +08:00
|
|
|
dict_init=dict_init,
|
2019-05-29 18:06:50 +08:00
|
|
|
return_n_iter=True,
|
|
|
|
|
)
|
2011-07-16 21:26:12 +08:00
|
|
|
self.components_ = Vt.T
|
2019-05-29 18:06:50 +08:00
|
|
|
components_norm = np.linalg.norm(self.components_, axis=1)[:, np.newaxis]
|
|
|
|
|
components_norm[components_norm == 0] = 1
|
|
|
|
|
self.components_ /= components_norm
|
2020-04-28 05:32:19 +08:00
|
|
|
self.n_components_ = len(self.components_)
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2011-05-10 19:43:06 +08:00
|
|
|
self.error_ = E
|
|
|
|
|
return self
|
|
|
|
|
|
2018-10-12 02:56:37 +08:00
|
|
|
def transform(self, X):
|
2011-07-27 04:30:07 +08:00
|
|
|
"""Least Squares projection of the data onto the sparse components.
|
2011-07-16 18:11:35 +08:00
|
|
|
|
|
|
|
|
To avoid instability issues in case the system is under-determined,
|
2011-07-16 22:13:37 +08:00
|
|
|
regularization can be applied (Ridge regression) via the
|
2011-07-16 18:11:35 +08:00
|
|
|
`ridge_alpha` parameter.
|
|
|
|
|
|
|
|
|
|
Note that Sparse PCA components orthogonality is not enforced as in PCA
|
|
|
|
|
hence one cannot use a simple linear projection.
|
2011-05-26 18:15:52 +08:00
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
X : ndarray of shape (n_samples, n_features)
|
2011-05-26 18:15:52 +08:00
|
|
|
Test data to be transformed, must have the same number of
|
|
|
|
|
features as the data used to train the model.
|
2011-06-13 21:41:50 +08:00
|
|
|
|
2011-05-26 18:15:52 +08:00
|
|
|
Returns
|
|
|
|
|
-------
|
2020-08-22 04:42:44 +08:00
|
|
|
X_new : ndarray of shape (n_samples, n_components)
|
2011-07-16 04:41:30 +08:00
|
|
|
Transformed data.
|
2011-05-26 18:15:52 +08:00
|
|
|
"""
|
2019-08-14 04:09:07 +08:00
|
|
|
check_is_fitted(self)
|
2014-12-29 21:56:02 +08:00
|
|
|
|
2020-10-08 21:47:24 +08:00
|
|
|
X = self._validate_data(X, reset=False)
|
2019-06-22 05:34:08 +08:00
|
|
|
X = X - self.mean_
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2018-10-12 02:56:37 +08:00
|
|
|
U = ridge_regression(
|
|
|
|
|
self.components_.T, X.T, self.ridge_alpha, solver="cholesky"
|
2014-07-14 21:45:21 +08:00
|
|
|
)
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2011-05-10 20:13:34 +08:00
|
|
|
return U
|
2011-07-22 08:38:11 +08:00
|
|
|
|
2021-10-27 19:08:37 +08:00
|
|
|
@property
|
|
|
|
|
def _n_features_out(self):
|
|
|
|
|
"""Number of transformed output features."""
|
|
|
|
|
return self.components_.shape[0]
|
|
|
|
|
|
2022-01-05 00:39:14 +08:00
|
|
|
def _more_tags(self):
|
|
|
|
|
return {
|
|
|
|
|
"preserves_dtype": [np.float64, np.float32],
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-22 08:38:11 +08:00
|
|
|
|
2011-07-22 19:53:47 +08:00
|
|
|
class MiniBatchSparsePCA(SparsePCA):
|
2021-09-01 00:31:34 +08:00
|
|
|
"""Mini-batch Sparse Principal Components Analysis.
|
2011-07-22 08:38:11 +08:00
|
|
|
|
2011-07-27 04:30:07 +08:00
|
|
|
Finds the set of sparse components that can optimally reconstruct
|
|
|
|
|
the data. The amount of sparseness is controllable by the coefficient
|
|
|
|
|
of the L1 penalty, given by the parameter alpha.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2015-06-03 12:24:04 +08:00
|
|
|
Read more in the :ref:`User Guide <SparsePCA>`.
|
|
|
|
|
|
2011-07-22 19:34:35 +08:00
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
n_components : int, default=None
|
2021-10-25 23:41:27 +08:00
|
|
|
Number of sparse atoms to extract. If None, then ``n_components``
|
|
|
|
|
is set to ``n_features``.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
alpha : int, default=1
|
2011-07-27 04:30:07 +08:00
|
|
|
Sparsity controlling parameter. Higher values lead to sparser
|
|
|
|
|
components.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
ridge_alpha : float, default=0.01
|
2011-07-31 20:33:45 +08:00
|
|
|
Amount of ridge shrinkage to apply in order to improve
|
|
|
|
|
conditioning when calling the transform method.
|
|
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
n_iter : int, default=100
|
2021-09-01 00:31:34 +08:00
|
|
|
Number of iterations to perform for each mini batch.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
callback : callable, default=None
|
2021-09-01 00:31:34 +08:00
|
|
|
Callable that gets invoked every five iterations.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
batch_size : int, default=3
|
2021-09-01 00:31:34 +08:00
|
|
|
The number of features to take in each mini batch.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
verbose : int or bool, default=False
|
2017-07-12 00:42:10 +08:00
|
|
|
Controls the verbosity; the higher, the more messages. Defaults to 0.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
shuffle : bool, default=True
|
2021-09-01 00:31:34 +08:00
|
|
|
Whether to shuffle the data before splitting it in batches.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
n_jobs : int, default=None
|
2018-08-18 17:57:10 +08:00
|
|
|
Number of parallel jobs to run.
|
|
|
|
|
``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
|
|
|
|
|
``-1`` means using all processors. See :term:`Glossary <n_jobs>`
|
|
|
|
|
for more details.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
method : {'lars', 'cd'}, default='lars'
|
2021-09-01 00:31:34 +08:00
|
|
|
Method to be used for optimization.
|
2011-09-19 17:11:33 +08:00
|
|
|
lars: uses the least angle regression method to solve the lasso problem
|
2011-08-25 00:57:05 +08:00
|
|
|
(linear_model.lars_path)
|
2011-09-19 17:11:33 +08:00
|
|
|
cd: uses the coordinate descent method to compute the
|
2011-07-22 19:34:35 +08:00
|
|
|
Lasso solution (linear_model.Lasso). Lars will be faster if
|
|
|
|
|
the estimated components are sparse.
|
|
|
|
|
|
2020-08-22 23:30:45 +08:00
|
|
|
random_state : int, RandomState instance or None, default=None
|
2020-01-09 01:53:01 +08:00
|
|
|
Used for random shuffling when ``shuffle`` is set to ``True``,
|
|
|
|
|
during online dictionary learning. Pass an int for reproducible results
|
|
|
|
|
across multiple function calls.
|
|
|
|
|
See :term:`Glossary <random_state>`.
|
2011-07-22 19:34:35 +08:00
|
|
|
|
2011-12-20 18:06:59 +08:00
|
|
|
Attributes
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
components_ : ndarray of shape (n_components, n_features)
|
2011-12-20 18:06:59 +08:00
|
|
|
Sparse components extracted from the data.
|
|
|
|
|
|
2020-04-28 05:32:19 +08:00
|
|
|
n_components_ : int
|
|
|
|
|
Estimated number of components.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.23
|
|
|
|
|
|
2014-07-28 17:04:59 +08:00
|
|
|
n_iter_ : int
|
2014-07-09 21:37:22 +08:00
|
|
|
Number of iterations run.
|
|
|
|
|
|
2020-08-22 04:42:44 +08:00
|
|
|
mean_ : ndarray of shape (n_features,)
|
2018-07-21 06:00:15 +08:00
|
|
|
Per-feature empirical mean, estimated from the training set.
|
|
|
|
|
Equal to ``X.mean(axis=0)``.
|
|
|
|
|
|
2021-06-09 22:58:03 +08:00
|
|
|
n_features_in_ : int
|
|
|
|
|
Number of features seen during :term:`fit`.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.24
|
|
|
|
|
|
2021-08-26 19:44:45 +08:00
|
|
|
feature_names_in_ : ndarray of shape (`n_features_in_`,)
|
|
|
|
|
Names of features seen during :term:`fit`. Defined only when `X`
|
|
|
|
|
has feature names that are all strings.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 1.0
|
|
|
|
|
|
2021-09-01 00:31:34 +08:00
|
|
|
See Also
|
|
|
|
|
--------
|
|
|
|
|
DictionaryLearning : Find a dictionary that sparsely encodes data.
|
|
|
|
|
IncrementalPCA : Incremental principal components analysis.
|
|
|
|
|
PCA : Principal component analysis.
|
|
|
|
|
SparsePCA : Sparse Principal Components Analysis.
|
|
|
|
|
TruncatedSVD : Dimensionality reduction using truncated SVD.
|
|
|
|
|
|
2018-08-19 00:50:10 +08:00
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> from sklearn.datasets import make_friedman1
|
|
|
|
|
>>> from sklearn.decomposition import MiniBatchSparsePCA
|
|
|
|
|
>>> X, _ = make_friedman1(n_samples=200, n_features=30, random_state=0)
|
2019-05-29 18:06:50 +08:00
|
|
|
>>> transformer = MiniBatchSparsePCA(n_components=5, batch_size=50,
|
|
|
|
|
... random_state=0)
|
2019-06-01 16:53:45 +08:00
|
|
|
>>> transformer.fit(X)
|
2018-08-19 00:50:10 +08:00
|
|
|
MiniBatchSparsePCA(...)
|
|
|
|
|
>>> X_transformed = transformer.transform(X)
|
|
|
|
|
>>> X_transformed.shape
|
|
|
|
|
(200, 5)
|
|
|
|
|
>>> # most values in the components_ are zero (sparsity)
|
|
|
|
|
>>> np.mean(transformer.components_ == 0)
|
|
|
|
|
0.94
|
2011-07-22 08:38:11 +08:00
|
|
|
"""
|
2021-06-18 02:21:09 +08:00
|
|
|
|
2020-03-31 16:28:36 +08:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
n_components=None,
|
|
|
|
|
*,
|
|
|
|
|
alpha=1,
|
|
|
|
|
ridge_alpha=0.01,
|
2012-12-08 05:09:01 +08:00
|
|
|
n_iter=100,
|
|
|
|
|
callback=None,
|
|
|
|
|
batch_size=3,
|
|
|
|
|
verbose=False,
|
2020-05-21 04:04:14 +08:00
|
|
|
shuffle=True,
|
|
|
|
|
n_jobs=None,
|
|
|
|
|
method="lars",
|
|
|
|
|
random_state=None,
|
|
|
|
|
):
|
2019-01-11 05:27:06 +08:00
|
|
|
super().__init__(
|
2017-07-05 15:22:21 +08:00
|
|
|
n_components=n_components,
|
|
|
|
|
alpha=alpha,
|
|
|
|
|
verbose=verbose,
|
|
|
|
|
ridge_alpha=ridge_alpha,
|
|
|
|
|
n_jobs=n_jobs,
|
|
|
|
|
method=method,
|
2020-05-21 04:04:14 +08:00
|
|
|
random_state=random_state,
|
|
|
|
|
)
|
2011-07-22 08:38:11 +08:00
|
|
|
self.n_iter = n_iter
|
|
|
|
|
self.callback = callback
|
2012-12-08 05:09:01 +08:00
|
|
|
self.batch_size = batch_size
|
2011-07-22 08:38:11 +08:00
|
|
|
self.shuffle = shuffle
|
|
|
|
|
|
2011-08-23 21:21:05 +08:00
|
|
|
def fit(self, X, y=None):
|
2011-07-22 08:38:11 +08:00
|
|
|
"""Fit the model from data in X.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-22 04:42:44 +08:00
|
|
|
X : array-like of shape (n_samples, n_features)
|
2021-09-01 00:31:34 +08:00
|
|
|
Training vector, where `n_samples` is the number of samples
|
|
|
|
|
and `n_features` is the number of features.
|
2011-07-22 08:38:11 +08:00
|
|
|
|
2017-09-04 15:47:48 +08:00
|
|
|
y : Ignored
|
2021-09-01 00:31:34 +08:00
|
|
|
Not used, present for API consistency by convention.
|
2017-09-01 17:10:01 +08:00
|
|
|
|
2011-07-22 08:38:11 +08:00
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
self : object
|
|
|
|
|
Returns the instance itself.
|
|
|
|
|
"""
|
2013-02-10 22:39:23 +08:00
|
|
|
random_state = check_random_state(self.random_state)
|
2020-02-29 22:05:11 +08:00
|
|
|
X = self._validate_data(X)
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2019-05-29 18:06:50 +08:00
|
|
|
self.mean_ = X.mean(axis=0)
|
|
|
|
|
X = X - self.mean_
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2012-06-08 01:24:21 +08:00
|
|
|
if self.n_components is None:
|
|
|
|
|
n_components = X.shape[1]
|
2012-06-27 01:00:51 +08:00
|
|
|
else:
|
|
|
|
|
n_components = self.n_components
|
2014-07-09 21:37:22 +08:00
|
|
|
Vt, _, self.n_iter_ = dict_learning_online(
|
|
|
|
|
X.T,
|
|
|
|
|
n_components,
|
|
|
|
|
alpha=self.alpha,
|
|
|
|
|
n_iter=self.n_iter,
|
|
|
|
|
return_code=True,
|
|
|
|
|
dict_init=None,
|
|
|
|
|
verbose=self.verbose,
|
|
|
|
|
callback=self.callback,
|
|
|
|
|
batch_size=self.batch_size,
|
|
|
|
|
shuffle=self.shuffle,
|
|
|
|
|
n_jobs=self.n_jobs,
|
|
|
|
|
method=self.method,
|
2014-07-20 22:17:28 +08:00
|
|
|
random_state=random_state,
|
2015-06-03 12:24:04 +08:00
|
|
|
return_n_iter=True,
|
|
|
|
|
)
|
2011-07-22 08:38:11 +08:00
|
|
|
self.components_ = Vt.T
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2019-05-29 18:06:50 +08:00
|
|
|
components_norm = np.linalg.norm(self.components_, axis=1)[:, np.newaxis]
|
|
|
|
|
components_norm[components_norm == 0] = 1
|
|
|
|
|
self.components_ /= components_norm
|
2020-04-28 05:32:19 +08:00
|
|
|
self.n_components_ = len(self.components_)
|
2018-07-21 06:00:15 +08:00
|
|
|
|
2011-07-22 08:38:11 +08:00
|
|
|
return self
|