NumPy full() Function with Examples

NumPy full() function in Python is used to return a new array of a given shape and data type filled with fill_value. In this article, I will explain syntax and how to use the numpy.full() function which returns an array of fill_value with the given shape, order, and datatype.

1. Quick Examples of full() Function

If you are in a hurry, below are some quick examples of how to use NumPy full() function in Python.


# Below are the quick examples

# Example 1: Use numpy.full() function on 1-D array
arr2 = np.full(6, 4)

# Example 2: Use numpy.full() function with two-dimensional arrays
arr2 = np.full((4, 3),8)

# Example 3: Return Array with dtype=str
arr2 = np.full((4,3),'2',dtype=str)

# Example 4: Return Array with dtype=float 
arr2 = np.full((3,4),'3',dtype=float)

# Example 5: Return Array with dtype=int
arr2 = np.full((2,4),'7',dtype=int)

2. Syntax of NumPy full()

Following is the syntax to create numpy.full() function.


# Syntax of numpy.full() 
numpy.full(shape, fill_value, dtype=None, order='C', *, like=None)

2.1 Parameters of full()

Following are the parameters of full().

  • shape –  It defines the shape of the array which is an int or sequence of ints. The shape of the new array, e.g., (4, 3) or 2.
  • fill_value – Value to fill in the array.
  • dtype – It is an optional parameter that specifies the data type of the returned array.
  • order – {‘C’, ‘F’}, optional: To store multi-dimensional data in row-major (C) or column-major (F) order/pattern in the memory location.
  • like – value should be array_like, optional

2.2 Return value of full()

It returns ndarray of fill_value with the given shape, order, and datatype.

3. Use NumPy full() Function on 1-D Array

To create a one-dimensional Numpy array of size 6, with the value 4 use NumPy full() function. Here shape=6 is used to specify the length of the array, we’re indicating that we want the output to have six elements and fill_value=4 specifies the array to be filled with value 4.


import numpy as np

# Use numpy full() function on 1-D array
arr2 = np.full(shape=6, fill_value=4)
print(arr2)

# Output:
# [4 4 4 4 4 4]

4. Use NumPy full() Function with Two-Dimensional Arrays

Let’s create a two-dimensional Numpy array with 4 rows and 3 columns with the value 8 for all elements. By using shape=(4,3), we’re indicating that we want the output to have 4 rows and 3 columns. The code fill_value=8 fills that 4×3 array with 8.


# Use numpy full() function with two-dimensional arrays
arr2 = np.full(shape=(4, 3),fill_value=8)
print(arr2)

# Output:
# [[8 8 8]
#  [8 8 8]
#  [8 8 8]
#  [8 8 8]]

5. Return Array with dtype=str

If you want full() output array in a specific data type uses the dtype argument. To return an array of values of type String use dtype=str. The following example returns a 2-D array with values filled with 2 but the data type of value is String.


# Return Array with dtype=str
arr2 = np.full((4,3),'2',dtype=str)
print(arr2)

# Output:
# [['2' '2' '2']
#  ['2' '2' '2']
#  ['2' '2' '2']
#  ['2' '2' '2']]

6. Return Array with dtype=float

To create a Numpy array that’s filled with floating point numbers instead of integers use dtype=float. The following example returns the array in float type.


# Return Array with dtype=float 
arr2 = np.full((3,4),'3',dtype=float)
print(arr2)

# Output:
# [[3. 3. 3. 3.]
#  [3. 3. 3. 3.]
#  [3. 3. 3. 3.]]

Conclusion

In this article, I have explained how to use numpy.full() function which returns an array of fill_value with the given shape, order, and datatype. By using this function, I have also explained how to fill values on 1-D and 2-D arrays and fill values with string & float types.

Happy Learning!!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing NumPy full() Function with Examples