Home > other >  Reshaping an array in Python
Reshaping an array in Python

Time:06-28

I am trying to reshape I1 from (7,2) to (1,7,2) by means of a variable k which has the shape of old I1. But I get an error. Is there a way to do it?

import numpy as np

I1= np.array([[0, 1],
              [0, 2],
              [1, 3],
              [2, 3],
              [2, 5],
              [3, 6],
              [5, 6]])
k=I1.shape
I1=I1.reshape(1,k)

The error is

<module>
    I1=I1.reshape(1,k)

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

CodePudding user response:

Because k is a tuple with 2 elements, when it's passed to a function, an asterisk * is needed.

Instead of I1 = I1.reshape(1, k), try I1 = I1.reshape(1, *k)

  • Related