Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numpy put() Function



The Numpy put() function is used to replace specific elements in an array at specified indices with new values. This function directly modifies the input array in-place and provides a convenient way to assign values to arbitrary positions in a flattened or multi-dimensional array.

The put() function operates on the flattened version of the target array, meaning that the specified indices are applied as if the array were a one-dimensional array. This is roughly equivalent to the operation:

a.flat[indices] = values

Syntax

Following is the syntax of the Numpy put() function −

numpy.put(a, indices, values, mode='raise')

Parameters

Following are the parameters of the Numpy put() function −

  • a: The input array to be modified.
  • indices: An array-like sequence of integers specifying the flattened positions where the values will be placed.
  • values: An array-like sequence of values to place into the array at the specified positions. If the length of values is shorter than indices, it is repeated.
  • mode (optional): A string that controls how out-of-bounds indices are handled. Options are −
  • 'raise': Raises an error (default).
  • 'wrap': Wraps around to valid indices.
  • 'clip': Clips to the range of valid indices.

Return Type

This function does not return a new array. Instead, it modifies the input array in-place.

Example

Following is a basic example to replace elements in a 1D array using the Numpy put() function −

import numpy as np  
array = np.array([10, 20, 30, 40, 50])  
np.put(array, [1, 3], [99, 77])  
print("Modified Array:", array)  

Output

Following is the output of the above code −

Modified Array: [10 99 30 77 50]  

Example: Using Repeated Values

If the number of elements in the values array is fewer than the elements in the indices array, the put() function repeats the values cyclically until they match the length of the indices array.

In the following example, a single element is provided in the values array, while three elements are specified in the indices array. The put() function replaces all the specified index positions with the given single value, repeated it as needed −

import numpy as np  
array = np.array([10, 20, 30, 40, 50])  
np.put(array, [0, 2, 4], [99])  
print("Modified Array:", array)  

Output

Following is the output of the above code −

Modified Array: [99 20 99 40 99]  

Example: Using the Mode Parameter

The mode parameter in the put() function, when set to clip, ensures that if an index is out of bounds, it is clipped to the nearest valid index. In the following example, indices 5 and 7 are out of bounds for the array. Since the mode is set to clip, these indices are clipped to the last valid index, resulted in the last element −

import numpy as np  
array = np.array([10, 20, 30, 40, 50])  
np.put(array, [1, 5, 7], [99, 77, 55], mode='clip')  
print("Modified Array:", array)  

Output

Following is the output of the above code −

Modified Array: [10 99 30 40 77]  
numpy_array_manipulation.htm
Advertisements