Numpy: Get shape and size of an array

20 August 2020

Code

# Import library
import numpy as np

# Create an array
x_1D = np.array([1,2,3,4])
x_2D = np.array([[1,2],
                 [3,4]])
x_3D = np.array([[[1,2],
                 [3,4]],               
                [[10,11],
                 [12,13]]])

# Shape
x_1D_shape = x_1D.shape
x_2D_shape = x_2D.shape
x_3D_shape = x_3D.shape

# Size
x_1D_size = x_1D.size
x_2D_size = x_2D.size
x_3D_size = x_3D.size

# Output
print('1-D array:', x_1D)
print('size:', x_1D_size, '; shape:', x_1D_shape, '\n')

print('2-D array:\n', x_2D)
print('size:', x_2D_size, '; shape:', x_2D_shape, '\n')

print('3-D array:\n', x_3D)
print('size:', x_3D_size, '; shape:', x_3D_shape, '\n')
1-D array: [1 2 3 4]
size: 4 ; shape: (4,) 

2-D array:
 [[1 2]
 [3 4]]
size: 4 ; shape: (2, 2) 

3-D array:
 [[[ 1  2]
  [ 3  4]]

 [[10 11]
  [12 13]]]
size: 8 ; shape: (2, 2, 2) 






Any errors in code above?
Please send a message.