'I need help to use only 8 out of 10 classes from cifar 10. Normally it loads all 10 classes
I want to perform 8 class classification only and hence need to filter any 8 classes out of 10. Please help. Thank you!
Code to load cifar 10 is below
#Keras library for CIFAR-10 dataset
from keras.datasets import cifar10
#Downloading the CIFAR dataset
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
# I tried the following but its changing the array shape please help
#Train
for i in range(8):
index = np.where(y_train == i)
X_train = x_train[index]
Y_train = y_train[index]
#Test
for i in range(8):
index = np.where(y_test == i)
X_test = x_test[index]
Y_test = y_test[index]
Solution 1:[1]
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
this contains all samples of 10 classes
you can choose index respect to classes
index = np.where(y_train.reshape(-1) == 0)
X_train = X_train[index]
y_train = y_train[index]
This gives all the samples with a label of 0
You can use this technique to load any specific classes you want.
Or if you want to load multiple classes, you can specify multiple conditions, like this:
class_0_index = np.where(y_train.reshape(-1) == 0)
X_train_class_0 = X_train[class_0_index]
y_train_class_0 = y_train[class_0_index]
class_1_index = np.where(y_train.reshape(-1) == 1)
X_train_class_1 = X_train[class_1_index]
y_train_class_1 = y_train[class_1_index]
class_2_index = np.where(y_train.reshape(-1) == 2)
X_train_class_2 = X_train[class_2_index]
y_train_class_2 = y_train[class_2_index]
X_train = np.concatenate((X_train_class_0, X_train_class_1, X_train_class_2))
y_train = np.concatenate((y_train_class_0, y_train_class_1, y_train_class_2)).reshape(-1,1)
This code loads all the samples with a label of 0
, 1
and 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |