How to Check the Size of a Numpy Array in Python?

Estimated read time 1 min read

To check the size of a NumPy array in Python, you can use the size attribute or the shape attribute of the array. Here’s how you can do it:

import numpy as np

# Create a NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Method 1: Using the size attribute
array_size = arr.size
print("Size of the array:", array_size)

# Method 2: Using the shape attribute
array_shape = arr.shape
print("Shape of the array:", array_shape)

Output:

Size of the array: 6
Shape of the array: (2, 3)

In the above example, size returns the total number of elements in the array, while shape returns a tuple specifying the dimensions of the array. In this case, the array has 2 rows and 3 columns, so the shape is (2, 3).

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply