Search code examples
pythonnumpymatrix3dpca

How can I apply PCA dimensionality reduction to a 3D matrix?


I want to apply PCA dimensionality reduction on a 3D matrix (69,2640,7680). I have 69 2D matrices each of them has a size (2640,7680). I want to apply PCA on those matrices as a 3D matrix (69,2640,7680). I don't how to do this.

Any help would be appreciated.

code:

    data=np.load('Normal_windows.npy')
    pca = PCA(n_components=1000)
    pca.fit(data)
    data_pca = pca.transform(data)
    print("original shape:   ", data.shape) ##(69,2640,7680)
    print("transformed shape:", data_pca.shape) 
 

Solution

  • PCA works on features if I understand correctly you have 69 items with (2640,7680) features right?

    If that is the case then you can just flatten the last two dimensions (something like:

    data_2d = np.array([features_2d.flatten() for features_2d in data])
    pca = PCA(n_components=1000)
    pca.fit(data_2d)
    data_pca = pca.transform(data_2d)
    print("original shape:   ", data_2d.shape) ##(69,2640*7680)
    print("transformed shape:", data_pca.shape)