numpy fromiter与列表生成器

时间:2015-10-07 16:02:19

标签: python arrays numpy generator

import numpy as np
def gen_c():
    c = np.ones(5, dtype=int)
    j = 0
    t = 10
    while j < t:
        c[0] = j
        yield c.tolist()
        j += 1 

# What I did:
# res = np.array(list(gen_c())) <-- useless allocation of memory

# this line is what I'd like to do and it's killing me
res = np.fromiter(gen_c(), dtype=int) # dtype=list ?

错误说ValueError: setting an array element with a sequence.

这是一段非常愚蠢的代码。我想从生成器创建一个列表数组(最后是一个2D数组)......

虽然我到处搜索,但我仍然无法弄清楚如何使它工作。

2 个答案:

答案 0 :(得分:3)

您只能使用numpy.fromiter()创建一维数组(而不是二维数组),如documentation of numpy.fromiter中所示 -

  

numpy.fromiter(iterable,dtype,count = -1)

     

从可迭代对象创建一个新的1维数组。

您可以做的一件事是将生成器函数转换为从c中提供单个值,然后从中创建一个数组,然后将其重新整形为(-1,5)。示例 -

import numpy as np
def gen_c():
    c = np.ones(5, dtype=int)
    j = 0
    t = 10
    while j < t:
        c[0] = j
        for i in c:
            yield i
        j += 1

np.fromiter(gen_c(),dtype=int).reshape((-1,5))

演示 -

In [5]: %paste
import numpy as np
def gen_c():
    c = np.ones(5, dtype=int)
    j = 0
    t = 10
    while j < t:
        c[0] = j
        for i in c:
            yield i
        j += 1

np.fromiter(gen_c(),dtype=int).reshape((-1,5))

## -- End pasted text --
Out[5]:
array([[0, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [2, 1, 1, 1, 1],
       [3, 1, 1, 1, 1],
       [4, 1, 1, 1, 1],
       [5, 1, 1, 1, 1],
       [6, 1, 1, 1, 1],
       [7, 1, 1, 1, 1],
       [8, 1, 1, 1, 1],
       [9, 1, 1, 1, 1]])

答案 1 :(得分:0)

正如文档建议的那样,np.fromiter()只接受一维迭代。 您可以先使用itertools.chain.from_iterable()展开可迭代,然后再np.reshape()将其展开:

import itertools
import numpy as np

def fromiter2d(it, dtype):

    # clone the iterator to get its length
    it, it2 = itertools.tee(it)
    length = sum(1 for _ in it2)

    flattened = itertools.chain.from_iterable(it)
    array_1d = np.fromiter(flattened, dtype)
    array_2d = np.reshape(array_1d, (length, -1))
    return array_2d

演示:

>>> iter2d = (range(i, i + 4) for i in range(0, 12, 4))

>>> from_2d_iter(iter2d, int)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

仅在Python 3.6上测试过,但也应该与Python 2一起使用。