在Python中协调np.fromiter和多维数组

iag*_*ito 11 python arrays numpy lazy-evaluation multidimensional-array

我喜欢使用np.fromiter,numpy因为它是一种构建np.array对象的资源惰性方式.但是,它似乎不支持多维数组,这些数组也非常有用.

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
Run Code Online (Sandbox Code Playgroud)

如何a在保持基于生成器的这种惰性构造的同时成为多维数组?

ali*_*i_m 14

它本身np.fromiter只支持构造一维数组,因此,它期望一个可迭代产生单个值而不是元组/列表/序列等.解决这个限制的一种方法是使用itertools.chain.from_iterable懒惰'解包'输出将您的生成器表达式转换为单个1D值序列:

import numpy as np
from itertools import chain

def fun(i):
    return tuple(4*i + j for j in range(4))

a = np.fromiter(chain.from_iterable(fun(i) for i in range(5)), 'i', 5 * 4)
a.shape = 5, 4

print(repr(a))
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)


Der*_*Weh 1

对问题的简短更新:NumPy=1.23现在可以完全执行示例中给出的操作:

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)), dtype='4i', count=5)
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

就我个人而言,我发现直接传递数据类型而不是使用字符串更具可读性(不是'i'结果int32而不是标准int64):

a = np.fromiter((fun(i) for i in range(5)), dtype=np.dtype((int, 4)), count=5)
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19]])
Run Code Online (Sandbox Code Playgroud)

另请参阅fromiter的文档,其中包含类似的示例。