NumPy full()

The full() method creates a new array of given shape and type, filled with a given fill value.


full() Syntax

The syntax of full() is:

numpy.empty(shape, fill_value, dtype = None, order = 'C', like = None)

full() Arguments

The full() method takes the following arguments:

  • shape - desired new shape of the array (can be integer or sequence of integers)
  • fill_value - value to fill the array with
  • dtype (optional) - datatype of the array
  • order (optional) - specifies the order in which the uninitialized values are filled
  • like (optional)- reference object to allow the creation of arrays that are not NumPy arrays

full() Return Value

The full() method returns the array of given shape, order, and datatype filled with a fill value.


Example 1: Create Array With full()

Output

1D Array:  [2 2 2 2 2]
2D Array: 
[[2. 2.]
 [2. 2.]
 [2. 2.]]
n-d array:
[[[1 2]
  [1 2]]

 [[1 2]
  [1 2]]]

If unspecified, the default dtype is the data type of fill value.


Example 2: Change the Data Type of Array Elements

You can change the data type of array elements by using the dtype argument.

import numpy as np

# create a 1D array of five 2s

# pass the dtype argument to change the data type

array1 = np.full(5, 2, dtype = 'float')

print('1D Array: ',array1)

Output

1D Array:  [2. 2. 2. 2. 2.]

Here, the fill value is 2, so the default dtype is an integer. However, we changed the data type of the filled value to floating-point by passing dtype='float'.


Using Optional Order Argument in fullempty()

The order argument specifies the order in which the uninitialized values are filled.

The order can be:

  • 'C' - elements are stored row-wise (default)
  • 'F' - elements are stored column-wise