NumPy full() function

Numpy Full

In this article, we will try to understand the full() function of the NumPy package in Python.

NumPy is a popular Python library for scientific computing that provides tools for working with large, multi-dimensional arrays and matrices of numerical data. One of the functions provided by NumPy is full(), which returns a new array of given shape and type, filled with a fill value.

The function allows you to create an array of any size and shape, and fill it with a specified value. This can be useful when you need to create an array of a certain size and type as a placeholder for other data, or when you want to initialize an array with a default value.

The full() function is a simple and efficient way to create and populate arrays with a single function call.

What is the full() function in NumPy?

This function is used to create a new array of given shapes and types. The values of the array are initialized with the fill_value passed in parameters. One can also provide the data type of the new array, and its order in the parameter.

Syntax of NumPy full()

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

Parameters

  • shape: int or sequence of ints
    • Required
    • Dimensions of a new array; can be single integers or tuples of integers.
  • fill_value: scalar or array_like
    • Required
    • Value to be added to the new array
  • dtype: data-type
    • Optional
    • Data-type of the elements in the new array; default = None, means np.array(fill_value).dtype.
  • order: {‘C’, ‘F’}
    • Optional
    • Which order should be used to store multidimensional data – C or Fortran-contiguous (row- or column-wise) 
  • like: array_like
    • Optional
    • To enable the generation of arrays that aren’t NumPy arrays, reference objects are provided. The outcome will be determined by an array-like provided in as like if it complies with the array function protocol. In this instance, it makes sure that an array object is created that is compatible with the one that was provided as an argument.

Implementation of NumPy full()

Make sure to import the NumPy package in your IDE before implementing the function. To do so, run the following line of code.

import numpy as np

Example 1. Passing only required parameters

Let’s create an array of a specific size and shape filled with a constant value

#one dimentional array
np.full(2,4)

#two dimentional array with singular element
np.full((2,3),4)

#two dimentional array with multiple element
np.full((2,3),[1,2,3])
Example 1
Example 1

Example 2. Passing other parameters

Let’s now create an array of a specific size and data type filled with a constant value.

np.full((3,3), [2,4,6], dtype=np.float16)

np.full((3,2), [2.8,1.9], dtype=int, order='F')

np.full((3,3), [1+2j], dtype=complex)
Example 2
Example 2

Conclusion

NumPy packages make working with arrays much easy. the full() function is a simple way to return a new array of a given shape and type, filled with fill_value.

Reference

https://numpy.org/doc/stable/reference/generated/numpy.full.html