numpy 调和np,Python中的fromiter和多维数组

s4chpxco  于 2023-04-30  发布在  Python
关注(0)|答案(2)|浏览(114)

我喜欢从numpy使用np.fromiter,因为它是一种资源惰性的方式来构建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

我怎样才能让a成为一个多维数组,同时保持这样一个基于生成器的懒惰结构?

bq9c1y66

bq9c1y661#

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)
z18hc3ub

z18hc3ub2#

关于这个问题的简短更新:使用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)

就我个人而言,我发现直接传递数据类型而不是使用字符串更容易阅读(不是'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]])

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

相关问题