Numpy: Create a 1D array of numpy arrays when all arrays have the same length

前端 未结 4 736
有刺的猬
有刺的猬 2021-01-14 05:48

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((         


        
4条回答
  •  被撕碎了的回忆
    2021-01-14 06:23

    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]
    

提交回复
热议问题