Numpy recarray.put () function | python

NumPy | Python Methods and Functions

numpy.recarray.put() Replaces specific elements of an array of records with specified values. Indexing works on a smoothed target array.

Syntax: numpy.recarray.put (indices, values, mode = `raise`)

Parameters:
indices: [array_like] Target indices, interpreted as integers.
values: [array_like] Values ​​to place in a at target indices. If values ​​is shorter than ind it will be repeated as necessary.
mode: [`raise`, `wrap`, `clip`, optional] Specifies how out-of-bounds indices will behave.

Return: [ndarray] Resultant array.

Code # 1:

# Python program explaining
# numpy.recarray.put () method

 
# import numy as a geek

import numpy as geek

 
# create an input array with two different fields

in_arr = geek.array ([( 1.0 , 2 ), ( 3.0 , - 4 ), ( 5.0 , 6 ),

( 7.0 , 8 ), ( 9.0 , - 4 ), ( 11.0 , - 2 )],

dtype = [( `a` , float ), ( `b` , int )])

 

print ( "Input array:" , in_arr)

 
# convert it to an array of posts,
# using arr.view (np.recarray)

rec_arr = in_arr.view (geek.recarray)

print ( "Record array of float:" , rec_arr.a)

print ( "Record array of int: " , rec_arr.b)

  
# using recarray.put methods
# place array of records in default mode

rec_arr.a.put ([ 0 , 2 ], [ - 14 , 15 ])

print ( " Output float array in default mode: " , rec_arr.a) 

 
# applying recarray.put methods
# float array of records in clip mode

rec_arr.a.put ( 13 , - 4 , mode = `clip` )

print ( "Output float array in clip mode: " , rec_arr.a) 

  
# applying recarray.put methods
# into array of int records

rec_arr.b.put ([ 1 , 2 , 4 ], [ 10 , 15 , 20 ])

print ( "Output int array in default mode:" , rec_arr.b) 

  
# applying recarray.put methods
# to an array of int records in clip mode

rec_arr.b.put ( 8 , 100 , mode = `clip` )

print ( "Output int array in clip mode:" , rec_arr.b) 

Exit:

 Input array: [(1., 2) (3., -4) (5., 6) (7., 8) (9., -4) (11. , -2)] Record array of float: [1. 3. 5. 7. 9. 11.] Record array of int: [2 -4 6 8 -4 -2] Output float array in default mode: [-14. 3. 15. 7. 9. 11.] Output float array in clip mode: [-14. 3. 15. 7. 9. -4.] Output int array in default mode: [2 10 15 8 20 -2] Output int array in clip mode: [2 10 15 8 20 100] 




Get Solution for free from DataCamp guru