How numpy full works in Python? Best example

How numpy full works in Python? Best example
“`html

When working with NumPy, creating arrays filled with a specific value is a common requirement. Whether you’re initializing a matrix for computations, setting a default value, or testing algorithms, numpy.full() is an efficient way to generate such arrays.

Understanding numpy.full()

The numpy.full() function creates a new NumPy array of a specified shape, filled with a given value. It provides a straightforward way to initialize an array without needing explicit loops.

Here’s the syntax:

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

Let’s break down each parameter:

  • shape: Defines the dimensions of the array. It can be an integer (for a 1D array) or a tuple (for higher-dimensional arrays).
  • fill_value: The constant value assigned to every element in the array.
  • dtype: Optional. Specifies the data type of the array, such as int, float, or bool.
  • order: Determines the memory layout of the array. 'C' (row-major) is the default, but 'F' (column-major) can be used.
  • like: Allows passing an existing array to influence the output type (introduced in NumPy 1.20).

Creating Basic Arrays Using numpy.full()

Let’s see a simple example. Suppose we need a 3×3 matrix filled with the number 7:

import numpy as np

arr = np.full((3, 3), 7)
print(arr)

Output:

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

This demonstrates how effortlessly numpy.full() creates a fixed-value array.

Specifying Data Type

By default, the resulting array inherits the data type of fill_value. However, you can explicitly define it:

arr = np.full((2, 4), 3.5, dtype=int)
print(arr)

Output:

[[3 3 3 3]
 [3 3 3 3]]

The decimal value 3.5 is cast to an integer, rounding it down.

Using Boolean and String Values

You’re not limited to numerical values. You can fill arrays with other data types, such as Boolean values:

arr = np.full((2, 3), True, dtype=bool)
print(arr)

Output:

[[ True  True  True]
 [ True  True  True]]

Similarly, you can create character-type arrays:

arr = np.full((2, 2), 'X', dtype=str)
print(arr)

Output:

[['X' 'X']
 ['X' 'X']]

Difference Between numpy.full() and Other Array Initialization Methods

NumPy offers other methods for initializing arrays with constant values, such as numpy.zeros() and numpy.ones(). Here’s how they compare:

Function Default Fill Value Usage Example
numpy.full() User-specified value np.full((2,2), 9)
numpy.zeros() 0 np.zeros((2,2))
numpy.ones() 1 np.ones((2,2))

If you need an array filled with a specific value, numpy.full() is the best option.

Advanced Usage: Column-Major Order

By default, arrays are stored in row-major (C-style) format. However, you can create a column-major (F-style) array using the order parameter:

arr = np.full((3, 3), 5, order='F')
print(arr)

Conclusion

The numpy.full() function is a highly efficient way to create predefined arrays in Python. Whether you need numerical values, strings, or booleans, it provides flexibility and control over the array’s type and storage order. Compared to other initialization methods, it excels when you need arrays filled with a specific value, making it an essential tool in scientific computing and data processing.

“` Other interesting article: How numpy ones works in Python? Best example