0
0
NumPydata~5 mins

Array attributes (shape, dtype, ndim, size) in NumPy

Choose your learning style9 modes available
Introduction

Array attributes help us understand the structure and type of data inside an array. They tell us the shape, data type, number of dimensions, and total size of the array.

When you want to check how many rows and columns your data has.
When you need to know what type of data (like integers or floats) is stored in the array.
When you want to find out how many dimensions your data has, like 1D, 2D, or 3D.
When you want to know the total number of elements in the array.
Syntax
NumPy
import numpy as np

array = np.array([...])

# Get the shape (rows, columns, ...)
array.shape

# Get the data type of elements
array.dtype

# Get the number of dimensions
array.ndim

# Get the total number of elements
array.size

shape returns a tuple showing the size in each dimension.

dtype tells the type of data stored, like int32 or float64.

Examples
Empty 1D array has shape (0,) and zero size.
NumPy
import numpy as np

empty_array = np.array([])
print(empty_array.shape)  # (0,) means empty 1D array
print(empty_array.size)   # 0 elements
Array with one element shows shape (1,) and 1 dimension.
NumPy
import numpy as np

one_element_array = np.array([42])
print(one_element_array.shape)  # (1,) means 1 element in 1D
print(one_element_array.ndim)   # 1 dimension
print(one_element_array.size)   # 1 element
2D array with 2 rows and 3 columns.
NumPy
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.shape)  # (2, 3) means 2 rows and 3 columns
print(matrix.ndim)   # 2 dimensions
print(matrix.size)   # 6 elements total
Shows the data type of elements in the array.
NumPy
import numpy as np

float_array = np.array([1.5, 2.5, 3.5])
print(float_array.dtype)  # float64 means floating point numbers
Sample Program

This program creates a 2D array and prints its shape, data type, number of dimensions, and total size.

NumPy
import numpy as np

# Create a 2D array (matrix)
number_matrix = np.array([[10, 20, 30], [40, 50, 60]])

print("Array:")
print(number_matrix)

# Print array attributes
print("Shape of array:", number_matrix.shape)
print("Data type of elements:", number_matrix.dtype)
print("Number of dimensions:", number_matrix.ndim)
print("Total number of elements:", number_matrix.size)
OutputSuccess
Important Notes

Time complexity: Accessing these attributes is very fast, O(1), because they are stored properties.

Space complexity: No extra space is used when accessing these attributes.

Common mistake: Confusing shape with size. Shape shows dimensions, size shows total elements.

Use shape to understand layout, size to know total data points, dtype to check data type, and ndim to know how many dimensions your data has.

Summary

Array attributes help you understand your data's structure and type.

shape shows the size in each dimension.

dtype tells the type of data stored.

ndim gives the number of dimensions.

size gives the total number of elements.