Numpy.put() in python

Numpy.put() replaces specified elements of an array with given values.

  • The indexing works on the flattened target array.
  • We are use Numpy here because it performs a wide variety of mathematical operations on arrays. It also adds powerful data structures to python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high level mathematical functions that operate on these arrays and matrices.
  • Array indexed works on flattened array.

SYNTAX:

numpy.put(array, indices, p_array, mode='raise')

where array, indices, p_array, mode are different parameters here

  • array – target array
  • indices – index of the values to be fetched
  • p_array – values to be placed in target array
  • mode – we have different modes here [{‘raise’, ‘wrap’, ‘clip’},optional]  mentions how out-of-bound indices will behave . Here raise is used to raise an error, wrap is wrap around, clip is clip to the range.

EXAMPLES:

Let us discuss two programs of numpy.put() now

Program 1:

import numpy as np
#importing numpy library
a = np.arrange(5)
np.put(a,[0,2],[1,1])
#Using np.put() function 
print(a)
# printing new array
Output: [ 1 1 1 3 4]

Program 2:

import numpy as np
#importing numpy library
a=np.arrange(5)
np.put(a,12,-2, mode='clip')
#Using np.put() function and selecting mode as clip
print(a) 
#printing new array
Output:[0 1 2 3 -2]

 

Finally I would conclude that I have explained about Numpy.put() syntax and operations.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top