1

What does this mean in numpy coding, (4,)? You have an array and you run the shape of it and it gives you this answer. What does it mean?

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

(4,)
Brown Bear
  • 16,802
  • 8
  • 40
  • 61
german
  • 45
  • 1
  • 1
  • 7
  • check the doc : https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html – Mohamed Ali JAMAOUI Sep 09 '17 at 22:13
  • And another recent question asking by the extra comma: https://stackoverflow.com/questions/46134891/why-an-extra-comma-in-the-shape-of-a-single-index-numpy-array – hpaulj Sep 10 '17 at 01:01

1 Answers1

1

Numpy's .shape property is a tuple that contains the size of the numpy object in every dimension.

Since your object is one-dimensional (a regular array), the length of the tuple is 1. Since your one-dimensional object contains 4 objects, it's size in the first dimension is 4, so the first element in the tuple is 4.

If the notation is confusing, tuple([4]) == (4,). The trailing comma is necessary because (4) would simply be regular parenthesis around an expression.

If your numpy object was a two-dimensional array of size 3x4, the .shape would give (3, 4).

orlp
  • 98,226
  • 29
  • 187
  • 285