pytorchdata-sciencelinear-algebrapcaprecision-recall

How to evaluate the quality of PCA returned by torch.pca_lowrank()?


I use the following piece of code:

U, S, V = torch.pca_lowrank(A, q=self.n_components)
self.V = V
self.projection = torch.matmul(A, V)

How to compute the cumulative percent variance or any other accuracy metric (single value between 0 and 100%) based on the above values returned? It's ok to project the matrix back with

approx = torch.matmul(self.projection, self.V.T)

if that helps with computing the metric.

I don't mind using other packages compatible with PyTorch.


Solution

  • You can calculate the cumulative percent variance as the ratio between the total variance of the reduced dimension matrix and the total variance of the original matrix.

    total_var = torch.var(A)
    
    total_var_approx = torch.var(approx)
    
    cumulative_percent_variance = (total_var_approx / total_var) * 100