简体   繁体   中英

How to Apply PCA to one-dimensional array?

I have flatten() X_train and X_test 32* 32 *3 and I want to apply Principal component analysis (PCA) to reduce the size of the feature to 150 but it's 1D and it gives me an error

x_train,x_test = X_train.flatten(), X_test.flatten()

pca = PCA(n_components=150)
  x_train = pca.fit_transform(x_train)
  x_test = pca.transform(x_test)

I get

ValueError: Expected 2D array, got 1D array instead

x_train shape (50000, 32, 32, 3)
, x_test shape (10000, 32, 32, 3)

after flatten

x_train shape (153600000,)
, x_test shape (30720000,)

fit methods of PCA class expect 2-dimesnional arrays in form (n_samples, n_features) . And ValueError occurs because of applying reshape somewhere inside a method while checking 2D form. Assuming your dataset something like CIFAR-10 (every sample is an image with 3 color channels and size 32 x 32 pixels; 50000 in train set and 10000 test), correct reshape before passing to PCA would be x_train.reshape(-1, 32 * 32 * 3) , analogously with x_test . That way every sample gets flattened into N-dimensional vector, and on bunch of them PCA makes sense.

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