NumPy - Array Size

NumPy Array Size

The size of a NumPy array refers to the total number of elements contained within the array. Understanding the size of an array is important for performing various operations, such as reshaping, broadcasting, and iterating through elements.

In this tutorial, we will discuss how to determine and manipulate the size of NumPy arrays.

Checking Array Size

NumPy provides the size attribute to check the number of elements in an array. This attribute returns an integer representing the total number of elements, regardless of its shape. Whether the array is one-dimensional or multi-dimensional, size will always provide the count of all elements present.

Knowing the size of an array can be useful in various scenarios, such as iterating over elements, reshaping arrays, and optimizing memory usage.

Example: Iterating Over Elements

When you need to iterate through each element in an array, knowing the size helps you define the range for your loop −

import numpy as np

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

# Iterating using the size attribute
for i in range(array.size):
   print(array[i])

Following is the output obtained −

1
2
3
4
5

Example: Memory Calculation

You can calculate the memory consumption of an array by multiplying its size by the size of each element using the “itemsize” attribute in NumPy −

import numpy as np

array = np.ones((1000, 1000), dtype=np.float64)

memory_usage = array.size * array.itemsize
print("Memory usage in bytes:", memory_usage)

This will produce the following result −

Memory usage in bytes: 8000000

NumPy Array Size vs. Array Shape

While the size attribute gives the total number of elements in an array, the shape attribute provides the dimensions of the array. The size of an array can be calculated from the shape by multiplying all the dimensions.

Example

In this example, we are retrieving the shape and size of an array using the NumPy “shape” and “size” attribute respectively −

import numpy as np

# Creating a 3D array
array = np.zeros((2, 3, 4))

# Checking the shape and size
print("Array shape:", array.shape)
print("Array size:", array.size)

The shape of the array is (2, 3, 4), and the size is 2 * 3 * 4 = 24 −

Array shape: (2, 3, 4)
Array size: 24

Resizing Arrays

The resize() function in NumPy is used to resize an array. This function changes the shape and size of the array in-place, and the new shape must be compatible with the total number of elements.

If the new shape is larger than the original, the new array is filled with repeated copies of the original array. If the new shape is smaller, the original array is truncated.

Unlike the reshape() function, which only changes the shape of an array without changing its data, the resize() function can change the size of the array as well, adding new elements or removing existing ones.

Example: Resizing to a Larger Array

In this example, the original array is resized to a “2x5” array. The new elements are filled by repeating the original array −

import numpy as np

# Creating a 1D array
array_1d = np.array([1, 2, 3])

# Resizing the array to a larger size
resized_array = np.resize(array_1d, (2, 5))

print("Original array:\n", array_1d)
print("Resized array:\n", resized_array)

After executing the above code, we get the following output −

Original array:
 [1 2 3]
Resized array:
 [[1 2 3 1 2]
  [3 1 2 3 1]]

Example: Resizing to a Smaller Array

Here, the original “2x3” array is resized to a “2x2” array. The excess elements are truncated −

import numpy as np

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

# Resizing the array to a smaller size
resized_array = np.resize(array_2d, (2, 2))

print("Original array:\n", array_2d)
print("Resized array:\n", resized_array)

The result produced is as follows −

Original array:
 [[1 2 3]
  [4 5 6]]
Resized array:
 [[1 2]
  [3 4]]

Example: In-Place Resizing

In this example, the original array is resized in place to a “2x5” array. The new elements are filled with zeros −

import numpy as np

# Creating a 1D array
array_1d = np.array([1, 2, 3])

# In-place resizing the array to a larger size
array_1d.resize((2, 5))

print("Resized array:\n", array_1d)

We get the output as shown below −

Resized array:
 [[1 2 3 0 0]
  [0 0 0 0 0]]
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team