Search code examples
pythonarraysnumpyscikit-learnpca

PCA implementation on 3D numpy array


I have a feature set of size 2240*5*16. 2240 are number of samples, 5 represents number of channels and 16 shows # of statistical features extracted such as mean, variance, etc. Now, I want to apply PCA. However, PCA is applicable on 2D array. I applied the following code:

from sklearn.decomposition import PCA
pca = PCA(n_components=5)
pca.fit(features)

I get the following error.

ValueError: Found array with dim 3. Estimator expected <= 2.

It doesn't support axis argument. As it is only applicable on 2D, how can I utilize it on my case (3D)? Any suggestion, if I want to reduce the dimensions from 2240*5*16 to 2240*5*5, please?


Solution

  • I would just loop over each channel and do PCA separately.

    import numpy as np
    from sklearn.decomposition import PCA
    
    X = np.random.rand(1000, 5, 10)
    
    X_transform = np.zeros((X.shape[0], 5, 5))
    for i in range(X.shape[1]):
    
        pca = PCA(n_components=5)
        f  = pca.fit_transform(X[:, i, :])
    
        X_transform[:, i, :] = f
    
    print((X_transform.shape))