Sometimes you want to create an empty numpy array without any value. There are many methods to create it. But in this tutorial, you will know how to make or create an empty numpy array using various methods.
Methods on How to Make an Empty Numpy Array
Let’s know all the methods that are generally used by the developers to create an empty numpy array. Just follow these methods for deep understanding.
Method 1: Make an Empty Numpy Array using the empty() function
The first method to make an empty numpy array is the use of the empty() function. It is a function provided by the NumPy module. Here you will pass a tuple for the number of rows and columns. It will create numpy array for that dimension.
Run the below lines of code to make an empty numpy array.
import numpy as np empty_array = np.empty((4, 5)) print(empty_array )
Method 2: Use the zeros() function
The second method to make or initialize the numpy array is the use of the zeros() function. This function creates a numpy array filled with the 0 elements only. It is just like an empty numpy array.
Just passe the tuple for the number of rows and columns and the function will create the array.
Execute the below lines of code to achieve that.
import numpy as np empty_array = np.zeros((4, 5)) print(empty_array )
There are also other methods you can use for creating an empty array. These functions are numpy.ones() , numpy.empty_like(), numpy.zeros_like(), or numpy.ones_like() e.t.c. All the functions that are with like are used to create the numpy array with the same dimension as an existing numpy array. The numpy.ones() function create numpy array with all elements of 1.
Creating an empty numpy array is very easy. You have to just properly follow the above methods. You can use the np.empty() function or np.zeros()function to initialize the array.
I hope you must have liked this article on how to How to create an Empty Numpy Array. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.