Let’s talk about the NumPy zeros function.
If you’re doing data science in Python, you need to be able to work with numerical data. One of the primary tools for working with numerical data is the NumPy array.
In some cases, you will have data that you can import into Python. In those cases, you can use the NumPy
array() function to transform the data into a NumPy array.
But in other cases, you might be starting from scratch. You might not have any data yet, or you might otherwise need to create an empty NumPy array.
To do this, you can create an array that only contains only zeros using the NumPy
In this blog post, I’ll explain how to use the NumPy zeros function. I’ll explain the syntax of the function, some of the parameters that help you control the function, and I’ll show you some examples of how it works.
However, before we dive into the syntax of NumPy zeros, we’re going to quickly review NumPy arrays. Ultimately, to understand the NumPy zeros function though it helps to have a good understanding of NumPy arrays.
Having said that, if you want to skip ahead directly to the section about the
np.zeros() syntax, click here.
A quick review of NumPy arrays
A NumPy array is basically like a container that holds numeric data that’s all of the same data type. I’m simplifying things a little, but that’s the essence of them.
We can create a very simple NumPy array as follows:
import numpy as np np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
Which we can visually represent as follows:
Here, we’ve used the NumPy array function to create a 2-dimensional array with 2 rows and 6 columns. Notice as well that all of the data are integers. Again, in a NumPy array, all of the data must be of the same data type.
Keep in mind that NumPy arrays can be quite a bit more complicated as well. It’s possible to construct 3-dimensional arrays and N-dimensional arrays. For the sake of clarity though, we’ll work with 1 and 2-dimensional arrays in this post.
Creating empty arrays
There will be times when you’ll need to create an empty array. For example, you may be performing some computations and you need a structure to hold the results of those computations. In such a case, you might want to create an empty array …. that is, an array with all zeroes.
One way of doing this is with the NumPy
array() function. You can create a an empty NumPy array by passing in a Python list with all zeros:
The problem with this though is that it may not always be efficient. It’s a little cumbersome. And it would be very cumbersome if you needed to create a very large array or an array with high dimensions. For example, here’s some code to create an array with 3 rows and 30 columns that contains all zeros:
np.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,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
Honestly, this is a little ridiculous (and would be even more ridiculous if you made a larger array).
Hypothetically, you could also create a 1-dimensional array with the right number of zeros, and then reshape it to the correct shape using the NumPy reshape method.
To be honest though, that method is also cumbersome and a little prone to errors.
There must be a better way, right?
Yes, there is.
Enter, the NumPy zeros function.
The syntax of the NumPy zeros function
The NumPy zeros function enables you to create NumPy arrays that contain only zeros.
Importantly, this function enables you to specify the exact dimensions of the array. It also enables you to specify the exact data type.
The syntax works like this:
You basically call the function with the code
Then inside the
zeros() function, there is a set of arguments. The first positional argument is a tuple of values that specifies the dimensions of the new array.
Next, there’s an argument that enables you to specify the data type. If you don’t specify a data type,
np.zeros() will use floats by default.
The syntax for using the zeros function is pretty straightforward, but it’s always easier to understand code when you have a few examples to work with. That being the case, let’s take a look at some examples.
Examples: how to use the NumPy zeros function
Let’s first take a look at a very simple example.
A very simple example of using the numpy zeros function
Here, we’re just going to create a 1-dimensional NumPy array with 5 zeros.
Which creates a NumPy array that looks something like this:
This is very simple. There are 5 elements in the array. They are zeros. Moreover, they are all floating point numbers. Remember, the elements of a NumPy array must all be of the same data type, and if we don’t specify the data type, the function will create floats by default.
That leads us to the next example of how to use the NumPy zeros function: creating a NumPy array with a specific data type.
Create a numpy zeros array with a specific data type
Here, we’ll create a NumPy array with all zeros of a specific data type.
This is very easy to do. We’re going to call the
zeros() function just like we’ve done previously in this blog post.
But inside the function, we’re going to use the
dtype parameter to specify the specific data type of elements in the output array.
Here, we’re going to create an array with integers, so we’ll use the code
dtype = int.
np.zeros(3, dtype = int)
Which produces a NumPy array that looks like this:
Notice a few things. First of all, all of the elements of this array are integers.
You can examine the data type with the following code:
np.zeros(3, dtype = int).dtype
This code retrives the data type attribute from the resulting NumPy array. To be precise, the data type is
This is just one example. Python and NumPy support a couple dozen different datatypes, so you can use this technique to create a variety NumPy arrays with zeros of specific data types (floats, complex numbers, etc).
Create a numpy zeros array with a specific shape
Next, let’s talk about creating arrays with a specific shape.
We can do this with the shape parameter:
np.zeros(shape = (2, 3))
Which produces an array like the following:
Technically though, you don’t need to explicitly call out the
shape = parameter; you can simply specify the shape with a tuple of values, and Python will infer that it refers to the shape (i.e.,
shape is a “positional argument”).
You’ll see people do this a lot. They’ll create NumPy arrays with a specific shape, but won’t explicitly include the
shape = parameter in the syntax. I think that’s a bad practice though. Be explicit. I think it’s better to type
shape = (2, 3) and explicitly use the
shape parameter. It’s just clearer.
Putting the pieces together: specifying shape and data type
Finally, let’s put all of those pieces together and create a NumPy zeros array with a specific shape and a specific data type:
np.zeros(shape = (3, 5), dtype = 'int')
Which will produce a NumPy array that looks something like this:
What did we do here?
We used the NumPy zeros function to create a 3 by 5 NumPy array that contains all zeros. Integers, to be specific. 3 rows, 5 columns.
To do this, we used the
shape parameter to specify the shape, and we used the
dtype parameter to specify the data type.
If you’re feeling a little ambitious, you can use this tool to create much larger arrays and arrays with different shapes.
np.zeros(shape = (3, 3, 5), dtype = 'int')
Which would look something like this:
I recommend that you play with the syntax a little, but make sure that you practice the syntax with simple cases first. Several of the examples earlier in this post are perfect for daily practice. Try to recall the syntax. Try to remember it and practice it regularly until you have it memorized.
For more Python data science tutorials, sign up for our email list
Here at Sharp Sight, we teach data science. We want to help you master data science as fast as possible.
If you sign up for our email list, you’ll receive Python data science tutorials delivered to your inbox.
Want to learn data science in Python? Sign up now.