1

What is different between x.shape[0] and x.shape in numpy? I code without [0] then have a bug: "TypeError: arange: scalar arguments expected instead of a tuple.", but when I add [0] in, my code runs completely.

And why i can't type x.shape[1] or x.shape[1000]?

Looking forward to receiving answers from everyone, many thanks!!

  • The question doesn't make sense, you're just taking an index. Why do you think `x.shape[1000]` would give a meaningful result? What result/information do you expect it to give? – roganjosh Oct 06 '17 at 18:02
  • @Divakar: I don't see what this question has to do with slice notation. This question may well be a duplicate, but that doesn't look like a good duplicate target. – Mark Dickinson Oct 06 '17 at 18:06
  • @MarkDickinson Yeah, the context is missing there, but it's all about slicing if one is asking what's the shape tuple and its slicing if I read the title. Reopening. Would love to vote to close it as being unclear. – Divakar Oct 06 '17 at 18:10
  • I'm sorry about x.shape[1000]. I have not read carefully about the definition of shape. I have read and understood this issue but I still have a problem with my code, can you help me check it? – Le Vu Minh Huy Oct 06 '17 at 18:12
  • @Divakar: Thanks. IMO It's at least off-topic, since it doesn't include the context and code necessary to produce the exception... – Mark Dickinson Oct 06 '17 at 18:13
  • Sorry this is the first time I posted here so I still do not clear about the title rules, hope mod close this topic because the question is not clear – Le Vu Minh Huy Oct 06 '17 at 18:16
  • I was able to reproduce your problem and I think the answer provided should help you. Note, your x array for which you are calling x.shape is probably only 1 dimension so x.shape is returning a tuple with only a single element (x,) where x is the length of the x array which is why x.shape[1] results in an error. However note that for example (2,) =/= 2 in python. – Will.Evo Oct 06 '17 at 18:35

1 Answers1

3

From your error message:

"TypeError: arange: scalar arguments expected instead of a tuple."

It sounds to me like you are trying to use the shape of an existing array to define the shape of a new array using np.arange.

Your problem is that you don't understand what x.shape is giving you.

For example:

x = np.array([[1,2,3],[4,5,6]])
x.shape

produces (2,3), a tuple. If I try to use just x.shape to define a argument in np.arange like this:

np.arange(x.shape)

I get the following error:

"arange: scalar arguments expected instead of a tuple."

Reason being np.arange accepts either a scalar (which creates an array starting at 0 and increasing by 1 to the length provided) or 3 scalars which define where to start and end the array and the step size. You are giving it a tuple instead which it doesn't like.

So when you do:

np.arange(x.shape[0])

you are giving the arange function the first scalar in the tuple provided by x.shape and in my example producing an array like this [0,1] because the first index in the tuple is 2.

If I alternatively did

np.arange(x.shape[1])

I would get an array like [0,1,2] because the second index in the tuple is a 3.

If I did any of the following,

np.arange(x.shape[2])
np.arange(x.shape[1000])
np.arange(x.shape[300])

I would get an error because the tuple created by x.shape has only two dimensions and so can't be indexed any further than 0 or 1.

Hope that helps!

Will.Evo
  • 1,112
  • 13
  • 31