Home > database >  List to Numpy array
List to Numpy array

Time:05-18

Python newbie here. I have a list of arrays of size 9. Each element of the list contains a numpy array of size (1,7). How do I convert the list into a numpy array of size (9,7)?

Thanks!

CodePudding user response:

How to put a list of horizontal vectors into one array

np.array(a).reshape(len(a), a[0].shape[1])
np.vstack(a)
np.r_[(*a,)]

np.array(a).squeeze() is applicable only if you know that 100x100

At large sizes, the speed of work becomes comparable, e.g. 1000 x 1000:

enter image description here

Given large enough sizes, reshape and vstack are comparable and perform better than others, e.g. 10 000 x 10 000:

enter image description here

As far as np.array(...).reshape(...) has shown equally good results at different sizes, it seems like a good choice. Although I would prefer vstack as a more clear and reasonably performant option.


p.s. Of course, performance depends on the hardware and may vary. I want to show that there is no silver bullet in this case.

  • Related