Random Projection for dimensionality reduction.
Classes
class features.extraction.random_projection.RandomProjectionExtractor(FeatureExtractor)
Random Projection for dimensionality reduction.
__init__( self, n_components: Union[int, float, str] = 10, distribution: str = 'gaussian', random_state: Optional[int] = None, )
Overview
Theory
For a data matrix X_{n \times p}, the projected matrix is:
where R is a random matrix. The minimum dimension k to preserve distances within a factor of 1 \pm \epsilon is:
Parameters
n_components
Target dimensionality:
- •
int >= 1: absolute number of components. - •
float < 1: percentage of original features. - •
"auto": Use the Johnson-Lindenstrauss formula to determine :math:`k`.
distribution
Distribution used for the random matrix:
- •
"gaussian": Normal distribution :math:`N(0, 1/k)`. - •
"sparse": Very-sparse projection (Achlioptas): :math:`\sqrt{3} \times \{-1, 0, 1\}` with probabilities :math:`\{1/6, 2/3, 1/6\}`. - •
"rademacher": Rademacher :math:`\{-1, 1\}` with probabilities :math:`\{1/2, 1/2\}`.
random_state
Attributes
n_components_
components_
Notes
- O(n \cdot p \cdot k) for projection.
- Much faster than PCA as it doesn't require eigenvalue decomposition.
- Very high-dimensional datasets where PCA is computationally prohibitive.
- When preserving pairwise distances is more important than maximizing variance.
- As a fast preprocessing step for distance-based algorithms (k-NN, clustering).
- Only approximately preserves distances.
- Projection matrix is not optimized for the specific data (data-independent).
References
Methods
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'RandomProjectionExtractor'
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'RandomProjectionExtractor'
Fit the random projection matrix.
Parameters
X
y
Returns
self
class features.extraction.random_projection.SparseRandomProjectionExtractor(RandomProjectionExtractor)
Sparse Random Projection for dimensionality reduction.
__init__( self, n_components: Union[int, float, str] = 10, density: float = ..., random_state: Optional[int] = None, )
Overview
Sparse random projection reduces memory requirements and speeds up the transformation by using a matrix where most entries are zero. It still preserves distances according to the Johnson-Lindenstrauss lemma.
This is equivalent to RandomProjectionExtractor with distribution="sparse".
Parameters
n_components
density
random_state
Project high-dimensional data using sparse matrix:
>>> from tuiml.features.extraction import SparseRandomProjectionExtractor
>>> import numpy as np
>>> X = np.random.randn(100, 1000)
>>> srp = SparseRandomProjectionExtractor(n_components="auto")
>>> X_projected = srp.fit_transform(X)
>>> print(f"Projected to {srp.n_components_} dimensions")