Class PCA<T>
- Namespace
- AiDotNet.Preprocessing.DimensionalityReduction
- Assembly
- AiDotNet.dll
Principal Component Analysis for dimensionality reduction.
public class PCA<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
-
PCA<T>
- Implements
- Inherited Members
Remarks
PCA finds the directions of maximum variance in the data and projects the data onto these principal components. This reduces dimensionality while preserving as much variance as possible.
PCA is useful for: - Reducing the number of features while retaining most information - Removing multicollinearity between features - Visualizing high-dimensional data - Noise reduction
For Beginners: PCA transforms your features into new features called "principal components" that are: - Uncorrelated with each other - Ordered by importance (how much variance they explain) - Linear combinations of your original features
Example: 100 features might be reduced to 10 principal components that capture 95% of the information in your data.
Constructors
PCA(int?, double?, bool, int[]?)
Creates a new instance of PCA<T>.
public PCA(int? nComponents = null, double? varianceRatio = null, bool whiten = false, int[]? columnIndices = null)
Parameters
nComponentsint?Number of components to keep. If null, keeps all.
varianceRatiodouble?Keep enough components to explain this variance ratio (0-1). Overrides nComponents if set.
whitenboolIf true, scale components to unit variance. Defaults to false.
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[]
ExplainedVarianceRatio
Gets the explained variance ratio for each component.
public double[]? ExplainedVarianceRatio { get; }
Property Value
- double[]
Mean
Gets the mean of each feature.
public double[]? Mean { get; }
Property Value
- double[]
NComponents
Gets the number of components to keep.
public int? NComponents { get; }
Property Value
- int?
NComponentsOut
Gets the number of components after fitting.
public int NComponentsOut { 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
VarianceRatio
Gets the target variance ratio to retain.
public double? VarianceRatio { get; }
Property Value
Whiten
Gets whether whitening is applied.
public bool Whiten { get; }
Property Value
Methods
FitCore(Matrix<T>)
Fits PCA by computing principal components.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix.
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>The transformed data.
Returns
- Matrix<T>
Data in original feature space.
TransformCore(Matrix<T>)
Transforms the data by projecting onto principal components.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The transformed data.