NumPy shape: The dimension of array (or matrix)

shape is a method of NumPy array and returns the shape of it.

import numpy

a = numpy.array([[1, 2, 3], [4, -7, 10]])

s = a.shape

print(s)  # (2, 3)
print(type(s))  # <class 'tuple'>

[[1, 2, 3], [4, -7, 10]] has 2 rows and 3 columns so shape is (2, 3).

import numpy

a1 = numpy.array([1, 2])
a2 = numpy.array([[1, 2, 3, 4]])
a3 = numpy.array([])
a4 = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a5 = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])

print(a1.shape)  # (2,)
print(a2.shape)  # (1, 4)
print(a3.shape)  # (0,)
print(a4.shape)  # (3, 3)
print(a5.shape)  # (3, 2, 2)

a5 is a tensor.

Comments

Powered by Markdown

More