Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List of List of Tuples Into 2d Numpy Array

Tags:

python

numpy

I have a list of list of tuples:

X = [[(0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], [(0.5, 0.5, 0.52), (1.0, 1.0, 1.0)], [(0.5, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)]]

and would like to convert it to a 2D numpy array. I tried the following but didn't work. I just want to make sure the new transformed 2D keeps the same shape and structure of X. Please help me , thanks a lot.

import numpy as np
X = [[(0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], [(0.5, 0.5, 0.52), (1.0, 1.0, 1.0)], [(0.5, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)]]
np.asarray([sublist for sublist in X])

Expected result would be:

[[ 0.5 ,  0.5 ,  0.5 ],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.5 ,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]]
like image 659
aBiologist Avatar asked Oct 01 '16 11:10

aBiologist


2 Answers

Dont know if you are working on python 2.x or 3.x but in 3.x you could try:

>> import numpy as np
>> X = [(....)] # Your list
>> array = np.array([*X])
>> print(array)
array([[[ 0.5 ,  0.5 ,  0.5 ],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.5 ,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]]])

Dont know if this is what you want to achieve.

like image 133
Nf4r Avatar answered Oct 30 '22 15:10

Nf4r


If I do the normal thing to your list I get a 3d array:

In [38]: np.array(X)
Out[38]: 
array([[[ 0.5 ,  0.5 ,  0.5 ],
        [ 1.  ,  1.  ,  1.  ]],

       [[ 0.5 ,  0.5 ,  0.52],
        [ 1.  ,  1.  ,  1.  ]],

       [[ 0.5 ,  0.52,  0.52],
        [ 1.  ,  1.  ,  1.  ]],

       [[ 0.52,  0.52,  0.52],
        [ 1.  ,  1.  ,  1.  ]],

       [[ 0.52,  0.52,  0.52],
        [ 1.  ,  1.  ,  1.  ]]])
In [39]: _.shape
Out[39]: (5, 2, 3)

The print (str) display looks a lot like your desired result - except for the extra set of [] to enclose the whole thing:

In [40]: print(__)
[[[ 0.5   0.5   0.5 ]
  [ 1.    1.    1.  ]]

 [[ 0.5   0.5   0.52]
  [ 1.    1.    1.  ]]

 [[ 0.5   0.52  0.52]
  [ 1.    1.    1.  ]]

 [[ 0.52  0.52  0.52]
  [ 1.    1.    1.  ]]

 [[ 0.52  0.52  0.52]
  [ 1.    1.    1.  ]]]

I could split it into a list of 5 2d arrays, but the display is rather different:

In [43]: np.split(np.array(X),5)
Out[43]: 
[array([[[ 0.5,  0.5,  0.5],
         [ 1. ,  1. ,  1. ]]]), array([[[ 0.5 ,  0.5 ,  0.52],
         [ 1.  ,  1.  ,  1.  ]]]), array([[[ 0.5 ,  0.52,  0.52],
        ...]

A list comprehension turning each sublist into an array does the same thing, [np.array(x) for x in X]

I could also reshape the (5,2,3) array into 2d arrays, (10,3) or (5,6) for example, but they won't look like your target.

Is the tuple layer important (as opposed to being a list)? I could preserve it (appearance wise) with a structured array: np.array(X, dtype=('f,f,f')).

like image 43
hpaulj Avatar answered Oct 30 '22 13:10

hpaulj