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

numpy interation in python without FOR loop

i have a following numpy array.
array([11, 11, 51, 11, 11, 51, 51, 11, 11, 51]) which has a shape (10,)

I want to make it to array([[11], [11], [51], [11], [11], [51], [51], [11], [11], [51]]) and shape should be (10,1).

One way to do it using the for loop, but i think which is not good way to get this done.

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

can someone suggest a more proper way?

thanks

>Solution :

a = np.array([11, 11, 51, 11, 11, 51, 51, 11, 11, 51])
a = a.reshape(-1,1)

(-1,1) means that you want the second shape to be exactly 1 and the first shape will be inferred from the length of the array.

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