Class RandomizedPCA<T>
- Namespace
- AiDotNet.Preprocessing.DimensionalityReduction
- Assembly
- AiDotNet.dll
Randomized PCA using randomized SVD for efficient computation.
public class RandomizedPCA<T> : TransformerBase<T, Matrix<T>, Matrix<T>>, IDataTransformer<T, Matrix<T>, Matrix<T>>
Type Parameters
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
RandomizedPCA<T>
- Implements
- Inherited Members
Remarks
Randomized PCA uses randomized algorithms to efficiently compute principal components without computing the full SVD. It's much faster than standard PCA for large datasets while providing accurate approximations of the top components.
The algorithm: 1. Generate random projection matrix 2. Form sample matrix Y = A * Ω (project data to random subspace) 3. Orthonormalize Y using QR decomposition 4. Form B = Q^T * A (project to Q's range) 5. Compute SVD of B to get principal components
For Beginners: Randomized PCA is faster because: - It only computes the components you need (not all) - Random projection preserves structure efficiently - Power iteration improves accuracy if needed - Works well for low-rank data
Use cases:
- Very large datasets where standard PCA is slow
- When you only need top few components
- Streaming or online scenarios
- Memory-constrained environments
Constructors
RandomizedPCA(int, int, int, int?, int[]?)
Creates a new instance of RandomizedPCA<T>.
public RandomizedPCA(int nComponents = 2, int nOversamples = 10, int nPowerIter = 2, int? randomState = null, int[]? columnIndices = null)
Parameters
nComponentsintTarget dimensionality. Defaults to 2.
nOversamplesintAdditional random vectors for accuracy. Defaults to 10.
nPowerIterintNumber of power iterations for accuracy. Defaults to 2.
randomStateint?Random seed for reproducibility.
columnIndicesint[]The column indices to use, or null for all columns.
Properties
Components
Gets the principal components (each row is a component).
public double[,]? Components { get; }
Property Value
- double[,]
ExplainedVariance
Gets the explained variance for each component.
public double[]? ExplainedVariance { get; }
Property Value
- double[]
NComponents
Gets the number of components (dimensions).
public int NComponents { get; }
Property Value
SingularValues
Gets the singular values.
public double[]? SingularValues { get; }
Property Value
- double[]
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
FitCore(Matrix<T>)
Fits Randomized PCA using randomized SVD.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>
GetExplainedVarianceRatio()
Gets the total explained variance ratio (proportion of total variance explained by selected components).
public double GetExplainedVarianceRatio()
Returns
- double
A value between 0 and 1 representing the fraction of total variance captured.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Reconstructs data from the reduced representation.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>
TransformCore(Matrix<T>)
Transforms data by projecting onto principal components.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>