Kashif
Kashif

Reputation: 3327

How to convert numpy array of lists into array of tuples

I am trying to convert my array of lists into an array of tuples.

results=

    array([[1.        , 0.0342787 ],
           [0.        , 0.04436508],
           [1.        , 0.09101833 ],
           [0.        , 0.03492954],
           [1.        , 0.06059857]])
    
    results1=np.empty((5,), dtype=object)
    results1[:] = np.array([tuple(i) for i in results])
    results1

I tried the above following the advice given here but I get the error ValueError: could not broadcast input array from shape (5,2) into shape (5).

How do I create a numpy array of tuples from a numpy array of lists?

Upvotes: 1

Views: 673

Answers (4)

hpaulj
hpaulj

Reputation: 231335

Working from the examples in my answer in your link, Convert array of lists to array of tuples/triple

In [22]: results=np.array([[1.        , 0.0342787 ],
    ...:            [0.        , 0.04436508],
    ...:            [1.        , 0.09101833 ],
    ...:            [0.        , 0.03492954],
    ...:            [1.        , 0.06059857]])
In [23]: a1 = np.empty((5,), object)
In [24]: a1[:]= [tuple(i) for i in results]
In [25]: a1
Out[25]: 
array([(1.0, 0.0342787), (0.0, 0.04436508), (1.0, 0.09101833),
       (0.0, 0.03492954), (1.0, 0.06059857)], dtype=object)

or the structured array:

In [26]: a1 = np.array([tuple(i) for i in results], dtype='i,i')
In [27]: a1
Out[27]: 
array([(1, 0), (0, 0), (1, 0), (0, 0), (1, 0)],
      dtype=[('f0', '<i4'), ('f1', '<i4')])

You got the error because you did not follow my answer:

In [30]: a1[:]= np.array([tuple(i) for i in results])
Traceback (most recent call last):
  File "<ipython-input-30-5c1cc6c4105a>", line 1, in <module>
    a1[:]= np.array([tuple(i) for i in results])
ValueError: could not broadcast input array from shape (5,2) into shape (5)

The a1[:]=... assign works for a list, but not for an array.

Note that wrapping the tuple list in an array just reproduces the original results:

In [31]: np.array([tuple(i) for i in results])
Out[31]: 
array([[1.        , 0.0342787 ],
       [0.        , 0.04436508],
       [1.        , 0.09101833],
       [0.        , 0.03492954],
       [1.        , 0.06059857]])

A list of tuples:

In [32]: [tuple(i) for i in results]
Out[32]: 
[(1.0, 0.0342787),
 (0.0, 0.04436508),
 (1.0, 0.09101833),
 (0.0, 0.03492954),
 (1.0, 0.06059857)]

Upvotes: 0

Ruli
Ruli

Reputation: 2780

Why dont do this?:

import numpy as np

results= np.array([[1.        , 0.0342787 ],
           [0.        , 0.04436508],
           [1.        , 0.09101833 ],
           [0.        , 0.03492954],
           [1.        , 0.06059857]])
    
results1 = [tuple(i) for i in results]
results1

Output:

[(1.0, 0.0342787), (0.0, 0.04436508), (1.0, 0.09101833), (0.0, 0.03492954), (1.0, 0.06059857)]

Upvotes: 0

amzon-ex
amzon-ex

Reputation: 1744

Remove np.array() from the assignment step in np.array([tuple(i) for i in results]) and it will work like a breeze. When you pass this list to np.array, the highest possible number of axes is automatically guessed, and your tuples, having pairs of numbers, end up reproducing a (5,2) matrix.

Upvotes: 0

F.NiX
F.NiX

Reputation: 1505

Try this, in order to get an array of tuples as mentioned in title:

import numpy as np
results = np.array([[1.        , 0.0342787 ],
                    [0.        , 0.04436508],
                    [1.        , 0.09101833],
                    [0.        , 0.03492954],
                    [1.        , 0.06059857]])
temp = []
for item in results:
    temp.append(tuple(item))
results1= np.empty(len(temp), dtype=object)
results1[:] = temp
print(results1)
#  array([(1.0, 0.0342787), (0.0, 0.04436508), (1.0, 0.09101833),
#         (0.0, 0.03492954), (1.0, 0.06059857)], dtype=object)

Upvotes: 1

Related Questions