Class SparsePCA<T>
- Namespace
- AiDotNet.Preprocessing.DimensionalityReduction
- Assembly
- AiDotNet.dll
Sparse Principal Component Analysis using L1 regularization.
public class SparsePCA<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
-
SparsePCA<T>
- Implements
- Inherited Members
Remarks
SparsePCA finds sparse principal components by applying L1 regularization (LASSO-like penalty) to the component loadings. This results in components where many loadings are exactly zero, making them more interpretable.
Unlike standard PCA where each component is a combination of ALL features, sparse PCA produces components that depend on only a subset of features.
For Beginners: Sparse PCA creates "simpler" principal components: - Standard PCA: Component = 0.3*Feature1 + 0.2*Feature2 + 0.1*Feature3 + ... - Sparse PCA: Component = 0.5*Feature1 + 0*Feature2 + 0.4*Feature3 + 0*... - Zeros make it easier to interpret what each component represents
Constructors
SparsePCA(int, double, double, int, double, int?, int[]?)
Creates a new instance of SparsePCA<T>.
public SparsePCA(int nComponents = 2, double alpha = 1, double ridge = 0.01, int maxIter = 100, double tol = 1E-06, int? randomState = null, int[]? columnIndices = null)
Parameters
nComponentsintNumber of sparse components. Defaults to 2.
alphadoubleSparsity regularization parameter. Higher values produce sparser components. Defaults to 1.0.
ridgedoubleRidge regularization for stability. Defaults to 0.01.
maxIterintMaximum number of iterations. Defaults to 100.
toldoubleConvergence tolerance. Defaults to 1e-6.
randomStateint?Random seed for reproducibility.
columnIndicesint[]The column indices to use, or null for all columns.
Properties
Alpha
Gets the sparsity regularization parameter.
public double Alpha { get; }
Property Value
Components
Gets the sparse components (each row is a component).
public double[,]? Components { get; }
Property Value
- double[,]
Error
Gets the reconstruction error for each iteration.
public double[]? Error { get; }
Property Value
- double[]
Mean
Gets the mean of each feature.
public double[]? Mean { get; }
Property Value
- double[]
NComponents
Gets the number of components.
public int NComponents { get; }
Property Value
Ridge
Gets the ridge regularization parameter.
public double Ridge { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
FitCore(Matrix<T>)
Fits Sparse PCA using coordinate descent with L1 regularization.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Transforms data back to original space.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>
TransformCore(Matrix<T>)
Transforms the data by projecting onto sparse components.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>
Returns
- Matrix<T>