As you've noticed, the documentation of np.fromiter
explains that the function creates a 1D array. You won't be able to create a 2D array that way, and @unutbu method of returning a 1D array that you reshape afterwards is a sure go.
However, you can indeed create structured arrays using fromiter
, as illustrated by:
>>> import itertools
>>> a = itertools.izip((1,2,3),(10,20,30))
>>> r = np.fromiter(a,dtype=[('',int),('',int)])
array([(1, 10), (2, 20), (3, 30)],
dtype=[('f0', '<i8'), ('f1', '<i8')])
but look, r.shape=(3,)
, that is, r
is really nothing but 1D array of records, each record being composed of two integers. Because all the fields have the same dtype
, we can take a view of r
as a 2D array
>>> r.view((int,2))
array([[ 1, 10],
[ 2, 20],
[ 3, 30]])
So, yes, you could try to use np.fromiter
with a dtype
like [('',int)]*data.shape[1]
: you'll get a 1D array of length size
, that you can then view this array as ((int, data.shape[1]))
. You can use floats instead of ints, the important part is that all fields have the same dtype.
If you really want it, you can use some fairly complex dtype
. Consider for example
r = np.fromiter(((_,) for _ in a),dtype=[('',(int,2))])
Here, you get a 1D structured array with 1 field, the field consisting of an array of 2 integers. Note the use of (_,)
to make sure that each record is passed as a tuple (else np.fromiter
chokes). But do you need that complexity?
Note also that as you know the length of the array beforehand (it's size
), you should use the counter
optional argument of np.fromiter
for more efficiency.