Using numpy.full() function (6 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

numpy.full() is an incredibly versatile function provided by the numpy library, a staple in the Python data science ecosystem. It’s designed to create a new array filled with a specified value. This tutorial will delve into the practical usage of numpy.full(), showcasing it through six progressively complicated examples. Whether you’re a beginner or an intermediate user, understanding how to effectively employ numpy.full() can significantly streamline your data manipulation tasks.

The Fundamentals of numpy.full()

Before we dive into the examples, let’s briefly discuss what numpy.full() does. The syntax of the function is as follows:

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

Where:

  • shape: Defines the shape of the array. It can be an integer or a sequence of integers.
  • fill_value: The value to fill the array with.
  • dtype: Optional. Specifies the data type of the created array. If not provided, the data type of the fill_value is used.
  • order: Optional. Specifies the memory layout of the array. ‘C’ for row-major order (C-style) and ‘F’ for column-major order (Fortran-style).

Example 1: Basic Usage

import numpy as np

# Creating a 2x3 array filled with 7s
array = np.full((2, 3), 7)
print(array)

Output:

[[7 7 7]
 [7 7 7]]

In this basic example, we create a 2×3 array entirely filled with the number 7. This showcases the fundamental capability of numpy.full() to generate arrays of a specified shape and fill value.

Example 2: Using dtype Argument

import numpy as np

# Creating an array of type float
array = np.full((3, 4), 6.1, dtype=float)
print(array)

Output:

[[6.1 6.1 6.1 6.1]
 [6.1 6.1 6.1 6.1]
 [6.1 6.1 6.1 6.1]]

Here, we’ve defined the dtype parameter as float to ensure the array supports decimal numbers. It’s a clear demonstration of how to control the data type of your numpy array with numpy.full().

Example 3: Multidimensional Arrays

import numpy as np

# Creating a 3D array filled with 'x'
array = np.full((2, 2, 3), 'x')
print(array)

Output:

[[['x' 'x' 'x']
  ['x' 'x' 'x']]

 [['x' 'x' 'x']
  ['x' 'x' 'x']]]

This example extends the utility of numpy.full() to creating a multidimensional (3D in this case) array filled with a specified value (‘x’ here). It illustrates the flexibility of numpy.full() in handling arrays of any dimensionality.

Example 4: Using numpy.full() with np.zeros() Comparison

import numpy as np

# np.zeros() example for comparison
zeros_array = np.zeros((2, 3))

# Equivalent using np.full()
full_array = np.full((2, 3), 0)
print('np.zeros() output:\n', zeros_array)
print('np.full() equivalent:\n', full_array)

Output:

np.zeros() output:
 [[0. 0. 0.]
 [0. 0. 0.]]
np.full() equivalent:
 [[0 0 0]
 [0 0 0]]

This example contrasts the use of np.full() to np.zeros(). While np.zeros() is strictly for creating arrays filled with 0s, np.full() offers more versatility by allowing you to specify the fill value.

Example 5: Filling an Array with a Range

import numpy as np

# Using a loop to fill an array with a range of values
shape = (5,)
array = np.full(shape, np.nan)
for i in range(shape[0]):
    array[i] = i
print(array)

Output:

[0. 1. 2. 3. 4.]

Although numpy.full() is often used to fill an array with a single value, you can combine it with looping structures to assign different values to each element, as demonstrated here. This example highlights the function’s compatibility with Python’s control flow statements.

Example 6: Creating a Mask With numpy.full()

import numpy as np

# Creating a boolean mask
mask = np.full((5, 5), False)
mask[2:4, 2:4] = True
print(mask)

Output:

[[False False False False False]
 [False False False False False]
 [False False True  True  False]
 [False False True  True  False]
 [False False False False False]]

This advanced example shows how numpy.full() can be used to create a boolean mask for arrays. Such masks are extremely useful in conditional filtering and complex array operations, showcasing the profound flexibility of numpy.full().

Conclusion

The numpy.full() function is a powerful tool in the NumPy library, allowing for the creation of arrays with customized fill values. Through these six examples, ranging from basic to more complex, we’ve seen its versatility in various scenarios. Whether it’s creating arrays of a certain shape and size, manipulating data types, or crafting intricate array operations, numpy.full() is an indispensable function for any data manipulation task.