NumPy empty() is an inbuilt function that is used to return an array of similar shape and size with random values as its entries. It is defined under numpy, which can be imported as import numpy as np, and we can create multidimensional arrays and derive other mathematical statistics with the help of numpy, which is a library in Python.
Numpy empty()
To create an array with random values, use numpy empty() function. The empty() function is used to create a new array of given shape and type, without initializing entries.
Syntax
numpy.empty(shape,dtype,order)
Parameters
The np empty() method takes three parameters out of which one parameter is optional.
The first parameter is the shape; it is an integer or a sequence of integers, or we can say several rows.
The second parameter is the order, which represents the order in the memory, such as C_contiguous or F_contiguous.
The third parameter is optional and is the datatype of the returning array. By default, it is float.
Return Value
The empty() function returns an array with random values of its entries.
Example programs on empty() method in Python
Write a program to show the working of an empty() function in Python.
import numpy as np mat1 = np.empty(4, dtype=int) print("Matrix mat1 : \n", mat1) mat2 = np.empty([3, 3], dtype=int) print("\nMatrix mat2 : \n", mat2) mat3 = np.empty([2, 2]) print("\nMatrix mat3 : \n", mat3)
Output
Matrix mat1 : [16843009 16843009 16843009 16843009] Matrix mat2 : [[1 2 3] [4 5 6] [9 8 7]] Matrix mat3 : [[5.77068674e-321 5.81743661e+180] [6.01334412e-154 1.73303925e+097]]
In this example, we can see that bypassing the shape of the matrix and using the empty() function, and we are getting random values of the matrix.
Write a program to take a 4×4 matrix and then apply the empty() function.
See the following code.
import numpy as np a = np.empty([4, 4]) print("\nMatrix mat3 : \n", a)
Output
Matrix a : [[6.23042070e-307 4.67296746e-307 1.69121096e-306 6.23058707e-307] [2.22522597e-306 1.33511969e-306 1.37962320e-306 9.34604358e-307] [9.79101082e-307 1.78020576e-306 1.69119873e-306 2.22522868e-306] [1.24611809e-306 8.06632139e-308 1.60221208e-306 2.29178686e-312]]
In the above example, we can see that when we passed a 4×4 matrix, we are getting random values as the entries of the matrix.