Updated March 28, 2023
Introduction to NumPy Empty
In Python, the NumPy module is used for working with array and to create a new array without initializing any entries of the array, which means the objects in the array will be initialized to value as “none” with given shape and the type and such creation of array are done by using a function known as an empty() function having the default data type as float and is required to set the values of the array manually by the users. In Python, the NumPy module also provides another function known as numpy.zeros() to create a new empty as done by the numpy.empty() function.
Working of NumPy empty() Function
In this article, we will discuss the NumPy function empty(). The Numpy is mainly used for dealing with a multi-dimensional array or array, and for working on this, the NumPy module provides many different functions in Python. In this article, we will elaborate on the empty() function, which is used for the creation of a new array with uninitialized values with none value with given shape and data type. This function can not only create a one-dimensional array, but also it can create a multi-dimensional array with rows and columns in it, wherein each cell data needs to be manually set by the users, else the NumPy array object will be returned with some garbage value, and hence it is faster when compared to other initialization array functions to zeros or ones.
Syntax of NumPy Empty
Now let us see in the below section the syntax and examples of the numpy.empty() function.
Syntax:
numpy.empty(shape, dtype, order)
Parameters:
- shape: this parameter is required to define the shape of the array either in the int or tuples of int.
- dtype: this parameter is an optional parameter that is used to define the data type to create an empty array, and by default, its value is set to float.
- order: this parameter is an optional parameter to arrange or order the multi-dimensional array in row and columns, where to arrange in row-wise then the value is set to “C” (C- style), and if we want to arrange in column-wise, then we have to set to “F” (Fortran style), and by default, this value is set to C.
This function returns a new empty array having the entries or data that are not initialized but having the array object initialized to none with defined or a given shape and data type parameters.
Example of NumPy Empty
Now let us demonstrate this function in the below simple example:
Code:
import numpy as np
print("Program to demonstrate empty function with various ways")
print("\n")
arr1 = np.empty(4, dtype = int)
print("The array with garbage value is as follows")
print(arr1)
print("\n")
arr2 = np.empty([4, 2], dtype = int)
print("The multi-dimensional array with garbage value is as follows")
print(arr2)
print("\n")
arr3 = np.empty([2,2])
print("The array with having only shape parameter")
print(arr3)
print("\n")
arr4 = np.empty([3,2], order = 'C')
print("The multi-dimensional array with parameter order in row wise")
print(arr4)
print("\n")
arr5 = np.empty([3,2], order = 'F')
print("The multi-dimensional array with parameter order in column wise")
print(arr5)
Output:
In the above program, we can see; first, we have imported a NumPy module, then we have written an alias naming for numpy as “np” and can be used for working of empty() function to create a new empty array using different parameters in first we are just creating a one-dimensional array with data type as “int,” and the number of elements in that one-dimensional array is declared as 4, so it prints an array containing 4 elements with int value but garbage value. Then we are creating two- dimensional array, which will again create a matrix with 4 rows and 2 columns with data type int. Similarly, we have created an array with only a shape parameter where we can see in the output it has values as 0 assigned to it. Lastly, we can see we have declared an empty() function with shape and order parameter with the value “C,” which will display the multi-dimensional array in row-wise with 3 rows and 2 columns as shown in the above screenshot. Then we also have seen how we can display the multi-dimensional in column-wise using the order specified using “F” in the above screenshot as an output of the above various ways of NumPy. empty() function with different parameters in different ways.
In Python, in the NumPy module, there is similar another function as an empty() function such as zeros() function, which is also used for creating an empty array, but it can initialize to zero instead of garbage value as initialized while using the empty() function. As this one of the slight difference (initialization of zero and garbage value using zeros() and empty() function respectively) between empty() and zeros() function of NumPy module in Python. These two functions have similar syntax, but as in the zeros function, initialization of zero is not done in the empty() function as it displays garbage value because it has entries with uninitialized values. Therefore it may be faster, and it usually requires a manual setting of values by the users. Still, we should be careful while manually setting values instead of garbage values in numpy.empty() function.
Conclusion
In this article, we conclude that Python can define an empty array or multidimensional array; it uses a NumPy module that provides a function for creating an empty array using numpy.empty() function, which will not initialize any entries in the array with the given shape and data type such as int and float as the default value for this function. In this article, we also saw the empty() function’s syntax with the parameters in detail. Furthermore, in this article, we also saw a simple example where we wrote the program to demonstrate all the parameters used. Finally, this article also saw the difference between the zeros() and empty() functions.
Recommended Articles
This is a guide to NumPy Empty. Here we discuss the Working of NumPy empty() Function with the example and output. You may also have a look at the following articles to learn more –