Search code examples
scikit-learncomputer-visionpca

Reduce dimension by PCA in sklearn


I want to reduce the dimension of image from (480,640,3) to (1,512) by PCA in sklearn. So I reshape the image to (1, 921600). After then, I perform pca to reduce the dimension. But it changes to (1,1) instead of (1,512)

>>> img.shape
(1, 921600)
>>> pca = PCA(n_components=512)
>>> pca.fit_transform(img).shape
(1, 1)

Could anyone tell me how to reduce the dimension of a single image? Thanks


Solution

  • That's expected. Wiki says (bold annotation by me):

    PCA is a statistical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components (or sometimes, principal modes of variation)

    Fitting PCA on shape (1, 921600) means, that it's one sample with 921600 features.

    sklearn's docs::

    n_components == min(n_samples, n_features)