使用itertools创建numpy数组

8
我可以使用itertools的各种函数来创建numpy数组。我可以很容易地提前计算出乘积、组合、排列等元素的数量,因此分配空间不应该是一个问题。
例如:
coords = [[1,2,3],[4,5,6]]
iterable = itertools.product(*coords)
shape = (len(coords[0]), len(coords[1]))
arr = np.iterable_to_array(
    iterable, 
    shape=shape, 
    dtype=np.float64, 
    count=shape[0]*shape[1]
) #not a real thing
answer = np.array([
    [1,4],[1,5],[1,6],
    [2,4],[2,5],[2,6],
    [3,4],[3,5],[3,6]])
assert np.equal(arr, answer)

2
那么,arr = np.array(list(iterable)) 对你来说不起作用,是有原因的吗?你可能在寻找 np.formiter,但是它在处理多维数组方面表现不佳,至少我上次尝试时是这样。 - juanpa.arrivillaga
我也可以创建一个零数组,然后填充各个值。这可能会更快:http://forthescience.org/blog/2015/06/07/performance-of-filling-a-numpy-array/...只是好奇是否有一种很好的方法让numpy完成这项工作,因为在Python中iterables随处可见。 - Him
1
不幸的是,据我所知,只有支持从可迭代对象构建一维数组。请查看此交换:https://mail.scipy.org/pipermail/numpy-discussion/2007-August/028898.html 实际上,他们建议使用 empty - juanpa.arrivillaga
我早有所料。谢谢! - Him
1个回答

8
这里有几种使用numpy生成包含这些值的数组的方法。
In [469]: coords = [[1,2,3],[4,5,6]]
In [470]: it = itertools.product(*coords)
In [471]: arr = np.array(list(it))
In [472]: arr
Out[472]: 
array([[1, 4],
       [1, 5],
       [1, 6],
       [2, 4],
       [2, 5],
       [2, 6],
       [3, 4],
       [3, 5],
       [3, 6]])

fromiter 函数需要与适当的结构化 dtype 配合使用:

In [473]: it = itertools.product(*coords)
In [474]: arr = np.fromiter(it, dtype='i,i')
In [475]: arr
Out[475]: 
array([(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5),
       (3, 6)], 
      dtype=[('f0', '<i4'), ('f1', '<i4')])

通常我们使用 numpy 提供的工具来生成序列和网格。 np.arange 在很多地方都被使用。

meshgrid 被广泛使用。经过一些试错,我发现我可以转置它的输出,并产生相同的序列:

In [481]: np.transpose(np.meshgrid(coords[0], coords[1], indexing='ij'), (1,2,0)).reshape(-1,2)
Out[481]: 
array([[1, 4],
       [1, 5],
       [1, 6],
       [2, 4],
       [2, 5],
       [2, 6],
       [3, 4],
       [3, 5],
       [3, 6]])

repeattile 也对像这样的任务非常有用:

In [487]: np.column_stack((np.repeat(coords[0],3), np.tile(coords[1],3)))
Out[487]: 
array([[1, 4],
       [1, 5],
       [1, 6],
       [2, 4],
       [2, 5],
       [2, 6],
       [3, 4],
       [3, 5],
       [3, 6]])

我过去对 fromiter 进行了一些计时。 我记得它仅比 np.array 提供了适度的时间节省。
以前我探索了 itertools 和fromiter ,并找到了一种使用 itertools.chain 将它们组合起来的方法。 将 itertools 数组转换为 numpy 数组
In [499]: it = itertools.product(*coords)
In [500]: arr = np.fromiter(itertools.chain(*it),int).reshape(-1,2)
In [501]: arr
Out[501]: 
array([[1, 4],
       [1, 5],
       [1, 6],
       [2, 4],
       [2, 5],
       [2, 6],
       [3, 4],
       [3, 5],
       [3, 6]])

这个可以运行,但是现在必须重新编写numpy的itertools包。 - Him
1
我发现了一个较早的答案,使用itertools.chain来压平product - hpaulj
那相当不错。 - Him
如果您将其简化为仅涉及该内容,我会接受您的答案。 - Him
你可以将其拆分为多个答案,以便Scott可以接受他喜欢的答案。 - endolith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接