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 the array_like object 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_like
thisarray = np.arange(4, dtype = int)
# implementing the numpy.ones_like() function
myarray = np.full_like(thisarray, 10)
# printing the two arrays
print(thisarray)
print(myarray)

Code explanation

  • 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.