PCA
Principal component analysis via power iteration with deflation on the sample covariance matrix. Deterministic (fixed initialization vector). Components are unit-length rows of a k x d matrix; sign is arbitrary.
Example
pca := PCA->New(1);
pca->Fit(X);
reduced := pca->Transform(X); # n x 1
ratios := pca->GetExplainedVarianceRatio();Operations
- New
- Fit
- GetComponents
- GetExplainedVariance
- GetExplainedVarianceRatio
- GetMean
- InverseTransform
- IsFitted
- Load
- Store
- Transform
Fit #
Fits the decomposition: centers the data, builds the sample covariance matrix and extracts the top components by power iteration + deflation.
method : public : Fit(X:Float[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | data matrix (rows=samples, cols=features) |
Return
| Type | Description |
|---|---|
| Bool | true if fitting succeeded |
GetComponents #
Gets the fitted components (num_components x features; unit rows).
method : public : GetComponents() ~ Float[,]Return
| Type | Description |
|---|---|
| Float | component matrix, or Nil if not fitted |
GetExplainedVariance #
Gets the variance explained by each component (eigenvalues).
method : public : GetExplainedVariance() ~ Float[]Return
| Type | Description |
|---|---|
| Float | explained variance array, or Nil if not fitted |
GetExplainedVarianceRatio #
Gets the fraction of total variance explained by each component.
method : public : GetExplainedVarianceRatio() ~ Float[]Return
| Type | Description |
|---|---|
| Float | ratio array, or Nil if not fitted |
GetMean #
Gets the per-feature means used for centering.
method : public : GetMean() ~ Float[]Return
| Type | Description |
|---|---|
| Float | mean array, or Nil if not fitted |
InverseTransform #
Maps projected data back into the original feature space.
method : public : InverseTransform(Z:Float[,]) ~ Float[,]Parameters
| Name | Type | Description |
|---|---|---|
| Z | Float | projected matrix (rows x num_components) |
Return
| Type | Description |
|---|---|
| Float | reconstruction (rows x features), or Nil if not fitted |
IsFitted #
Whether the decomposition has been fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if fitted |
Load # function
Loads a fitted decomposition from a file.
function : Load(filename:String) ~ PCAParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| PCA | fitted decomposition, or Nil on failure |
New # constructor
Constructor
New(num_components:Int)Parameters
| Name | Type | Description |
|---|---|---|
| num_components | Int | number of principal components to keep |