How to make a Structured Array from multiple simple array

后端 未结 2 1712
花落未央
花落未央 2021-01-16 02:10
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.d         


        
2条回答
  •  隐瞒了意图╮
    2021-01-16 02:20

    zip does create a list of tuples, which could be memory-intensive if the arrays are big. You could use itertools.izip to be more memory-efficient:

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

    For small arrays of length ~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
    

    For arrays of length ~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
    

    So np.fromiter appears to be slightly faster than np.array.

提交回复
热议问题