Home > Net >  Reshaping a list containing an array in Python
Reshaping a list containing an array in Python

Time:08-29

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

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

CodePudding user response:

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])

CodePudding user response:

Try this:

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