6

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.  ]]
aBiologist
  • 2,007
  • 2
  • 14
  • 21
  • 2
    Your data can't be converted to a 2D array as it is. Whats your expected result? – Mazdak Oct 01 '16 at 11:49
  • 1
    "It did not work" is not a problem description. – timgeb Oct 01 '16 at 11:49
  • The current structure of `X` lends itself to an array of shape `(5,2,3)`. There are then a number of ways that one could turn this into a 2D array, so please specify what your expected result is. – Angus Williams Oct 01 '16 at 12:02
  • That `Expected result` is not one array. – Divakar Oct 01 '16 at 12:14
  • Updated expected result. @Divakar, what is it then ? – aBiologist Oct 01 '16 at 12:18
  • 1
    5 arrays. To have one array, you should have one starting bracket at the start of the array and a matching ending bracket at the end of the array definition. You should look into how multi-dim arrays are stored. – Divakar Oct 01 '16 at 12:22
  • i got the same issue, after loading with np.genfromtxt. I want to convert into a 2D array of numbers rather than 1D array of tuples – mhstnsc Oct 26 '17 at 12:14

2 Answers2

6

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.

Nf4r
  • 1,390
  • 1
  • 7
  • 9
  • 1
    your not using my input, please refer back to my input. Also the expected output should be 2D array not 3D which what is my question is about!!! – aBiologist Oct 01 '16 at 12:46
  • 1
    First of all: There is a comment on the 2nd line, read it. 2nd: How do you expect to get ONE 2D array, when your results are 5 2D arrays? If all you want is FIVE 2D arrays, just use: arrays = [np.asarray(sublist) for sublist in X] – Nf4r Oct 01 '16 at 12:53
1

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')).

hpaulj
  • 221,503
  • 14
  • 230
  • 353