Understanding ndarray.shape and ndarray.size attributes in NumPy (6 examples)

Updated: February 28, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python, offering a powerful N-dimensional array object and tools for integrating C/C++ and Fortran code. This tutorial focuses on two essential attributes of NumPy arrays: ndarray.shape and ndarray.size. Mastering these attributes can significantly enhance your data manipulation and analysis capabilities.

Let’s embark on this journey with a primer on NumPy arrays and progress through six exemplary examples to deeply understand ndarray.shape and ndarray.size.

Example 1: Creating a Basic Array

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print('Array:', a)
print('Shape:', a.shape)
print('Size:', a.size)

The output is:

Array: [1 2 3 4 5]
Shape: (5,)
Size: 5

This basic example illustrates how to create a simple array using NumPy and how to check its shape and size. The shape attribute returns a tuple indicating the number of elements along each dimension of the array, and the size attribute indicates the total number of elements in the array.

Example 2: Multi-dimensional Arrays

import numpy as np

b = np.array([[1, 2], [3, 4], [5, 6]])
print('Array:', b)
print('Shape:', b.shape)
print('Size:', b.size)

The output is:

Array: [[1 2]
 [3 4]
 [5 6]]
Shape: (3, 2)
Size: 6

This example shows a 2-dimensional array and illustrates how the shape changes to show the number of rows and columns. Meanwhile, size remains a simple count of all elements in the array.

Example 3: Reshaping an Array

import numpy as np

# Reshape a 1D array to a 2D array
a = np.array([1, 2, 3, 4, 6, 8])
new_shape = a.reshape((2, 3))
print('Reshaped Array:', new_shape)
print('Shape:', new_shape.shape)
print('Size:', new_shape.size)

The output is:

Reshaped Array: [[1 2 3]
 [4 6 8]]
Shape: (2, 3)
Size: 6

This unfolds the capability of NumPy’s reshape function, enabling the transformation of array dimensions without altering the size.

Example 4: Broadcasting in Arrays

import numpy as np

# Initializing two arrays for broadcasting
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Broadcasting and adding arrays
result = np.add(a, b)
print('Broadcasted result:', result)
print('Shape:', result.shape)
print('Size:', result.size)

This demonstrates how NumPy handles array operations involving arrays of different shapes through broadcasting, reflecting changes in the resultant array’s shape and size attributes.

Example 5: Advanced Multi-dimensional Slicing

import numpy as np

# Creating a 3D array
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Slicing the array
slice_arr = arr[:, 1, :]
print('Sliced Array:', slice_arr)
print('Shape:', slice_arr.shape)
print('Size:', slice_arr.size)

This example ventures into multi-dimensional slicing, showing how slicing affects the shape and size of the resultant array, offering robust tools for data manipulation.

Example 6: Dynamic Reshaping with -1

import numpy as np

# Using -1 to dynamically reshape an array
arr = np.arange(10)
reshaped_arr = arr.reshape((2, -1))
print('Dynamic Reshape:', reshaped_arr)
print('Shape:', reshaped_arr.shape)
print('Size:', reshaped_arr.size)

The usage of -1 in the reshape function illustrates how NumPy can automatically calculate the dimension size, showcasing another level of ease and flexibility in manipulating array structures.

Conclusion

This tour through the ndarray.shape and ndarray.size attributes in NumPy has illustrated how understanding and effectively utilizing these aspects can lead to more efficient data handling and analysis within Python. By mastering these foundational components, you can unlock a new level of capability in your scientific computing tasks.