Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert 1d numpy array to 2d

I have an array A of shape (30,) where each row has a list with 2000 elements. I want to convert this into a 2d array of shape (30, 2000). This is what I tried

A = np.reshape(A, (30, -1))

But, running this gives me an array of shape (30, 1) rather than (30, 2000). What should I do to get the correct shape?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

where each row has a list with 2000 elements

As Ahmed Mohamed AEK points out in the comments this won’t work as the numpy object is of shape (30,). One easy fix is to stack them into a 30 by 2000 np.array.
For example:

A = np.vstack(A)

or equvalently:

A = np.stack(A, axis=0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading