SparseRandomProjection
- class sklearn.random_projection.SparseRandomProjection(n_components='auto', *, density='auto', eps=0.1, dense_output=False, compute_inverse_components=False, random_state
Reduce dimensionality through sparse random projection.
Sparse random matrix is an alternative to dense random projection matrix that guarantees similar embedding quality while being much more memory efficient and allowing faster computation of the projected data.
If we note s = 1 / density the components of the random matrix are
drawn from:
-sqrt(s) / sqrt(n_components) with probability 1 / 2s
0 with probability 1 - 1 / s
+sqrt(s) / sqrt(n_components) with probability 1 / 2sRead more in the User Guide.
Dimensionality of the target projection space.
n_components can be automatically adjusted according to the
number of samples in the dataset and the bound given by the
Johnson-Lindenstrauss lemma. In that case the quality of the
embedding is controlled by the eps parameter.
It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset.
Ratio in the range (0, 1] of non-zero component in the random projection matrix.
If density = ‘auto’, the value is set to the minimum density as recommended by Ping Li et al.: 1 / sqrt(n_features).
Use density = 1 / 3.0 if you want to reproduce the results from Achlioptas, 2001.
Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when n_components is set to ‘auto’. This value should be strictly positive.
Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space.
If True, ensure that the output of the random projection is a dense numpy array even if the input and random projection matrix are both sparse. In practice, if the number of components is small the number of zero components in the projected data will be very small and it will be more CPU and memory efficient to use a dense representation.
If False, the projected data uses a sparse representation if the input is sparse.
Learn the inverse transform by computing the pseudo-inverse of the
components during fit. Note that the pseudo-inverse is always a dense
array, even if the training data was sparse. This means that it might be
necessary to call inverse_transform on a small batch of samples at a
time to avoid exhausting the available memory on the host. Moreover,
computing the pseudo-inverse does not scale well to large matrices.
Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See Glossary.
Concrete number of components computed when n_components=”auto”.
Random matrix used for the projection. Sparse matrix will be of CSR format.
Pseudo-inverse of the components, only computed if
compute_inverse_components is True.
Concrete density computed from when density = “auto”.
Ping Li, T. Hastie and K. W. Church, 2006, “Very Sparse Random Projections”. https://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf
D. Achlioptas, 2001, “Database-friendly random projections”, https://cgi.di.uoa.gr/~optas/papers/jl.pdf
>>> import numpy as np
>>> from sklearn.random_projection import SparseRandomProjection
>>> rng = np.random.RandomState(42)
>>> X = rng.rand(25, 3000)
>>>
Generate a sparse random projection matrix.
Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers.
Not used, present here for API consistency by convention.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For
example, if the transformer outputs 3 features, then the feature names
out are: ["class_name0", "class_name1", "class_name2"].
Only used to validate feature names with the names seen in fit.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Project data back to its original space.
Returns an array X_original whose transform would be X. Note that even if X is sparse, X_original is dense: this may use a lot of RAM.
If compute_inverse_components is False, the inverse of the components is
computed during each call to inverse_transform which can be costly.
Data to be transformed back.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Project the data by using matrix product with the random matrix.
The input data to project into a smaller dimensional space.
Projected array. It is a sparse matrix only when the input is sparse and
dense_output = False.
Manifold learning on handwritten digits: Locally Linear Embedding, Isomap…
The Johnson-Lindenstrauss bound for embedding with random projections
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
BaseRandomProjection class instance.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Pass only if the estimator accepts additional params in its fit method.
Transformed array.
Transformed feature names.
Reconstructed data.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.