note.nkmk.me

NumPy: Get the number of dimensions, shape, and size of ndarray

Posted: 2019-11-06 / Tags: Python, NumPy

To get the number of dimensions, shape (size of each dimension) and size (number of all elements) of NumPy array, use attributes ndim, shape, and size of numpy.ndarray. The built-in function len() returns the size of the first dimension.

  • Number of dimensions of numpy.ndarray: ndim
  • Shape of numpy.ndarray: shape
  • Size of numpy.ndarray (total number of elements): size
  • Size of the first dimension of numpy.ndarray: len()

Take the following numpy.ndarray from 1 to 3 dimensions as an example.

import numpy as np

a_1d = np.arange(3)
print(a_1d)
# [0 1 2]

a_2d = np.arange(12).reshape((3, 4))
print(a_2d)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

a_3d = np.arange(24).reshape((2, 3, 4))
print(a_3d)
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]
# 
#  [[12 13 14 15]
#   [16 17 18 19]
#   [20 21 22 23]]]
Sponsored Link

Number of dimensions of numpy.ndarray: ndim

The number of dimensions of numpy.ndarray can be obtained as an integer value int with attribute ndim.

print(a_1d.ndim)
# 1

print(type(a_1d.ndim))
# <class 'int'>

print(a_2d.ndim)
# 2

print(a_3d.ndim)
# 3

If you want to add a new dimension, use numpy.newaxis or numpy.expand_dims(). See the following post for details.

Shape of numpy.ndarray: shape

The shape (= size of each dimension) of numpy.ndarray can be obtained as a tuple with attribute shape.

Even in the case of a one-dimensional array, it is a tuple with one element instead of an integer value. Note that a tuple with one element has a trailing comma.

print(a_1d.shape)
# (3,)

print(type(a_1d.shape))
# <class 'tuple'>

print(a_2d.shape)
# (3, 4)

print(a_3d.shape)
# (2, 3, 4)

For example, in the case of a two-dimensional array, it will be (number of rows, number of columns). If you only want to get either the number of rows or the number of columns, you can get each element of the tuple.

print(a_2d.shape[0])
# 3

print(a_2d.shape[1])
# 4

It is also possible to assign to different variables.

row, col = a_2d.shape
print(row)
# 3

print(col)
# 4

Use reshape() to convert the shape. See the followin post for details.

Sponsored Link

Size of numpy.ndarray (total number of elements): size

The size (= total number of elements) of numpy.ndarray can be obtained with the attributesize.

print(a_1d.size)
# 3

print(type(a_1d.size))
# <class 'int'>

print(a_2d.size)
# 12

print(a_3d.size)
# 24

Size of the first dimension of numpy.ndarray: len()

len() is the built-in function that returns the number of elements in a list or the number of characters in a string.

For numpy.ndarray, len() returns the size of the first dimension. Equivalent to shape[0] and also equal to size only for one-dimensional arrays.

print(len(a_1d))
# 3

print(a_1d.shape[0])
# 3

print(a_1d.size)
# 3

print(len(a_2d))
# 3

print(a_2d.shape[0])
# 3

print(len(a_3d))
# 2

print(a_3d.shape[0])
# 2
Sponsored Link
Share

Related Categories

Related Posts