Qbik
Qbik

Reputation: 6147

Convert ndarray of lists to ndarray

nda.shape is (2,2), convert it to be (2,2,2)

dtypes = [('a', np.float64), ('b', object)]
nda = np.zeros((2,2), dtype = dtypes)

nda['b'][0,0] = [1,2]
nda['b'][1,0] = [2,3]
nda['b'][0,1] = [3,4]
nda['b'][1,1] = [9,5]

Solution should give : nda['b'][0,0,1]==2, nda['b'][1,1,0]==9 etc.

Upvotes: 0

Views: 96

Answers (2)

hpaulj
hpaulj

Reputation: 231325

You've created an odd structure; you can't simply reshape it:

In [1]: dtypes = [('a', np.float64), ('b', object)] 
   ...: nda = np.zeros((2,2), dtype = dtypes) 
   ...:  
   ...: nda['b'][0,0] = [1,2] 
   ...: nda['b'][1,0] = [2,3] 
   ...: nda['b'][0,1] = [3,4] 
   ...: nda['b'][1,1] = [9,5]   

It has 2 fields, one with numbers, the other with lists:

In [2]: nda                                                                     
Out[2]: 
array([[(0., list([1, 2])), (0., list([3, 4]))],
       [(0., list([2, 3])), (0., list([9, 5]))]],
      dtype=[('a', '<f8'), ('b', 'O')])

The list field:

In [3]: nda['b']                                                                
Out[3]: 
array([[list([1, 2]), list([3, 4])],
       [list([2, 3]), list([9, 5])]], dtype=object)
In [4]: _.shape                                                                 
Out[4]: (2, 2)

If converted to 1d, we can stack (or otherwise combine with concatenate):

In [5]: nda['b'].ravel()                                                        
Out[5]: 
array([list([1, 2]), list([3, 4]), list([2, 3]), list([9, 5])],
      dtype=object)
In [6]: np.stack(nda['b'].ravel())                                              
Out[6]: 
array([[1, 2],
       [3, 4],
       [2, 3],
       [9, 5]])
In [7]: np.stack(nda['b'].ravel()).reshape(2,2,2)                               
Out[7]: 
array([[[1, 2],
        [3, 4]],

       [[2, 3],
        [9, 5]]])

In general if you have a object dtype array of lists or arrays, it can consolidated into one (numeric) array with sort version of concatenate, but it has to be 1d, an 'iterable' of arrays/lists.

And, yes, unpacking the field into a nested list produces something that can be converted back to a (2,2,2) array:

In [14]: _2['b'].tolist()                                                       
Out[14]: [[[1, 2], [3, 4]], [[2, 3], [9, 5]]]

(You can't simply put these arrays (or lists) back into the nda array. The dtype is wrong.)

With a different dtype (`b field is 2 integers, not the more generic object):

In [10]: dtypes = [('a', np.float64), ('b', int, (2,))] 
    ...: nda = np.zeros((2,2), dtype = dtypes) 
    ...:  
    ...: nda['b'][0,0] = [1,2] 
    ...: nda['b'][1,0] = [2,3] 
    ...: nda['b'][0,1] = [3,4] 
    ...: nda['b'][1,1] = [9,5]                                                  
In [11]: nda                                                                    
Out[11]: 
array([[(0., [1, 2]), (0., [3, 4])],
       [(0., [2, 3]), (0., [9, 5])]],
      dtype=[('a', '<f8'), ('b', '<i8', (2,))])
In [12]: nda['b']                                                               
Out[12]: 
array([[[1, 2],
        [3, 4]],

       [[2, 3],
        [9, 5]]])

Upvotes: 1

Qasim Khan
Qasim Khan

Reputation: 154

Try the following

nda = np.resize(nda, (2,2,2))
nda.shape

Results

(2,2,2)

Upvotes: 0

Related Questions