Click or drag to resize
sqx

SQXPrincipalComponentAnalysisTable Method

Function [table] PrincipalComponentAnalysisTable([Matrix] A)

Namespace:  sqx
Assembly:  SQX (in SQX.dll) Version: 1.0.2.6 (1.0.2.6)
Syntax
C#
public static IEnumerator PrincipalComponentAnalysisTable(
	Matrix A
)

Parameters

A
Type: sqxMatrix
[Matrix]

Return Value

Type: IEnumerator
[table]
Remarks

NOTE: The Covariance Matrix of a standardize table is equal to the Correlation Matrix of the source table.

Math References


James D. McCaffrey (2023) Principal Component Analysis (PCA) From Scratch Using The Classical Technique With C#

https://visualstudiomagazine.com/Articles/2024/01/17/principal-component-analysis.aspx
https://jamesmccaffrey.wordpress.com/2023/12/29/principal-component-analysis-pca-from-scratch-using-classical-technique-with-csharp/

Attributes

IsDeterministic = true, IsPrecise = true, DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None, TableDefinition = "Rotations int,EigenValues Vector,VarianceExplained Vector,EigenVectors Matrix,L Matrix"
Examples
SQL
use [SQX]
go
-- Your query here. (Numeric fields only)
declare @Query nvarchar(max) = N'
select MyField1, MyField2, MyField3, MyFieldN
from MyDB.dbo.MyTable
'

declare @M sqx.Vector, @V sqx.Vector, @Cor sqx.Matrix
declare @Rotation int, @EIGENVECTORS sqx.Matrix
, @EIGENVALUES sqx.Vector, @VAREXPLAINED sqx.Vector
exec sqx.MeanVarCorMatrix @Query = @Query, @Population = 1, @SnapShot = 0
, @Means = @M OUTPUT, @Vars = @V OUTPUT, @Cor = @Cor OUTPUT
select @V = @V.Sqrt(), @Rotation = pcat.Rotations
, @EIGENVECTORS = pcat.EigenVectors, @EIGENVALUES = pcat.EigenValues
, @VAREXPLAINED = pcat.VarianceExplained
from sqx.PrincipalComponentAnalysisTable(@Cor) pcat
-- Show Results
print '** Correlation Matrix'
exec sqx.MatrixToTable @Cor
print '** Means, StDevs, Eigen Values and Variance Explained'
select m.ID FieldNumber, m.Value Mean, s.Value [StDev]
, a.[Value] [EigenValue], b.[Value] [VarianceExplained]
from sqx.VectorTable(@M) m
inner join sqx.VectorTable(@V) s on m.ID = s.ID
inner join sqx.VectorTable(@EIGENVALUES) a on m.ID = a.ID
inner join sqx.VectorTable(@VAREXPLAINED) b on m.[ID] = b.[ID]
order by b.[Value] desc
print '** Eigen Vectors'
exec sqx.MatrixToTable @EIGENVECTORS
See Also