Reconcile np.fromiter und mehrdimensionale Arrays in Python

Ich liebenp.fromiter vonnumpy weil es ein ressourcenschonender Weg ist, @ zu baunp.array Objekte. Es scheint jedoch, als ob es keine mehrdimensionalen Arrays unterstützt, die auch sehr nützlich sind.

import numpy as np

def fun(i):
    """ A function returning 4 values of the same type.
    """
    return tuple(4*i + j for j in range(4))

# Trying to create a 2-dimensional array from it:
a = np.fromiter((fun(i) for i in range(5)), '4i', 5) # fails

# This function only seems to work for 1D array, trying then:
a = np.fromiter((fun(i) for i in range(5)),
        [('', 'i'), ('', 'i'), ('', 'i'), ('', 'i')], 5) # painful

# .. `a` now looks like a 2D array but it is not:
a.transpose() # doesn't work as expected
a[0, 1] # too many indices (of course)
a[:, 1] # don't even think about it

Wie bekomme icha um ein mehrdimensionales Array zu sein und dabei eine so träge Konstruktion auf der Basis von Generatoren beizubehalten?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage