如何从多个简单数组中制作结构化数组

时间:2011-11-22 01:00:07

标签: python numpy

import numpy as np

a=np.array([1,2,3,4,5,6,7,8,9])
b=np.array(["a","b","c","d","e","f","g","h","i"])
c=np.array([9,8,7,6,5,4,3,2,1])
datatype=np.dtype({
 'names':['num','char','len'],
 'formats':['i','S32','i']
})

d=np.array(zip(a,b,c),dtype=datatype)

上面的代码首先使用zip()创建一个列表,然后将其转换为结构化数组。它的效率很低,我想知道是否有任何内置函数可以在NumPy中执行此操作。

2 个答案:

答案 0 :(得分:2)

zip会创建一个元组列表,如果数组很大,这可能是内存密集型的。您可以使用itertools.izip来提高内存效率:

import itertools
d=np.fromiter(itertools.izip(a,b,c),dtype=datatype)

对于长度约为10的小数组:

In [68]: %timeit np.fromiter(itertools.izip(a,b,c),dtype=datatype)
100000 loops, best of 3: 15.8 us per loop

In [69]: %timeit np.array(zip(a,b,c),dtype=datatype)
10000 loops, best of 3: 20.8 us per loop

对于长度为~10000的数组:

In [72]: A=np.tile(a,1000)
In [74]: B=np.tile(b,1000)
In [75]: C=np.tile(c,1000)

In [83]: %timeit np.fromiter(itertools.izip(A,B,C),dtype=datatype)
100 loops, best of 3: 10.7 ms per loop

In [84]: %timeit np.array(zip(A,B,C),dtype=datatype)
100 loops, best of 3: 12.7 ms per loop

所以np.fromiter似乎比np.array略快。

答案 1 :(得分:2)

您也可以尝试numpy.rec.fromarrays

import numpy as np

a=np.array([1,2,3,4,5,6,7,8,9])
b=np.array(["a","b","c","d","e","f","g","h","i"])
c=np.array([9,8,7,6,5,4,3,2,1])

d = np.rec.fromarrays([a,b,c], formats=['i','S32','i'], names=['num','char','len'])

虽然时间不如使用itertools

In [2]: %timeit d = np.rec.fromarrays([a,b,c], formats=['i','S32','i'], names=['num','char','len'])
10000 loops, best of 3: 86.5 us per loop

In [6]: import itertools

In [7]: %timeit np.fromiter(itertools.izip(a,b,c),dtype=datatype)
100000 loops, best of 3: 11.5 us per loop