jeudi 28 mai 2020

replace random.sample with numpy.random python

I have 40 folders each with 10 images. this code splits each image folder in half and from those 10 images 5 are choosen in random for training and 5 for testing

for Class in allClasses:
    path = folderPath + '/' + Class
    allImages = os.listdir(path)
    imageCount = len(allImages)
    # seperating training images
    randomlist = random.sample(range(imageCount), (int)(imageCount / 2))
    trainingImagesForClass = [allImages[i] for i in randomlist]
    trainingImages.append([readImage(path + '/' + i) for i in trainingImagesForClass])
    # seperating test images
    testList = [x for x in range(imageCount) if x not in randomlist]
    testImagesForClass = [allImages[i] for i in testList]
    testImages.append([readImage(path + '/' + i) for i in testImagesForClass])

This code works perfectly fine but i want to implement this using numpy random




Aucun commentaire:

Enregistrer un commentaire