Raff
Raff

Reputation: 27

Numpy array in array with unequal length

I am wondering how to create a numpy array in a numpy array. Each array in the main array should be able to have a different length. The main array should be the index number to the array object inside.

I already have an example of this type of array but I want to re-create something similar.

Example:

test.shape
(200,)

test[0].shape
(5, 10)

test[1].shape    
(3, 10)

test[2].shape    
(6, 10)

Upvotes: 1

Views: 5002

Answers (2)

hpaulj
hpaulj

Reputation: 231385

The most reliable way of creating an object array is to initialize it and fill it. The behavior of np.array is too variable.

In [658]: alist = [np.ones((5,10),int), np.zeros((3,10),int), np.arange(60).resh
     ...: ape(6,10)]
In [659]: arr = np.empty(len(alist), dtype=object)
In [660]: arr[:] = alist
In [661]: arr
Out[661]: 
array([ array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]),
       array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]),
       array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])], dtype=object)

The behavior of np.array varies with the relative shape of the components:

Nice object array:

In [668]: np.array((np.ones((3,5),int), np.ones((2,5),int)),object)
Out[668]: 
array([array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]]),
       array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])], dtype=object)

3d array:

In [669]: np.array((np.ones((3,5),int), np.ones((3,5),int)),object)
Out[669]: 
array([[[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]]], dtype=object)

error

In [670]: np.array((np.ones((3,4),int), np.ones((3,5),int)),object)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-670-e9b41001d868> in <module>()
----> 1 np.array((np.ones((3,4),int), np.ones((3,5),int)),object)

ValueError: could not broadcast input array from shape (3,4) into shape (3)

Upvotes: 2

jakevdp
jakevdp

Reputation: 86330

You need to create an array with dtype=object, so that numpy knows to treat each entry as a Python object rather than treating the entire nested list as a single array.

For example:

import numpy as np
x = np.empty((5, 10))
y = np.empty((3, 10))
z = np.empty((6, 10))

test = np.array([x, y, z], dtype=object)
test[0].shape
# [5, 10]

Upvotes: 0

Related Questions