SashimiDélicieux
SashimiDélicieux

Reputation: 485

How to inverse PCA without one component?

I want to denoise signals by applying PCA, then deleting one component and inversing PCA back to have denoised signals. Here's what I tried :

reduced = pca.fit_transform(signals)
denoised = np.delete(reduced, 0, 1)
result = pca.inverse_transform(denoised)

But I have the error :

ValueError: shapes (11,4) and (5,5756928) not aligned: 4 (dim 1) != 5 (dim 0)

How can I invert PCA ?

Upvotes: 1

Views: 1198

Answers (1)

seralouk
seralouk

Reputation: 33147

To remove noise, first fit the PCA for a number of components (pca = PCA(n_components=2)). Then, look at the eigenvalues and identify components that are noise.

After identifying these noisy components (write this does), transform the whole dataset.

Example:

import numpy as np
from sklearn.decomposition import PCA


X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA(n_components=2)
pca.fit(X)

eigenvalues = pca.explained_variance_
print(eigenvalues)
#[7.93954312 0.06045688] # I assume that the 2nd component is noise due to λ=0.06 << 7.93

X_reduced = pca.transform(X)

#Since the 2nd component is considered noise, keep only the projections on the first component
X_reduced_selected = X_reduced[:,0]

And to invert use this:

pca.inverse_transform(X_reduced)[:,0]

Upvotes: 1

Related Questions