I have a numpy array of shape holding many (200 in this example) monochromatic 64x64 pixel images, thus having the shape:
>>> a.shape
(200L, 1L, 64L, 64L)
I want to split these images in 3 new arrays, a1, a2, a3 where they will contain 80%, 10%, 10% of the images respectively, and I am doing it in the following way (I do not want them to be consecutive in a):
import numpy as np
import random
a = --read images from file--
a1 = numpy.empty((0,1,64,64))
a2 = numpy.empty((0,1,64,64))
a3 = numpy.empty((0,1,64,64))
for i in range(200): #200 is the number of images
temp = a[-1]
a = np.delete(a,-1,0)
rand = random.random()
if rand < 0.8:
a1 = np.append(a1,[temp],0)
elsif rand < 0.9:
a2 = np.append(a2,[temp],0)
else:
a3 = np.append(a3,[temp],0)
I try to emulate pop and append which are done at O(1) time on lists, but does the same hold for numpy arrays?
Is there some way to do this more efficiently (faster) for a large number (thousands) of images?