简体   繁体   中英

How can I reverse .reshape() and get back to a 3D array?

I have a dataset of shape (256, 180, 360). I reshaped it to 2D, removed the 0 values, and applied PCA using:

data = data.reshape(data.shape[0], data.shape[1] * data.shape[2]).T
data = data[~np.all(data == 0, axis = 1)]
# Dataset is now of shape (27719, 256)

data = StandardScaler().fit_transform(data)
pca = PCA()
transformed = pca.fit_transform(data)

Now, the next step is to reshape the transformed dataset back to 3D and plot the PCA results. I tried:

transformed.reshape(360, 180, 256)

which gives me the error "cannot reshape array of size 7096064 into shape (360,180,256)". I understand I cannot get back to the original shape because I removed 0 values which changes that shape, of course, but I have tried other variations of this alongside using variations with the transpose but I cannot get it back to 3D (not necessarily the exact dimensions as before). Any recommendations?

You can't.

What you can do in this scenario is to not use fit_transform , and instead have two separate pipelines. One that uses fit to train on the dataset with all the zero entries removed, and then use transform s on the original dataset to get your transformed data.

flat_data = data.reshape(data.shape[0], data.shape[1] * data.shape[2]).T
nonzero_data = flat_data[~np.all(flat_data == 0, axis = 1)]

scaler = StandardScaler()
pca = PCA()
pca.fit(scaler.fit_transform(nonzero_data))

transformed = pca.transform(scaler.transform(flat_data)).reshape(data.shape)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM