Software, Tech & Coding simplified.

NumPy How to Create an Empty Array

To create an empty NumPy array:

  1. Specify the shape of the array.
  2. Call the numpy.empty() function.

For instance, let’s create an empty array with no elements:

import numpy as np

arr = np.empty(0)

print(arr)

Output:

[]

However, creating an array without elements rarely makes any sense. Instead, you should know and specify the shape of the final array in advance.

For instance, let’s create an empty 2D array:

import numpy as np

# Empty 2 x 3 matrix
arr = np.empty([2, 3])

print(arr)

Output (contains arbitrary values due to the uninitialized elements):

array([[1.23674196e-316, 0.00000000e+000, 6.94758172e-310],
       [6.94758172e-310, 0.00000000e+000, 6.94741422e-310]])

In this guide, you learn:

  • How to create an empty array using numpy.empty() function.
  • What emptiness means in the context of NumPy arrays.
  • Why a truly empty array is rarely useful?
  • Create an array of zeros using the numpy.zeros() function.
  • numpy.empty() vs numpy.zeros().

The numpy.empty() Function

The numpy.empty() function creates an array without initializing its entries.

The complete syntax for using this function is:

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

Where:

  • shape describes the shape of the empty array. It can be a tuple or a singular integer value.
  • dtype is an optional parameter that determines the datatype for the array elements. By default, this is numpy.float64.
  • order is an optional parameter that specifies how to store the multidimensional data in memory. The options are:
    • ‘C’ for C-style row-major form
    • ‘F’ for Fortran-style column-major form.
  • like is an optional parameter. It is a reference object that makes it possible to create non-NumPy arrays.

Each of these parameters serves a useful purpose. However, in this guide, we are only going to focus on the mandatory shape parameter.

For example, let’s create an empty array with zero elements:

import numpy as np

arr = np.empty(0)

print(arr)

Output:

[]

Noitce that this is almost never useful!

This is because NumPy array elements are stored in contiguous blocks of memory.

To add rows/columns into an existing array, such as to the empty array you just created, the array needs to be copied to a new memory location.

As you can imagine, this is very inefficient especially if done many times repeatedly.

Instead, you should create an “empty” array that has the shape of your result array.

To do this, use the numpy.empty() function but specify the shape of the array as a parameter. Then fill in the values to the empty array.

For instance, let’s create an empty 2D array that represents a 2 x 3 matrix:

import numpy as np

arr = np.empty([2, 3])

print(arr)

Output:

array([[1.23674196e-316, 0.00000000e+000, 6.94758172e-310],
       [6.94758172e-310, 0.00000000e+000, 6.94741422e-310]])

As you can see, now there are some random values in the array, even though it is supposed to be empty.

Emptiness means that the elements in the array are not initialized. But the array is not really empty. Instead, the array values are arbitrary and depend on what happens to be in the chunk of memory allocated for them.

But why is this useful?

As explained, appending directly to a numpy array is not possible (not even by using the numpy.append function). Instead, you inefficiently create copies of the array each time you append values to it.

This is why the best bet is to create an “empty” array that has the desired shape of the array you want to create. Then you can just fill in the values to the array as you go. This saves you from wasting computing time in copying the array.

An alternative way to create an “empty” array with NumPy is by using the numpy.zeros() function.

Let’s take a look at how it works.

numpy.zeros() Function

Another common way to initialize “empty” arrays is to use the numpy.zeros() function.

This returns an array where each element is zero.

The working principle is almost identical to the numpy.empty() function.

For instance, let’s initialize some arrays with zeros:

np.zeros(3)      # array([ 0.,  0.,  0.])
np.zeros([2, 2]) # array([[ 0.,  0.], [ 0.,  0.]])

Unlike the numpy.empty() function, numpy.zeros() produces an array that has zeros in it.

The full syntax for the numpy.zeros() function is identical to the numpy.empty() function.

numpy.zeros(shape, dtype=float, order='C', *, like=None)

See the parameter descriptions in the previous chapter.

Now that you know how to create empty NumPy arrays, let’s discuss which one you should use.

numpy.empty() vs numpy.zeros()

So far you have learned two similar ways to create “empty” NumPy arrays:

  • numpy.empty()
  • numpy.zeros()

However, it might be tricky to decide which one to use.

Let’s make a quick comparison between the two to make things more clear to you.

  • The numpy.empty() function does not initialize the elements in the array.
  • The numpy.zeros() function initializes the elements at 0.

This means numpy.empty() function is usually faster because it does not spend time initializing the zeros.

But at the same time, using the numpy.empty() function, you need to manually set all the values in the array. This is because you do not want to leave the arbitrary values hanging there.

Thus it is usually preferable to use the numpy.zeros() function to initialize “empty” arrays, even though it can be less efficient.

Conclusion

Today you learned how to create an empty NumPy array.

To recap, numpy.empty(0) creates an empty array with 0 elements.

np.empty(0)

However, creating an empty array without any elements is rarely useful. This is because adding elements to a NumPy array means creating a copy of the array. This is usually inefficient.

Instead, you should create an empty array with shape information. In other words, you want to know the size of the final array in advance.

np.empty([2, 3])

Notice that using numpy.empty() means the empty elements are going to have arbitrary values.

Use it with caution!

Alternatively (and usually preferably), use the numpy.zeros() function for creating “empty” arrays. This function fills the array with zeros as initial values.

Thanks for reading.

Happy coding!

Further Reading

Best Data Science Courses

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email