In Python, the
numpy.zeros() function returns a new array of given shape and type, filled with zeros.
Here is the parameter table of
If it sounds great to you, please continue reading, and you will fully understand the
numpy.zeros() function through Python code snippets and vivid visualization.
- Concretely, I will introduce its syntax and parameters.
- Then, you will learn some basic examples of this function.
- Finally, I will address three top questions about
numpy.zeros 3d array,
You can find all the codes in this tutorial here.
Syntax and Parameters
Here is the syntax of
# Syntax numpy.zeros(shape[, dtype=float[, order='C'[, *[, like=None]]]])
Here is the parameter table of
|The shape of the new array, e.g., |
|data-type, optional||The desired data type of the array, e.g., |
|Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Default is |
|Reference object to allow the creation of arrays that are not NumPy arrays. If an array-like passed in as like supports the |
The output of
numpy.zeros() function is an array of zeros with the given
dtype, and order.
Generally, you will only need to land arguments on the first two parameters,
dtype, to control the shape and data type of the output all-zeros array.
We can just pass a
shape argument to the
np.zeros and get an all-zeros array back.
For example, we pass a size of
(3,3) to the shape parameter and the
numpy.zeros returns a new all-zeros array with the size
(3,3) and default date type,
# Basic Example import numpy as np shape = (3, 3) all_zeros_array = np.zeros(shape) print(all_zeros_array)
Besides, we can pass an
int to the
For instance, let’s pass 3 to the
shape parameter and see what the
# Basic Example 2 import numpy as np shape = 3 all_zeros_array = np.zeros(shape) print(all_zeros_array)
Great! It returns an all-zeros array with the shape of
(1,3). So when you pass an integer to the
shape argument, the
numpy.zeros will return a one-dimension array with the length as the input integer.
np.zeros() 3d array
So far, we have understood how to generate 1d and 2d all-zeros array with the
numpy.zeros. What about a 3d array or even a higher dimensional array? We can achieve it in two ways.
First, you can explicitly pass an
(x, y, z) alike tuple to the
shape parameter to get a 3d all-zeros array. For example, we’d like a 3d array with the size of
(1, 2, 3).
import numpy as np # three_dim Example 1 shape = (1, 2, 3) all_zeros_array = np.zeros(shape) print(all_zeros_array)
💡 Tip: Take a closer look at the output and see what the size of
(1, 2, 3) means. 🙂
Second, you can use the little multiplication trick and get a higher-dimensional all-zeros array in a more compact way. In order to use this trick, you need to ensure that every dimension size in your new array is the same.
For instance, let’s create a 4d all-zeros array with the size of
(2, 2, 2, 2).
import numpy as np # three_dim Example 2 shape = (2,) * 4 print('shape:\n', shape) print('-'*85) all_zeros_array = np.zeros(shape) print('output array:\n', all_zeros_array)
⭐ Caveat: Remember to add the comma when representing a single-element tuple.
np.zeros() vs. np.zeros_like()
When searching for
np.zeros, you might encounter another alike method,
The main difference between
np.zeros_like is that the former returns an all-zeros array based on the shape you pass and the latter one based on the array-like thing you pass.
For example, to get a 2d all-zeros array, you can either use our old friend,
np.zeros, or use our new friend,
import numpy as np # np.zeros way: shape = (2, 3) first_output = np.zeros(shape) print('first output:\n', first_output) print('-' * 85) # np.zeros_like way: # given a 2d array like thing array_like = [[1, 2, 3], [4, 5, 6]] second_output = np.zeros_like(array_like) print('second output:\n', second_output)
Yes, as shown above, we can use either
np.zeros_like to generate the same size all-zeros array.
But you may notice that the data types of the output arrays are not the same. This is because we do not specify the dtype parameter in the first output, so it takes the default
numpy.float64 data type.
Besides, the input
np.zeros_like has an integer data type. Therefore, the
np.zeros_like will return an array of zeros with the same shape and type as a given array, which has the integer data type.
To sum up, you can choose to use
numpy.zeros_like based on what you have.
When to use
np.zeros() and when
If you want to make an all-zeros copy of an array-like object, you will be better off using the
numpy.zeros_like. Otherwise, please consider the
Based on our previous example, you probably get more curious about another commonly used parameters of
Let’s learn more about it!
To jog your memory, here is the parameter table of
First, you can specify the data type of the output array through the
For example, in our previous 2d array, we also want the first array to have an integer data type.
import numpy as np # dtype unspecified: shape = (2, 3) first_output = np.zeros(shape) print('previous first output:\n', first_output) print('-' * 85) # dtype = np.int8: shape = (2, 3) first_output = np.zeros(shape, dtype=np.int8) print('present first output:\n', first_output)
Lastly, if you want to custom data type for each field, you can also a list of tuples to the
For example, in a 2d array, I want the
x field (the first column) to be
numpy.float64 and the
y field (the second column) to be
import numpy as np # dtype unspecified: shape = (3, 2) output = np.zeros(shape, dtype=[('x', np.float64), ('y', np.int8)]) print('output:\n', output) print('\noutput dtype:\n', output.dtype)
That’s it for our
We learned about its syntax, parameters, and basic examples.
We also worked on the top three questions about the
np.zeros() function, ranging from
numpy.zeros 3d array,
Hope you enjoy all this, and happy coding!
Anqi Wu is an aspiring Data Scientist and enthusiastic Python Freelancer. She is an incoming student for a Master’s program in Analytics and builds her Python Freelancer profile on Upwork.
Anqi is passionate about machine learning, statistics, data mining, programming, and many other data science related fields. She has proven her expertise during her undergraduate years, including multiple winning and top placements in mathematical modeling contests. She loves supporting and enabling data-driven decision-making, developing data services, and teaching.
She is skilled at programming languages like Python, R, and SQL, actively delving into the world of Machine Learning and Deep Learning and traveling along her data science journey with joy. Data sensitivity and business acumen are her advantages to march towards the career path as a data scientist 🙂
Here is a link to the author’s website: https://www.anqiwu.one/. She uploads data science blogs weekly to document her data science learning and practicing for the past week, along with some best learning resources and inspirational thoughts.
I hope you enjoy this article! Cheers!