What is the numpy.full_like() function in Python?
Overview
In Python, the numpy.full_like() function is used to return an entire array with the same shape and type as the array that was passed to it.
Syntax
numpy.full_like(a, fill_value)
Parameter value
The numpy.full_like() function takes the following parameter values:
a: This represents thearray_likeobject or prototype of the array that needs to be created.fill_value: This represents the fill value.
Return value
The numpy.full_like() function returns an array of fill_value, with the same shape and data type as the array_like parameter that is passed to it.
Code example
import numpy as np# creating an array_likethisarray = np.arange(4, dtype = int)# implementing the numpy.ones_like() functionmyarray = np.full_like(thisarray, 10)# printing the two arraysprint(thisarray)print(myarray)
Code explanation
-
Line 1: We import the
numpymodule. -
Line 4: We create an array prototype, using the
numpy.arange()method. We assign the output to a variable calledthisarray. -
Line 7: We implement the
numpy.full_like()function onthisarrayby changing the elements ofthisarrayto the fill value of10. We assign the output to another variable calledmyarray -
Lines 11–12: We print both the prototypes
thisarrayand the modified arraymyarray.