Create an empty and a full NumPy array in Python

In this tutorial, we will explore how to create an empty and a full Numpy array in Python, accompanied by several practical examples.

  • NumPy (Numerical Python) is an open-source Python library widely utilized across various scientific and engineering disciplines.
  • The NumPy API is extensively employed in Pandas, SciPy, Matplotlib, scikit-learn, scikit-image, and the majority of other data science and scientific Python packages.
  • NumPy can be utilized to execute a broad range of mathematical operations on arrays.

creating an empty NumPy array in python

To create an empty NumPy array in python the following steps need to be achieved :

  • First step we need to perform is to import the numpy package using the import statement.
  • The empty() function in NumPy is used to create an array with a specified shape that is initialized with arbitrary values. The shape of this empty array is determined by the user.
import numpy as np
array1 = np.empty((2, 3))
print(array1)

output:

[[6.93167257e-310 6.93171505e-310 6.93167256e-310]
 [6.93167256e-310 6.93167256e-310 6.93167256e-310]]

From the above code, the output that is generated are the random values that was allocated in the memory.

Creating a full NumPy array in python with example

Python’s numpy module offers a function called “numpy.full()”.

To create an full NumPy array in python the following steps need to be achieved :

  • First step we need to perform is to import the numpy package using the import statement.
  • A full NumPy array is an array where all the elements have the same predefined value. This is useful when you want to initialize an array with a specific value.
import numpy as np
array_full = np.full((3, 3), 5)
print(array_full)

output:

We used the ‘np.full()’ function to create a 3×3 array filled with the value 5. Finally, we displayed the array using the ‘print()’ function.

[[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]]

Leave a Comment

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

Scroll to Top