Last Updated : 24 Jan, 2025
numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros().
Let's understand with the help of an example:
Python
import numpy as np
#Create 1D array
arr = np.zeros(5)
print(arr)
Syntax of numpy.zeros()
Parameters:numpy.zeros(shape, dtype = None, order = 'C')
by using NumPy, we can easily create a 2D array filled with zeros using the numpy.zeros() function.
Python
import numpy as np
# Creating a 2D array with 3 rows and 4 columns
arr = np.zeros((3, 4))
print(arr)
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]Specifying Data Type (dtype)
dtype parameter in numpy.zeros() defines the type of data stored in the array.
Python
import numpy as np
# Create an array of tuples with zeros
d = np.zeros((2, 2), dtype=[('f', 'f4'), ('i', 'i4')])
print(d)
[[(0., 0) (0., 0)] [(0., 0) (0., 0)]]C vs F Order
Choosing the right memory layout can significantly improve performance, depending on our specific operations. If your operations are row-wise, use C-order. If they are column-wise, use F-order.
Python
import numpy as np
# Create a 2x3 array in C-order
e = np.zeros((2, 3), order='C')
print("C-order array:", e)
# Create a 2x3 array in F-order
f = np.zeros((2, 3), order='F')
print("F-order array:", f)
C-order array: [[0. 0. 0.] [0. 0. 0.]] F-order array: [[0. 0. 0.] [0. 0. 0.]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4