I have an iterable of tuples, and I'd like to build an ndarray from it. Say that the shape would be (12345, 67890). What would be an efficient and elegant way to do so?
Here are a few options, and why I ruled them out:
np.array(my_tuples)starts allocating the array before it knows the size, which requires inefficient relocations according to NumPy's documentation.Create an array with uninitialized content using
np.ndarray((12345, 67890))and then do a loop that populates it with data. It works and it's efficient, but a bit inelegant because it requires multiple statements.Use
np.fromiterwhich appears to be geared towards 1-dimensional arrays only.
Does anyone have a better solution?
(I've seen this question, but I'm not seeing any promising answers there.)
np.array(list(your_generator))is straight forward, and probably as efficient as any.np.stack([np.array(row) for row in generator])might time better (or not), ornp.concatenate([np.atleast_2d(row) for row in generator]). etc.np.zerosornp.emptyto create an 'uninitialized' array, notnp.ndarray.