Using ndarray.resize() method in NumPy (5 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

Introduction

Numerical Python, more commonly known as NumPy, is a foundational package for numerical computations in Python. Among its core features is the ability to efficiently handle arrays and matrices, which are crucial for data science, machine learning, and scientific computing. In this tutorial, we will explore the ndarray.resize() method in NumPy, providing a thorough understanding through five practical examples, starting from the basics to more advanced applications.

Understanding ndarray.resize()

The ndarray.resize() method in NumPy is a powerful tool that allows you to change the shape of an array in place. Unlike reshaping, which returns a new array with a different shape, resizing modifies the original array, which can be very useful when working with large datasets or when memory efficiency is a concern. However, it’s important to note that resizing may alter the array’s data, either by repeating it to fit the new shape or by removing excess elements.

Syntax:

ndarray.resize(new_shape, refcheck=True)

Parameters:

  • new_shape: tuple of ints, or n ints. The new shape should be compatible with the original shape. If an integer, the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
  • refcheck: bool, optional. If False, the reference count check is skipped. This check ensures that the array is not referenced elsewhere. This parameter is useful mainly for arrays that are meant to be modified in-place but are referenced elsewhere. The default is True.

Note:

  • This method does not return a new array; it modifies the existing array in-place.
  • Use with caution: resizing an array with refcheck=False can lead to the array being shared between two different objects and can lead to unexpected results if the array is modified.

Example 1: Basic Resizing

import numpy as np

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

# Resizing the array to a 2x3 matrix
arr.resize((2, 3))

print(arr)

Output:

[[1 2 3]
 [4 5 6]]

This example demonstrates how to resize an array into a different shape. The original one-dimensional array is transformed into a two-dimensional 2×3 matrix.

Example 2: Increasing Size with Repetition

import numpy as np

# Doubling the size of an existing array
original = np.array([1, 2, 3])
original.resize((2, 3))

print(original)

Output:

[[1 2 3]
 [1 2 3]]

In this example, resizing increases the size of the original array, with the additional elements being filled by repeating the original array. This showcases how ndarray.resize() handles size increments.

Example 3: Reducing Size

import numpy as np

# Reducing the size of an existing array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
arr.resize((2, 2))

print(arr)

Output:

[[1 2]
 [3 4]]

When reducing the size of an array, extra elements are removed, and only the first elements that fit the new shape are retained. This example effectively demonstrates how data can be lost when resizing downward.

Example 4: Complex Resizing and Filling

import numpy as np

# Complex resizing with a custom fill
arr = np.array([1, 2, 3, 4])
arr.resize((3, 3), refcheck=False)
arr[1:,1:] = 5

print(arr)

Output:

[[1 2 3]
 [0 5 5]
 [0 5 5]]

This example delves into more complex behavior with resizing, such as changing the shape to a non-direct correspondent of the original size and manually filling in the new elements with a custom value. The refcheck=False parameter allows us to resize even if it contains references to other Python objects, which can be particularly useful in advanced applications.

Example 5: Combining Resizing with Operations

import numpy as np

# Illustrative example combining resizing and operations
arr = np.array([1, 2, 3, 4])
arr.resize((2, 2))
arr = arr * 2

print(arr)

Output:

[[2 4]
 [6 8]]

This final example demonstrates combining the resizing function with an array operation. After resizing, we proceed to multiply every element of the modified array by 2. This showcases how resize() can be seamlessly integrated into data processing pipelines.

Conclusion

Throughout these examples, we’ve seen the versatility and power of the ndarray.resize() method in NumPy. From basic resizing to more sophisticated applications involving element filling and operation integration, resize() offers a fundamental mechanism for array manipulation. Understanding and utilizing this function can significantly enhance your ability to manage and transform data within your Python projects.