NumPy – Working with ndarray.put() method (6 examples)

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

Introduction

In the world of Python data science and numerical computing, NumPy reigns as a foundational package. It provides an efficient interface for working with large, multi-dimensional arrays and matrices. Among its comprehensive set of operations, the put() method stands out for its utility in modifying array elements in-place. This tutorial embarks on a detailed exploration of the ndarray.put() method with six illustrative examples, ranging from basic implementations to more complex applications.

Syntax & Parameters of ndarray.put()

Before diving into examples, let’s define what ndarray.put() is. This method replaces specified elements of an array with given values. The syntax is as follows:

array.put(indices, values, mode='raise')

Where:

  • indices: This is an array of integer indices, specifying the positions where values should be inserted.
  • values: The new values to insert into the array at the specified indices.
  • mode: This optional argument can be ‘raise’, ‘wrap’, or ‘clip’. It determines what to do if indices are outside the bounds of the array.

Example 1: Simple Replacement

Let’s start with a basic example, replacing elements in a one-dimensional array.

import numpy as np

arr = np.array([1, 2, 3, 4])
arr.put([0, 3], [5, 6])
print(arr)

Output:

[5 2 3 6]

This illustrates how you can target elements at indices 0 and 3 for replacement.

Example 2: Handling Out-of-Bounds with ‘wrap’ Mode

Next, let’s handle cases where specified indices are beyond the array’s size using the ‘wrap’ mode.

import numpy as np

arr = np.array([1, 2, 3, 4])
arr.put([4, 5], [7, 8], mode='wrap')
print(arr)

Output:

[7, 8, 3, 4]

This demonstrates the ‘wrap’ mode, where indices exceeding the array dimensions are wrapped around and start from the beginning.

Example 3: Filling With ‘clip’ Mode

In the ‘clip’ mode, if the indicated indices are out of bounds, they are clipped to the array’s limits.

import numpy as np

arr = np.array([1, 2, 3, 4])
arr.put([4, 5], [9, 10], mode='clip')
print(arr)

Output:

[1, 2, 3, 9]

Here, the index 5 is clipped to the last index of the array, 3, resulting in the last element’s replacement.

Example 4: Updating Multi-Dimensional Arrays

The put() method is not limited to one-dimensional arrays. Here’s how you can update elements in a two-dimensional array.

import numpy as np

arr = np.array([[1, 2], [3, 4]])
arr.put([0, 3], [5, 6])
print(arr)

Output:

[[5 2]
 [3 6]]

Note that the indices are calculated as if the array were flattened, adhering to row-major order (C-style).

Example 5: Using put() with Boolean Indexing

Boolean indexing offers powerful capabilities for conditional modifications. Here, we’ll replace elements that satisfy a certain condition.

import numpy as np

arr = np.array([1, -2, 3, -4])
indices = np.where(arr < 0)
arr.put(indices, [10, 20])
print(arr)

Output:

[1, 10, 3, 20]

This method elegantly combines np.where() with put() to selectively modify array elements.

Example 6: Complex Data Manipulation

For our final example, let’s perform more intricate data manipulations, combining multiple NumPy operations.

import numpy as np

arr = np.arange(10)
arr.put([0, -1], [arr.max(), arr.min()])
print(arr)

Output:

[9, 1, 2, 3, 4, 5, 6, 7, 8, 0]

This showcases how you can use array statistics (like max and min) in tandem with put() for dynamic element replacement.

Conclusion

Through these examples, we’ve seen the versatility and power of the ndarray.put() method. Whether you’re making simple replacements, handling out-of-bounds indices, or engaging in complex data manipulations, put() provides a neat, efficient way to modify NumPy arrays in-place. Its integration into broader array operations empowers users to execute sophisticated data processing tasks with minimal code.