Create Numpy Array of different shapes & initialize with identical values using numpy.full() in Python

In this article we will discuss how to create a Numpy Array of different shapes and initialized with same identical values using numpy.full().

numpy.full()

Python’s Numpy module provides a function to create a numpy array of given shape and all elements initialized with a given value,

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

Arguments:
shape: Shape of the new array
fill_value : Intialization value
dtype : Data type of elements | Optional

It returns a Numpy array of given shape and type, all elements in it will be initialized with fill_value.

To use Numpy in our code we need to include following module i.e.

import numpy as np

Checkout some examples,

Advertisements

Example 1:

Create a 1D Numpy Array of length 10 & all elements initialized with value 5

# Create a 1D Numpy Array of length 10 & all elements initialized with value 5
arr = np.full(10, 5)

Contents of the Create Numpy array:
[5 5 5 5 5 5 5 5 5 5]

Data Type of Contents of the Numpy Array : int32
Shape of the Numpy Array : (10,)

Example 2:

Create a 2D Numpy Array of 4 rows | 5 columns & all elements initialized with value 7

#Create a 2D Numpy Array of 4 rows & 5 columns. All intialized with value 7
arr = np.full((4,5), 7)

Contents of the Create Numpy array:
[[7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]]

Data Type of Contents of the Numpy Array : int32
Shape of the Numpy Array : (4,5)

Example 3:

Create a 3D Numpy Array of shape (2,4,5) & all elements initialized with value 8

# Create a 3D Numpy array & all elements initialized with value 8
arr = np.full((2,4,5), 8)

Contents of the Create Numpy array:
[[[8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]]

 [[8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]]]

Data Type of Contents of the Numpy Array : int32
Shape of the Numpy Array : (2, 4, 5)

Example 4:

Create initialized Numpy array of specified data type

Along with initialization value, we can specify the data type too i.e.

# Create a 1D Numpy array & all float elements initialized with value 9
arr = np.full(10, 9, dtype=float)

Contents of the Create Numpy array:
[9. 9. 9. 9. 9. 9. 9. 9. 9. 9.]

Data Type of Contents of the Numpy Array : float64

Complete example is as follows,

import numpy as np

def main():

   print('*** Create 1D Numpy Array filled with identical values ***')
   # Create a 1D Numpy Array of length 10 & all elements intialized with value 5
   arr = np.full(10, 5)

   print('Contents of the Numpy Array : ' , arr)
   print('Data Type of Contents of the Numpy Array : ', arr.dtype)
   print('Shape of the Numpy Array : ', arr.shape)

   print('*** Create 2D Numpy Array filled with identical values ***')
   #Create a 2D Numpy Array of 4 rows & 5 columns. All intialized with value 7
   arr = np.full((4,5), 7)

   print('Contents of the Numpy Array : ', arr, sep='\n')
   print('Data Type of Contents of the Numpy Array : ', arr.dtype)
   print('Shape of the Numpy Array : ', arr.shape)

   print('*** Create 3D Numpy Array filled with identical values ***')
   # Create a 3D Numpy array & all elements initialized with value 8
   arr = np.full((2,4,5), 8)

   print('Contents of the Numpy Array : ', arr, sep='\n')
   print('Data Type of Contents of the Numpy Array : ', arr.dtype)
   print('Shape of the Numpy Array : ', arr.shape)

   print('*** Create 1D Numpy Array of specified Data Type filled with identical values ***')

   # Create a 1D Numpy array & all float elements initialized with value 9
   arr = np.full(10, 9, dtype=float)

   print('Contents of the Numpy Array : ', arr)
   print('Data Type of Contents of the Numpy Array : ',  arr.dtype)
   print('Shape of the Numpy Array : ', arr.shape)



if __name__ == '__main__':
   main()


Output:
*** Create 1D Numpy Array filled with identical values ***
Contents of the Numpy Array :  [5 5 5 5 5 5 5 5 5 5]
Data Type of Contents of the Numpy Array :  int32
Shape of the Numpy Array :  (10,)
*** Create 2D Numpy Array filled with identical values ***
Contents of the Numpy Array : 
[[7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]]
Data Type of Contents of the Numpy Array :  int32
Shape of the Numpy Array :  (4, 5)
*** Create 3D Numpy Array filled with identical values ***
Contents of the Numpy Array : 
[[[8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]]

 [[8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]
  [8 8 8 8 8]]]
Data Type of Contents of the Numpy Array :  int32
Shape of the Numpy Array :  (2, 4, 5)
*** Create 1D Numpy Array of specified Data Type filled with identical values ***
Contents of the Numpy Array :  [9. 9. 9. 9. 9. 9. 9. 9. 9. 9.]
Data Type of Contents of the Numpy Array :  float64
Shape of the Numpy Array :  (10,)

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top