Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why an extra comma in the shape of a single index numpy array

Tags:

arrays

numpy

People also ask

Why does my NumPy array have commas?

A parenthesized number followed by a comma denotes a tuple with one element. The trailing comma distinguishes a one-element tuple from a parenthesized n . In a dimension entry, instructs NumPy to choose the length that will keep the total number of array elements the same.

What is the significance of shape function in NumPy library?

Python numpy shape function The numpy module provides a shape function to represent the shape and size of an array. The shape of an array is the no. of elements in each dimension. In NumPy, we will use a function called shape that returns a tuple, the elements of the tuple give the lengths of the array dimensions.

What does Array_like mean in Python?

Any sequence that can be interpreted as an ndarray. This includes nested lists, tuples, scalars and existing arrays. so even scalars can be taken into account, just like np. array(1024) .

Does a NumPy array have an index?

You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.


The reason we don't use (12) for a one-element tuple (like [12] for one-element list) is that round parentheses also appear in formulas. E.g., in x = 2*(5+7) the part (5+7) is just a number, not a tuple. But what if we actually meant it to be a one-element tuple? The trailing comma is a way to indicate that. Compare:

>>> 2*(5+7)
24
>>> 2*(5+7,)
(12, 12)

With lists, the trailing comma is not needed although some style guides recommend it for consistency.

>>> 2*[5+7]
[12, 12]
>>> 2*[5+7,]
[12, 12]

A numpy array's shape property always returns a tuple.

The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specify the sizes of each dimension.

(12,) is just a one-element tuple, so this indicates that you have a one-dimensional array (because the tuple has length 1) with a size of 12.

Documented here.