2

I already saw How to combine dimensions in numpy array?

And tried that, but when I tried

imgs.reshape(img.shape[:-2]+(-1))

which I assumed would be the logical extension, I get an error:

can only concatenate tuple (not "int") to tuple

I was going to comment on the accepted answer from the linked question and ask them for help directly instead of creating a new thread for this but I don't have the required reputation to comment.

Edit: For example, I have an input array that is of shape (x,y,8,8) where x and y can change depending on the image that is fed into the function. I want to make it into an array of shape (x,y,64). The 64 in this case never changes.

1
  • provide sample data and expected output Commented Mar 1, 2019 at 4:23

1 Answer 1

6

You must add a comma for it to be recognized as a tuple. Try

imgs.reshape(img.shape[:-2] + (-1,))

Or, use unpacking:

imgs.reshape((*img.shape[:-2], -1))
Sign up to request clarification or add additional context in comments.

1 Comment

I had also tried your first suggestion and it was giving me back (x,y,9) instead of (x,y,64) for some reason. Your second suggestion worked though, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.