Understand numpy.empty(): It Cannot Create an Empty NumPy Array – NumPy Tutorial

By | May 16, 2020

If you plan to create an empty numpy ndarrary, numpy.empty() function can not do it. In this tutorial, we will use some examples to show you the reason.

Syntax

numpy.empty() is defined as:

numpy.empty(shape, dtype=float, order='C')

It can create a new array of given shape and type,  the value of array is randomized.

For example

Create an uninitialized int32 array

import numpy as np
d = np.empty([2, 2], dtype=np.int32)
print(d)

Run this code, you will get a new array.

[[         0          0]
 [         0 1072693248]]

You will find the value of d is randomized, which means you do not konw how they are generated.

Moreover, the array d is not empty.

Create an uninitialized float32 array

We also can create a float32 array using numpy.empty(). The example code is:

d = np.empty([2, 2], dtype=np.float32)
print(d)

The value of d is:

[[0.    0.   ]
 [0.    1.875]]

They are also randomized and d is not empty.

In general, numpy.empty() can not create an empty array.

Leave a Reply

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