1

Why the shape of any 1d array has an extra comma? Why (5,) instead of (5).
And why an extra comma is omitted for >= 2 arrays? Why it is (3,2) instead of (3,2,).

1D Example

data = array([11, 22, 33, 44, 55])
print(data.shape)

(5,) <======= EXTRA COMMA

2D Example

data = [[11, 22],
[33, 44],
[55, 66]]
data = array(data)
print(data.shape)

(3,2) <====

John Puskin
  • 131
  • 3
  • 1
    Does this answer your question? [Difference between numpy.array shape (R, 1) and (R,)](https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r) – FBruzzesi May 07 '20 at 09:56
  • No. I am not asking the difference between (R,1) and (R). I am asking the shape function internals. – John Puskin May 07 '20 at 10:08
  • 4
    shape returns _Tuple of array dimensions_. If you try to do `type((5))` and `type((5, ))` you will see that the former returns `int` and the latter returns `tuple`. In fact to have a tuple with one element you should add an extra comma. – FBruzzesi May 07 '20 at 10:10
  • 1
    `type((5))` vs. `type((5,))` vs. `type((5,1))` are all different things. However, using extra comma is a little bit confusing. But it is Ok. I got it. Maybe a better represantation will be found. – John Puskin May 07 '20 at 10:19
  • For example: `type((5))` is `int` and `type((5,))` is `tuple(5)` and `type((5,1))` may `tuple(5,1)`. – John Puskin May 07 '20 at 10:27

1 Answers1

0

(1) would be more consistent with tuples of length greater than one, but in Python syntax, this is nothing more than an expression in brackets and evaluates to the integer 1. (1,) on the other hand is a tuple.

(By the way, you can also write (1,2,) instead of (1,2).)

Nico Schlömer
  • 37,093
  • 21
  • 139
  • 189