Home > Back-end >  numpy array of list to 2d array
numpy array of list to 2d array

Time:11-05

>>> print(a)
 
array([list([52, 584, 78]), list([35, 140, 584])], dtype=object)

I would like to convert above data to numpy 2d array with int, but when I enter np.array(a), the result was the same as before. How can I convert type?

CodePudding user response:

You want to use np.stack

>>> import numpy as np
>>> a = ([1,2,3,4,5])
>>> b = ([2,3,4,5,6])
>>> c = ([3,4,5,6,7])

>>> np.array([a, b, c])
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7]])

>>> np.stack([a, b, c], axis=0)
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7]])
  • Related