How to empty an array in Numpy?

Numpy offers different ways to create and empty arrays. Let’s learn how to empty an array in Numpy. We will use the Numpy empty method and a clever trick.

Numpy empty array

There are 2 different ways to empty an array in Numpy.

Using the np.empty Function

The first method is to use the Numpy empty function.

import numpy as np

my_array = np.empty(shape=(0, 0))
print(f"My empty array: \n{my_array}")

Numpy empty array

An empty function creates an empty matrix. Thanks to shape parameters, you can define the shape of an array. Of course, you can change it in the future by using the reshape function.

See also  How to use interpolate in Numpy

Creating an Empty Array with np.array

Another way is just to create an array but not set any elements in that array. Python allows us to do that.

import numpy as np

my_array = np.array([])
print(f"My empty array: \n{my_array}")

Numpy empty array

The result is exactly the same. Which way would you prefer to choose to create an empty matrix in Numpy? Consider using numpy empty array or create empty matrix python techniques for optimal solutions in your projects.

See also  How to count number of zeros in Numpy array?