Numpy put: How to Use np put() Function in Python

Python NumPy put() is an inbuilt function that is used to replace specific array elements with given values. If we want to replace any array element with another value we can use this function.

Python NumPy put()

Numpy put(array, indices, p_array, mode = ‘raise’) function replaces specific elements of an array with given values of p_array. To replace any array element with another value, use the numpy put() method.

Syntax

numpy.put(array, indices, values, mode)

Parameters

The NumPy put() function can take up to 4 parameters.

  1. array: It is the array in which we want to work
  2. indices: Index of the values to be replaced. When the function is called, this flattens the array and works on it.
  3. values: It’s an array that contains the values which are to be inserted in the array.
  4. mode: This is an optional field. We have 3 types of mode:
    1. raise: This is the default mode which raises an error.
    2. warp: It warps around the array.
    3. clip: It clips to the range of the array.

Return Value

The put() function returns a modified array after replacing the values.

Program using default mode

See the following code.

#Importing numpy
import numpy as np

#We will create an 1D array
arr = np.array([47, 20, 41, 63, 21, 4, 74])
#Printing the array
print("The array is: ", arr)
#Shape of the array
print("Shape of the array is : ", np.shape(arr))

#Now we will replace array elements at position 2 and 3
np.put(arr, [2, 3], [100, 1000])

#Printing new array
print("New array is: ", arr)

Output

The array is:  [47 20 41 63 21  4 74]
Shape of the array is :  (7,)
New array is:  [47 20 100 1000 21 4 74]

Explanation

In this program, we have first declared one 1D array and then we have printed that array and its shape.

We have given index value [2,3] and values [100,1000] which have replaced values of array index 2 and 3 by 100 and 1000 respectively.

Program using mode=warp

See the following code.

#Importing numpy
import numpy as np

#We will create an 1D array
arr = np.array([47, 20, 41, 63, 21, 4, 74])
#Printing the array
print("The array is: ", arr)
#Shape of the array
print("Shape of the array is : ", np.shape(arr))

#Now we will replace array elements at position 2 and 3
np.put(arr, [2, 100], [90, 540], mode='warp')
#Printing new array
print("New array is: ", arr)

Output

The array is:  [47 20 41 63 21  4 74]
Shape of the array is :  (7,)
New array is:  [ 47  20 540  63  21   4  74]

Explanation

In this program, we have first declared one 1D array and then we have printed that array and its shape. We have given index value [2,100] and values [100,1000] and mode=’warp’ which has only replaced values of array index 2 by value 540. 

As 100 is out of bound, it warped one value.

Program using mode=clip

See the following code.

#Importing numpy
import numpy as np

#We will create an 1D array
arr = np.array([4, 10, 4, 13, 21, 4, 74])
#Printing the array
print("The array is: ", arr)
#Shape of the array
print("Shape of the array is : ", np.shape(arr))

#Now we will replace array elements at position 2 and 3
np.put(arr, 500, -500, mode='clip')
#Printing new array
print("New array is: ", arr)

Output

The array is:  [4 10  4 13 21 4 74]
Shape of the array is :  (7,)
New array is:  [4 10 4 13 21 4 -500]

Explanation

In this program, we have first declared one 1D array and then we have printed that array and its shape.

We have given index value [500] and values [500] and mode=’clip’ which has clipped the value and replaced it with the last value.

See also

NumPy take()

NumPy any()

NumPy all()

NumPy apply_along_axis()

NumPy apply_over_axes()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.