How to intepret the shape of the array in Python?

Question:

I am using a package and it is returning me an array. When I print the shape it is (38845,). Just wondering why this ‘,’.

I am wondering how to interpret this.

Thanks.

Asked By: Shan

||

Answers:

It sounds like you’re using Numpy. If so, the shape (38845,) means you have a 1-dimensional array, of size 38845.

Answered By: bradley.ayers

Python has tuples, which are like lists but of fixed size. A two-element tuple is (a, b); a three-element one is (a, b, c). However, (a) is just a in parentheses. To represent a one-element tuple, Python uses a slightly odd syntax of (a,). So there is only one dimension, and you have a bunch of elements in that one dimension.

Answered By: Amadan

It seems you’re talking of a Numpy array.

shape returns a tuple with the same size as the number of dimensions of the array. Each value of the tuple is the size of the array along the corresponding dimensions, or, as the tutorial says:

An array has a shape given by the number of elements along each axis.

Here you have a 1D-array (as indicated with a 1-element tuple notation, with the coma (as @Amadan) said), and the size of the 1st (and only dimension) is 38845.
For example (3,4) would be a 2D-array of size 3 for the 1st dimension and 4 for the second.

You can check the documentation for shape here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html

Answered By: Bruno

Just wondering why this ‘,’.

Because (38845) is the same thing as 38845, but a tuple is expected here, not an int (since in general, your array could have multiple dimensions). (38845,) is a 1-tuple.

Answered By: Karl Knechtel
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.