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

Reshaping a list containing an array in Python

I want to reshape the list Ii to (1,11,2) but I am getting an error. I present the expected output.

import numpy as np

Ii=[np.array([[0, 2],
        [2, 4],
        [2, 5],
        [2, 6],
        [3, 1],
        [3, 7],
        [4, 5],
        [4, 6],
        [5, 3],
        [5, 7],
        [6, 5]])]

Y=Ii[0].shape
Ii=Ii[0].reshape(1,Y)

The error is

in <module>
    Ii=Ii[0].reshape(1,Y)

TypeError: 'tuple' object cannot be interpreted as an integer

The expected output is

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

array([[[0, 2],
        [2, 4],
        [2, 5],
        [2, 6],
        [3, 1],
        [3, 7],
        [4, 5],
        [4, 6],
        [5, 3],
        [5, 7],
        [6, 5]]])

>Solution :

np.newaxis can be used here to reshape the array.

Ii[0][np.newaxis,:]

or you can use reshaping after unpacking tuple.

Ii[0].reshape([1, *Y])
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