1

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?

2
  • Why? Why not a (5,2) array of floats? Or just a list of tuples? Commented Oct 27, 2020 at 20:38
  • You did not follow the advice given in the link :( Commented Oct 27, 2020 at 23:30

4 Answers 4

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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)]

1 Comment

This is not an array of tuples.
0

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)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.