ndarray.shape

Full name
ndarray.shape
Library
NumPy
Syntax

ndarray.shape

Description

The numpy.ndarray.shape attribute returns a tuple with the size of the array, but can also be used to resize it. As with the numpy.reshape function, one of the dimensions sizes can be -1, in which case the size of that dimension is inferred from the number of elements in the array and the size of the other dimensions.

Examples

In this example we have a single dimension numpy array, created from a list with three elements:

import numpy as np

a = np.array([1, 2, 3])
print(a)
print(a.shape)

Numpy.ndarray.shape function. Example of use

The comma -without a number after it- returned by the attribute indicates that this second dimension is not defined (the comma serves to specify that it is a tuple -even if it only has one element-, not an integer).

In this second example the numpy array is formed from a list with only one element: a list of three elements. This is interpreted as one row (since there was only one item in the original list) and three columns:

import numpy as np

a = np.array([[1, 2, 3]])
print(a)
print(a.shape)

Numpy.ndarray.shape function. Example of use

The size of the numpy array is (1, 3): one row and three columns.

If the list from which the numpy array is created contains not one but three lists, each with a single element, it would be interpreted as three rows and a single column:

import numpy as np

a = np.array([[1], [2], [3]])
print(a)
print(a.shape)

Numpy.ndarray.shape function. Example of use

In this case the size of the numpy array is (3, 1): three rows and one column.

If the list from which the numpy array is created contains two sublists, each with three elements, it is interpreted as two rows and three columns:

import numpy as np

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

Numpy.ndarray.shape function. Example of use

Now, the size of the array is (2, 3): two rows (since the original list had two sub-rows) and three columns (each sub-row had three elements).

We can create a three-dimensional numpy array if, in the previous scenario, we replace each of the elements of the lower level with a new list:

import numpy as np

a = np.array([
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
print(a)

Numpy.ndarray.shape function. Example of use

In this case, the numpy array is formed from a list with two elements (two sublists), which is interpreted as two elements in the first dimension (rows), each of them formed by three other sublists, which is interpreted as three elements in the second dimension (columns), each one made up of three elements, which is interpreted as three elements in the third dimension (depth).

We can use the attribute to resize a numpy array. For example, in this case we start with a one-dimensional array and six elements, and convert it into a two-dimensional array of size (3, 2):

import numpy as np

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

a.shape = (3, 2)
print(a)
print(a.shape)

Numpy.ndarray.shape function. Example of use

 

If, when resizing the array, we use the value -1 to specify the size of a dimension, this size is inferred from the number of elements and the size of the other dimensions. If, as in this case, we have six elements and the size of the second dimension is set to three, the first dimension must have a size of 2:

import numpy as np

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

a.shape = (-1, 3)
print(a)
print(a.shape)

Numpy.ndarray.shape function. Example of use

 

In this example, we start from a two-dimensional numpy array of size (2, 3) and convert it to a single-dimensional numpy array. To do this, we set the new size to (-1,) -we could also have used as a new size (6,), or (-1) or (6), without specifying that it is a tuple-:

import numpy as np

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

a.shape = (-1,)
print(a)
print(a.shape)

Numpy.ndarray.shape function. Example of use

 

Submitted by admin on Mon, 01/21/2019 - 10:34