What is the numpy.full() function in Python?

Overview

The numpy.full() function is used to return a new array of a given shape and data type filled with fill_value.

Syntax

numpy.full(shape, fill_value, dtype=None, like=None)

Parameters

The numpy.full() function takes the following parameter values:

  • shape: This represents the shape of the desired array.
  • fill_value: This represents the fill value.
  • dtype: This represents the data type of the desired array. This is an optional parameter.
  • like: This represents the prototype or the array_like object.

Return value

The numpy.full() function returns an array of fill_value shape, data type, and order.

Example

import numpy as np
# implementing the numpy.full() function
myarray = np.full((2, 2), 5)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We implement the numpy.full() function on an array that has 2 arrays and 2 elements to change all the elements to 5. The output is assigned to a variable myarray().
  • Line 6: We print the variable myarray.

Free Resources