在 Python 中协调 np.fromiter 和多维数组 [英] Reconcile np.fromiter and multidimensional arrays in Python

查看:37
本文介绍了在 Python 中协调 np.fromiter 和多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I love using np.fromiter from numpy because it is a resource-lazy way to build np.array objects. However, it seems like it doesn't support multidimensional arrays, which are quite useful as well.

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 成为一个多维数组,同时保持这种基于生成器的惰性结构?

How can I get a to be a multidimensional array while keeping such a lazy construction based on generators?

推荐答案

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

By itself, np.fromiter only supports constructing 1D arrays, and as such, it expects an iterable that will yield individual values rather than tuples/lists/sequences etc. One way to work around this limitation would be to use itertools.chain.from_iterable to lazily 'unpack' the output of your generator expression into a single 1D sequence of values:

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)

这篇关于在 Python 中协调 np.fromiter 和多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆