Adding numpy to the array in a loop is inefficient; there may be situations where they cannot be avoided, but this does not seem to be one of them. If you know the size of the array you are going to end in, the best thing is to simply allocate the array, something like this:
data = np.zeros([N, 4114]) for i in range(1, N): filename = splitpath+str(i)+'.tiff' feature = featureselection(filename) data[i] = feature
Sometimes you donโt know the size of the final array. There are several ways to deal with this case, but the simplest is probably to use a temporary list, for example:
data = [] for i in range(1,N): filename = splitpath+str(i)+'.tiff' feature = featureselection(filename) data.append(feature) data = np.array(data)
Just for completeness, you can also do data = np.zeros([0, 4114]) , but I would recommend against this and suggest one of the methods above.
source share