Reputation: 861
I would like to fill a 3D numpy array which has dimension (?, 100, 100). The first dimension will range between 500 and 1000 but is not known beforehand. I could use the append method, but this is slow.
Is there another posibility to create such a 3D numpy array where the first dimension is not known beforehand? The numpy array is filled using a for loop.
Second, let's assume I have the following two numpy arrays:
import numpy as np
arr1 = np.random.randint(0, 100, size=(30, 100, 100))
arr2 = np.random.randint(0, 100, size=(50, 100, 100))
How can I concatenate arr1
and arr2
along the first dimension so that the resulting array has shape (80, 100, 100)?
Upvotes: 2
Views: 1521
Reputation: 448
About the first question, I always use np.empty and np.append methodes which are totaly fine.
arr = np.empty((0, 100, 100), int)
arr = np.append(arr,x,axis=0)
about the second question, append works well again:
arr3 = np.append(arr1, arr2, axis=0)
also 'concatenate' is usable:
arr3 = np.concatenate((arr1, arr2), axis=0)
Upvotes: 2