numpy.full() in Python: An Easy Guide

The numpy.full() function in the NumPy library is designed to create a new array with a specified shape, filling all its elements with a constant value. It provides a convenient and efficient way to generate arrays with uniform values, such as creating matrices initialized with a specific constant. This function is particularly useful in scenarios where a predefined constant needs to be assigned to an entire array of a given shape.

In this article, we will understand Python numpy.full() function, and its syntax, and demonstrate it with various examples. Let’s get started.

Also Read: NumPy Array Addition with numpy.add() and Addition Operator

Syntax of numpy.full() Function

The numpy.full() function takes the shape of the desired array, the constant value to be assigned to each element, an optional data type parameter, and an optional order parameter specifying the memory layout.

Syntax:

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

Parameters:

  • shape: The shape of the new array. It can be an integer or a tuple of integers.
  • fill_value: The constant value to fill the array with.
  • dtype (optional): The data type of the array. If not specified, the data type is inferred from the fill_value.
  • order (optional): Specifies the memory layout of the array (‘C’ for C-style, ‘F’ for Fortran-style). The default is ‘C’.

Return:

It returns a new array of the defined shape filled with the specified constant value.

Let us now look at some examples to demonstrate the use of numpy.full() function in Python.

Creating a 1-D Array Filled with Constant Value Using numpy.full()

Let’s pass the shape of the output array and the fill_value to get a one-dimensional array filled with a constant value fill_value.

Example:

import numpy as np

f1=np.full(3,79)
print(f1)

Here we have first imported the NumPy library as np and then we have passed the first parameter which is the shape of our output array as 3 means there will be 3 elements in our 1-D array and passed the second parameter fill_value which means the constant value in the output array as 79. We then saved the result in the variable f1 and printed it.

Output:

Creating a 1-D Array Filled with Constant Value Using numpy.full()

We can see in the output that we got a one-dimensional array filled with constant value 79 of data type integer.

Changing the Data Type of the Output Array in numpy.full()

By passing parameter dtype into the numpy.full() function we can change the data type of the output array to some other data type as by default the data type of the output array is the same as the data type of fill_value which we have passed.

Example:

import numpy as np

f1 = np.full(3,79,dtype=float)
print(f1)

The only difference with the above example is that here we have passed the dtype parameter as float which means our output array will be of float data type.

Output:

Changing the Data Type of the Output Array in numpy.full()

We can see in the output that we got floating-point numbers in our output array.

Creating a 2-D Array Filled with Constant Value Using numpy.full()

We can also create a two-dimensional full array using numpy.full() function, for this we have to pass the number of rows and columns of the 2-D array in the form of a tuple for the shape parameter.

Example:

import numpy as np

f2=np.full((2,3),50.9)
print(f2)

Here we have passed (2,3) which means in the output array there will be 2 rows and 3 columns and there will be a constant value of 50.9 at each position in the full array as we have passed 50.9 for the second parameter which is fill_value.

Output:

Creating a 2-D Array Filled with Constant Value Using numpy.full()

In the output above, we got the two-dimensional array of 2 rows and 3 columns and at each position, there is a constant value of 50.9 in the array.

Creating a 3-D Array Filled with String Using numpy.full()

In this example, we will create a three-dimensional full array by passing the shape in the numpy.full() function which contains all three dimensions of an array in the form of a list, with this we also pass a string as a constant value.

Example:

import numpy as np

f3=np.full([2,3,4],'python')
print(f3)

Output:

Creating a 3-D Array Filled with String Using numpy.full()

We got a three-dimensional full array where at each position there is a constant value of string data type.

Conclusion

Now that we have reached the end of this article, we hope it has elaborated on the different ways to create new arrays using numpy.full() function exclusively from the NumPy library. Here’s another article that explains how to use the numpy.where() in Python. CodeForGeek has many other entertaining and equally informative articles that can be of great help to those who want to advance in Python, so be sure to check them out as well.

Reference

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

Priyanshu Singh
Priyanshu Singh
Articles: 44