Python numpy.empty() function

In this tutorial, we are going to understand about numpy.empty() function, it is really an easy to use a function which helps us create an array .numpy.empty() function helps us create an empty array, it returns an array of given shape and types without initializing entry of an array, the performance of the array is faster because empty does not set array values to zero. By using numpy.empty() function we can set all the values of the array manually.

Format= numpy.empty(size, data type = float, order = ‘F’)

Order can be of two types:

C Contiguous: it is used because the rows are stored as contiguous blocks of memory.

F Contiguous:  it is used because the columns are in contiguous blocks of memory:

How to use numpy.empty() function

import numpy as np

NumPy helps us to process arrays. It provides a multidimensional array object, and tools that help us to work on the array. When we call the import NumPy as np, we are reducing the phrase “numpy” to “np” to make our code easier to read. It also avoids namespace issues.

 

Creating an array by just giving the size of the array

f=np.empty(5)
f

Output

array([1.49293119e-311, 7.56602523e-307, 6.23054972e-307, 1.78021391e-306, 2.00274982e-307])

we can see that it is giving us dynamic floating values since we have not initialized the data type.

 

Creating an array by giving the size , order and data type of the array

f=np.empty([3,3],int)

Output

array([[-1925555984,         703,         100],
       [        116,         121,         112],
       [        101,         703, -1897248608]])

Here we can see that  we get dynamic integer values and a definite size of the array

Printing the type on the array

 

f.dtype

Output

Leave a Reply

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