The NumPy full() function in Python is used to create an array with a specified shape and fill it with a given 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.
# Quick examples of full() function
# 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(shape=(4,3),fill_value='2',dtype=str)
# Example 4: Return array with dtype=str
arr2 = np.full(shape=(3,4),fill_value='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 the array with.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, you’re indicating that you want the output to have six elements and fill_value=4
specifies the array to be filled with value 4.
In the below example, np.full(shape=6, fill_value=4)
creates a 1-D array with a shape of 6, and all elements are filled with the value 4
. Adjust the shape
and fill_value
parameters as needed for your specific use case.
# Import numpy module
import numpy as np
# Use numpy full() function on 1-D array
arr2 = np.full(shape=6, fill_value=4)
print("Full 1-D array:\n", arr2)
Yields below output.
4. Use NumPy full() Function with Two-Dimensional Arrays
You can use the numpy.full()
function to create a two-dimensional array. For instance, np.full(shape=(4, 3),fill_value=8)
creates a 2-D array with a shape of (4, 3), and all elements are filled with the value 8
. Adjust the shape
and fill_value
parameters as needed for your specific use case.
# Use numpy full() function
# With two-dimensional arrays
arr2 = np.full(shape=(4, 3),fill_value=8)
print("Full 2-D array:\n", arr2)
# Output:
# Full 2-D array:
# [[8 8 8]
# [8 8 8]
# [8 8 8]
# [8 8 8]]
5. Return Array with dtype=str
To create a NumPy array with a specified data type (dtype
) of string, you can use the dtype
parameter in the numpy.full()
function.
In the below example, np.full(shape=(4,3),fill_value='2',dtype=str)
creates a 2-D array with a shape of (4, 3), and all elements are filled with the string value '2'
, and the data type is set explicitly to str
. Adjust the shape
, fill_value
, and dtype
parameters as needed for your specific use case.
# Return array with dtype=str
arr2 = np.full(shape=(4,3),fill_value='2',dtype=str)
print("2-D Array with dtype=str::\n", arr2)
# Output:
# 2-D Array with dtype=str::
# [['2' '2' '2']
# ['2' '2' '2']
# ['2' '2' '2']
# ['2' '2' '2']]
6. Return Array with dtype=float
If you want to create a NumPy array with a specified data type (dtype
) of float, you can use the dtype
parameter in the numpy.full()
function.
In the below example, np.full(shape=(3,4),fill_value='3',dtype=float)
creates a 2-D array with a shape of (3, 4), and all elements are filled with the float value 3
, and the data type is set explicitly to float
. Adjust the shape
, fill_value
, and dtype
parameters as needed for your specific use case.
# Return array with dtype=str
arr2 = np.full(shape=(3,4),fill_value='3',dtype=float)
print("2-D Array with dtype=float:\n", arr2)
# Output:
# 2-D Array with dtype=float:
# [[3. 3. 3. 3.]
# [3. 3. 3. 3.]
# [3. 3. 3. 3.]]
Frequently Asked Questions
The numpy.full()
function in NumPy is used to create an array with a specified shape and fill it with a constant value. Its primary purpose is to initialize an array where all elements have the same predetermined value.
You can create a 1-D array using the numpy.full()
function by specifying the shape parameter as a single integer.
o create a 2-D array using numpy.full()
, you need to specify the shape
parameter as a tuple of two integers representing the number of rows and columns in the array.
To create an array with a specific data type using numpy.full()
, you can use the dtype
parameter. The dtype
parameter allows you to explicitly specify the data type of the array.
To create a 2-D array with a specific order (either row-major or column-major), you can use the order
parameter in the numpy.full()
function. The order
parameter takes a string argument, where ‘C’ stands for row-major (default), and ‘F’ stands for column-major.
The numpy.full()
function fills the entire array with a constant value. If you need different values for each element, consider using other functions like numpy.array()
or specifying a list of values manually.
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!!
Related Articles
- NumPy flip() Function in Python
- Python NumPy Reverse Array
- How to Check NumPy Array Equal
- How to Use NumPy Argsort() in Python
- How to Use NumPy argmax in Python
- Python NumPy Split Array – Using split() Function
- How to Use NumPy Random choice() in Python?
- NumPy Variance Function in Python
- How to Use NumPy random seed() in Python
- NumPy nanmean() – Get Mean ignoring NAN Values
References
- https://numpy.org/doc/stable/reference/generated/numpy.ones.html#numpy.ones
- https://numpy.org/doc/stable/reference/generated/numpy.ones_like.html#numpy.ones_like