1

I have a multidimensional list of tuples of different sizes in a list and I am trying to convert that to a 1-D array, but I keep getting an error. If my list looks like:

rain2 = [[(298.0, 345.0, 412.0)], [(421.0, 203.0)], [(491.0,)]]

How do I convert that to a 1-D array? I tried:

chain = np.asarray(rain2, dtype=float)

But I get the error:

ValueError: setting an array element with a sequence.

Thanks!

MAJ
  • 437
  • 5
  • 17

2 Answers2

0

You could use np.ravel along with np.hstack to convert it to 1D array:

import numpy as np

rain2 = [[(298.0, 345.0, 412.0)], [(421.0, 203.0)], [(491.0)]]
chain = np.hstack(np.ravel(rain2))
print(chain)

Output:

[298. 345. 412. 421. 203. 491.]
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
0

Also check this below:

np.reshape(chain, (chain.size,1))

Jimmys
  • 357
  • 1
  • 3
  • 14