Skip to content
Advertisement

How to append 0 and 1 to different classes of images / deep learning

I am a beginner in python and deep learning language, I am creating my list of X_train composed of two different classes of images and I would like to assign 0s and 1s to each class.

So let’s say

X_train=[]
Y_train=[]
X_train.append(imageA)
X_train.append(imageB)

So I would like to assign 0s and 1s to the image classes in my Y_train list I share with you these two lines of code which summarize my reasoning and which are obviously false since I am here looking for solutions

for i, imageA in enumerate (X_train):
    Y_train.append(1)

for j, imageB in enumerate (X_train):
    Y_train.append(0)

What is the correct way to do this? Thank you in advance for your answers.

Advertisement

Answer

This way at the end of the code Y_train will include two lists, the first one is of all the images of type imageB and the second one of all the images of the type imageA

I wasn’t sure how you meant to identify which image is witch so I used is but feel free to replace it with any way you want

zero_lst = []
one_lst = []

for img in X_train:
    if img is imageA:
        one_lst.append(img)

for img in X_train:
    if img is imageB:
        zero_lst.append(img)

Y_train = [zero_lst, one_lst]

If my comment helped you please consider marking it as the answer :)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement