Reshaping a list containing an array in Python

Question:

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

Asked By: user19862793

||

Answers:

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])
Answered By: arunesh

Try this:

Ii = Ii[0].reshape(1, Y[0], Y[1])
Answered By: Behrouz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.