|

Fill NumPy Arrays with numpy.fill and numpy.full

Filling NumPy arrays with a specific value is a typical task in Python. It’s common to create an array, then initialize or change some values, and later reset the array to a starting value. It’s also common to initialize a NumPy array with a starting value, such as a no data value. These operations may be especially important when working with geographical data like raster and NetCDF files.

There are two simple ways to fill NumPy arrays. You can fill an existing array with a specific value using numpy.fill(). Alternatively, you can initialize a new array with a specific value using numpy.full(). NumPy also has built-in functions to create and fill arrays with zeros (numpy.zeros()) and ones (numpy.ones()).

We’ll start with a demonstration of how to change all the values in an existing array (numpy.fill()), then move on the to built-in function to initialize arrays with specific values.

All of the code for this lesson is going to be written as if you were using an interactive console (like the Anaconda prompt, bash, terminal, cmd, etc.). Make sure you have imported NumPy as follows to use the code snippets in this tutorial

import numpy as np

numpy.fill()

To start, let’s create a simple, empty array. If you’re not familiar with how to create NumPy arrays you can check out my guide. This code creates a 1D array with 5 elements. In a real-world example, this could be an array that I initialized and then wanted to assign another value to later.

a = np.empty(5)

The array should look similar to the result below. Your values will be slightly different. Again, for a description of creating arrays with NumPy visit my guide. It will explain the creation methods used in this tutorial.

array([7.56603881e-307, 6.23054972e-307, 6.23053954e-307, 1.02360052e-306,
 1.78020984e-306])

Now, let’s fill that array with a specified value using numpy.fill(). Numpy.fill() is a very simple function. It can be called on any NumPy array of any dimension. It takes one argument, the value that will fill all array elements. For example, to fill the array we created with the value 2, use the following line of code.

a.fill(2)

Your result should match the one below.

array([2., 2., 2., 2., 2.])

That’s all for numpy.fill() it’s a very simple, very powerful, and very useful function.

numpy.full()

With numpy.full() we can combine the two lines of code from the last section (one line to create an empty array, and one line to fill the array with a value) into a single function. Numpy.full() is useful when you want to initialize an array and already know the value you want to array to be initialized to. For example, you may want to create a raster that is initially filled with a specific no data value.

Two arguments must be specified for numpy.full(): the shape of the array, and the fill value. Remember, NumPy array shapes are defined as tuples. Two optional arguments can also be specified: the data type (dtype) and whether to use C or Fortran order to store the data. Usually, the defaults for these arguments are fine.

To create a 2D array with 5 rows and 5 columns that is filled with -9999, use the following code.

a = np.full((5,5), -9999)

And the result . . .

array([[-9999, -9999, -9999, -9999, -9999],
       [-9999, -9999, -9999, -9999, -9999],
       [-9999, -9999, -9999, -9999, -9999],
       [-9999, -9999, -9999, -9999, -9999],
       [-9999, -9999, -9999, -9999, -9999]])

Notice the values are integers. If I wanted the values to be floating point, I simply need to pass a floating point value (i.e. add a decimal) to np.full, like so.

a = np.full((5,5), -9999.)

Then check the array data type to ensure it is indeed a float.

a.dtype

And it is.

dtype('float64')

Once again, a very simple function that is very useful.

numpy.zeros()

Numpy.zeros() is basically a special implementation of numpy.full(). It takes a shape argument and the optional data type and order arguments. The result is an array filled with zeros. We can create the same array as above with zeros like so.

np.zeros((5,5))

And the result . . .

array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

numpy.ones()

Numpy.ones() functions the same as numpy.zeros(), but (obviously) initializes an array of ones. The arguments are the same; shape, data type, and order.

Conclusion

Filling arrays is a simple, yet important operation. It is an operation that you will conduct many times. Understanding the various methods available to fill arrays can help you streamline your programs and analyses by eliminating unnecessary code.

Similar Posts