NumPy full()

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

Example

import numpy as np

# create an 2x2 array filled with 3s array1 = np.full((2, 2), 3)
print(array1) ''' Output: [[3 3] [3 3]] '''

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()

import numpy as np

# create a 1D array of five 2s array1 = np.full(5,2)
print('1D Array: ',array1)
# create a 2D array of 2.0s array2 = np.full((3,2), 2.0)
print('2D Array: \n',array2)
# create an nd-array of (1,2) array3 = np.full((2, 2, 2), (1, 2))
print('n-d array:\n',array3)

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