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.
numpy.full_like(a, fill_value)
The numpy.full_like()
function takes the following parameter values:
a
: This represents the array_like
object or prototype of the array that needs to be created.fill_value
: This represents the fill 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.
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)
Line 1: We import the numpy
module.
Line 4: We create an array prototype, using the numpy.arange()
method. We assign the output to a variable called thisarray
.
Line 7: We implement the numpy.full_like()
function on thisarray
by changing the elements of thisarray
to the fill value of 10
. We assign the output to another variable called myarray
Lines 11–12: We print both the prototypes thisarray
and the modified array myarray
.