Table of Contents

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

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
SparsePCA<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
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

nComponents int

Number of sparse components. Defaults to 2.

alpha double

Sparsity regularization parameter. Higher values produce sparser components. Defaults to 1.0.

ridge double

Ridge regularization for stability. Defaults to 0.01.

maxIter int

Maximum number of iterations. Defaults to 100.

tol double

Convergence tolerance. Defaults to 1e-6.

randomState int?

Random seed for reproducibility.

columnIndices int[]

The column indices to use, or null for all columns.

Properties

Alpha

Gets the sparsity regularization parameter.

public double Alpha { get; }

Property Value

double

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

int

Ridge

Gets the ridge regularization parameter.

public double Ridge { get; }

Property Value

double

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

Methods

FitCore(Matrix<T>)

Fits Sparse PCA using coordinate descent with L1 regularization.

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<T>

GetFeatureNamesOut(string[]?)

Gets the output feature names after transformation.

public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)

Parameters

inputFeatureNames string[]

Returns

string[]

InverseTransformCore(Matrix<T>)

Transforms data back to original space.

protected override Matrix<T> InverseTransformCore(Matrix<T> data)

Parameters

data Matrix<T>

Returns

Matrix<T>

TransformCore(Matrix<T>)

Transforms the data by projecting onto sparse components.

protected override Matrix<T> TransformCore(Matrix<T> data)

Parameters

data Matrix<T>

Returns

Matrix<T>