numpy:在所有数组都具有相同长度的情况下,创建一维numpy数组 [英] Numpy: Create a 1D array of numpy arrays when all arrays have the same length

查看:135
本文介绍了numpy:在所有数组都具有相同长度的情况下,创建一维numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将现有的2D数组转换为1D数组.我唯一能找到的方法就是使用类似的东西:

I want to be able to convert an existing 2D array to a 1D array of arrays. The only way I can find is to use something like:

my_2d_array = np.random.random((5, 3))
my_converted_array = np.zeros(len(my_2d_array), dtype='O')
for i, row in enumerate(my_converted_array):
    my_converted_array[i] = row

是否有更快/更清洁的方法?

Is there a faster/cleaner method of doing this?

如果内部数组具有不同的形状,则有可能,例如:

If the inner arrays have different shapes it is possible, for example:

my_1d_array = np.array([
    np.array([0, 1], dtype=np.float),
    np.array([2], dtype=np.float)
], dtype='O')
assert my_array.shape == (2,)

但是,如果数组长度相同,那么numpy会自动将其设为2D数组:

But if the arrays are the same length numpy automatically makes it a 2D array:

my_2d_array = np.array([
    np.array([0, 1], dtype=np.float),
    np.array([2, 3], dtype=np.float)
], dtype='O')
assert my_array.shape == (2, 2)

为了澄清一些答案,我不能使用flattenreshaperavel,因为它们将保持相同数量的元素.相反,我想从形状为(N, M)的2D数组变为形状为(N,)的对象(1D数组)的1D数组,每个对象的形状均为(M,).

To clarify for some answers, I can't use flatten, reshape or ravel as they would maintain the same number of elements. Instead I want to go from a a 2D array with shape (N, M) to a 1D array with shape (N,) of objects (1D arrays), which each have shape (M,).

推荐答案

这是使用np.frompyfunc的一种方法,该方法比您的输入类型少,并且速度可比-小数组看起来大致相同,而大数组则更快:

Here's one method using np.frompyfunc that is a bit less typing than yours and comparable in speed - it seems roughly the same for small arrays but faster for large ones:

>>> import numpy as np
>>> 
>>> def f_empty(a):
...     n = len(a)
...     b = np.empty((n,), dtype=object)
...     for i in range(n):
...         b[i] = a[i]
...     return b
... 
>>> def f_fpf(a):
...     n = len(a)
...     return np.frompyfunc(a.__getitem__, 1, 1)(np.arange(n))
... 
>>> def f_fpfl(a):
...     n = len(a)
...     return np.frompyfunc(list(a).__getitem__, 1, 1)(np.arange(n))
... 

>>> from timeit import repeat
>>> kwds = dict(globals=globals(), number=10000)

>>> a = np.random.random((10, 20))
>>> repeat('f_fpf(a)', **kwds)
[0.04216550011187792, 0.039600114803761244, 0.03954345406964421]
>>> repeat('f_fpfl(a)', **kwds)
[0.05635825078934431, 0.04677496198564768, 0.04691878380253911]
>>> repeat('f_empty(a)', **kwds)
[0.04288528114557266, 0.04144620103761554, 0.041292963083833456]

>>> a = np.random.random((100, 200))
>>> repeat('f_fpf(a)', **kwds)
[0.20513887284323573, 0.2026138547807932, 0.20201953873038292]
>>> repeat('f_fpfl(a)', **kwds)
[0.21277308696880937, 0.18629810912534595, 0.18749701930209994]
>>> repeat('f_empty(a)', **kwds)
[0.2321561980061233, 0.24220682680606842, 0.22897077212110162]

>>> a = np.random.random((1000, 2000))
>>> repeat('f_fpf(a)', **kwds)
[2.1829855730757117, 2.1375885657034814, 2.1347726942040026]
>>> repeat('f_fpfl(a)', **kwds)
[1.8276268909685314, 1.8227900266647339, 1.8233762909658253]
>>> repeat('f_empty(a)', **kwds)
[2.5640305397100747, 2.565472401212901, 2.4353492129594088]

这篇关于numpy:在所有数组都具有相同长度的情况下,创建一维numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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