In this NumPy article, I will explain what is NumPy zeros in Python, its syntax, parameters, and return values. I will explain various examples with different parameters, and I will also explain what the NumPy zeros_like() is.
The numpy.zeros() function in Python efficiently creates arrays filled with zero values, which can be of various dimensions, including 1D, 2D, or higher. While the np.zeros_like() is a function in NumPy that returns a new array with the same shape and type as a given array, filled with zeros.
NumPy zeros in Python
The numpy.zeros() function is a built-in NumPy Python routine that generates a new array of a specified size, filled with zeros.
This is particularly useful in situations where we need to initialize an array in Python with a default value of zero before filling it with more meaningful data.
The zeros serve as a placeholder, ensuring that the NumPy array in Python is the correct size and shape for subsequent operations.
Example: Let’s see one array created with the help of the np.zeros() function in Python.
import numpy as np
example_array = np.zeros(5)
print(example_array)
Output: The implementation of the code is:
[0. 0. 0. 0. 0.]
Now, let’s see how exactly the np.zeros() function in Python works:
np.zeros Syntax
The syntax of the NumPy zeros in Python is:
numpy.zeros(shape, dtype=float, order='C')
NumPy Zeros Parameters Required
The parameters required for the NumPy zeros in Python are:
Name | Description |
---|---|
shape | The shape parameter is a single integer or a tuple of integers that define the dimensions of the new array in Python. For e.g., shape=3 would create a one-dimensional array with three elements, while shape=(3, 4) would create a two-dimensional 3×4 array. |
dtype | The data type parameter dtype is optional and specifies the desired data type for the elements in the array in Python. The default data type is float, but it can be changed to other types such as int, complex, bool, etc. |
order | The order parameter is also optional and can be either ‘C‘ for a C-style row-major array, or ‘F‘ for a Fortran-style column-major array in Python. The default is ‘C‘. |
numpy.zeros() Return Value
The numpy.zeros() function in Python returns an array filled with zeros.
The shape of the array is specified by the shape argument, which can be an integer for a one-dimensional array or a tuple for a multi-dimensional array in Python.
The dtype argument specifies the data type of the array elements, with the default being float64.
np.zeros in Python use cases
Let’s see some of the examples where the conditions are different with respect to the parameters:
Case 1: 1D array from numpy.zeros in Python without dtype and order
Here, we have to create a NumPy 1D array in Python but with default dtype and order parameters.
import numpy as np
array_1d = np.zeros(5)
print(array_1d)
Output: The implementation of the code:
[0. 0. 0. 0. 0.]
This way we can use NumPy zeros in Python to create a 1D array.
Case 2: 2D array from numpy.zeros() without dtype and order
For instance, we have to create a 2D NumPy array in Python with the help of the np.zeros() function.
import numpy as np
array_2d = np.zeros((3, 4))
print(array_2d)
Output: The implementation with a screenshot is given below:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
The NumPy zeros in Python can be used to create a 2D array.
Case 3: numpy.zeros() with int dtype parameter
For example, let’s create an array of integers with only zeros in Python.
import numpy as np
arr_int = np.zeros((2, 3), dtype=int)
print("Integer Array:\n", arr_int)
Output: The implementation of the Python code is:
Integer Array:
[[0 0 0]
[0 0 0]]
This way we can use NumPy zeros in Python to create an array with only zeros in the form of integers.
Case 4: NumPy zeros complex dtype parameter
For instance, create an array of only zeros in the complex form with the help of the NumPy zeros in Python.
import numpy as np
arr_complex = np.zeros((2, 3), dtype=complex)
print("Complex Array:\n", arr_complex)
Output: The result of the code is mentioned below with the screenshot:
Complex Array:
[[0.+0.j 0.+0.j 0.+0.j]
[0.+0.j 0.+0.j 0.+0.j]]
This way we can use the np.zeros() function to create an array full of zeros in the form of complex type in Python.
Case 5: numpy.zeros() with order=C
Consider a situation where we have to create a C-style row-major array in Python with the help of the NumPy zeros() function.
import numpy as np
array_c = np.zeros((2, 3), order='C')
print("Row-major (C-style) array:\n", array_c)
Output: The implementation of the code with a screenshot:
Row-major (C-style) array:
[[0. 0. 0.]
[0. 0. 0.]]
This way we can use the NumPy zeros in Python to create a C-style row-major array.
Case 6: NumPy zeros in Python with order=F
Here, we have to create a Fortran-style column-major array in Python using numpy.zeros() function.
import numpy as np
array_f = np.zeros((2, 3), order='F')
print("Column-major (Fortran-style) array:\n", array_f)
Output:
Column-major (Fortran-style) array:
[[0. 0. 0.]
[0. 0. 0.]]
This way we can use the NumPy zeros in Python to create a Fortran-style column-major array.
np.zeros_like function in Python
The np.zeros_like() is a function in NumPy Python that returns a new array with the same shape and type as a given array, filled with zeros.
This is particularly useful when we want to create an array of zeros with the exact dimensions of another NumPy array in Python, without having to manually specify the size.
Here’s a basic usage example:
import numpy as np
existing_array = np.array([[1, 2, 3], [4, 5, 6]])
zeros_array = np.zeros_like(existing_array)
print(zeros_array)
Output: The zeros_array will have the same shape as the existing_array, but all its elements will be initialized to 0.
[[0 0 0]
[0 0 0]]
Note: The np.zeros_like() in Python also takes additional parameters such as dtype and order.
For example, if we want to create a zeros array of floats using the shape of an existing integer array through Python:
import numpy as np
existing_array = np.array([[1, 2, 3], [4, 5, 6]], dtype=int)
zeros_array_float = np.zeros_like(existing_array, dtype=float)
print(zeros_array_float)
Output: In this case, zeros_array_float has the same shape as existing_array, but it’s of type float.
[[0. 0. 0.]
[0. 0. 0.]]
This way we copy an array type and create an array of the same dimension that contains only zeros using the np.zeros_like function in Python.
Conclusion
The NumPy zeros in Python function is a simple yet powerful tool, offering an efficient way to create zero-filled arrays of any shape and size. Its ability to specify data types and storage orders provides the flexibility needed for various applications in scientific computing.
With this guide, users can effectively leverage numpy.zeros() and np.zeros_like() in their Python programming for initializing arrays, optimizing performance, and laying the groundwork for complex computational tasks.
You may like to read:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.