0

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
3
  • you don't want to reshape, you want to add dim, try this : I1[None,...] Commented Jun 27, 2022 at 15:16
  • Best way to do this is, as say @I'mahdi adding a new dimension I1[None,:,:] will give you a new array with an aditional axis with length 1. It's the same as I1[np.newaxis,:,:] (new axis it's actually None but in a pretty way to be clear that you are adding an axis). Last but less recommendable is to create a list with I1 and then create array agayn (more computation, less pretty, slower but a posible way) np.array( [I1] ) Commented Jun 27, 2022 at 15:21
  • (1,k) as a tuple is (1, (7,2)). It should be (1,7,2). Commented Jun 27, 2022 at 19:18

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.