Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,)

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)

EDIT: 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,).

like image 494
user2685230 Avatar asked Jan 01 '26 10:01

user2685230


1 Answers

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]
like image 82
Paul Panzer Avatar answered Jan 02 '26 23:01

Paul Panzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!