📜  np.zero - Python (1)

📅  最后修改于: 2023-12-03 15:17:59.483000             🧑  作者: Mango

Numpy.zeros() in Python

Numpy.zeros() function returns a new array of given shape and type and fills it with zeros.

Syntax
numpy.zeros(shape, dtype=float, order='C')
  • shape: Shape of the new array in the form of tuple (rows, columns).
  • dtype: Optional. The data type of the returned array. Default is float.
  • order: Optional. It specifies the memory layout order of the array. Default is 'C', row-major order.
Example
import numpy as np

# Creating an array with shape (3,4) and data type int
a = np.zeros((3,4), dtype=int)

print(a)

# Output
# [[0 0 0 0]
#  [0 0 0 0]
#  [0 0 0 0]]

In this example, we create a new array of shape (3,4) and data type int using np.zeros() function. The returned array is filled with zeros.

Conclusion

np.zeros() is a useful function for creating new arrays filled with zeros. It is particularly useful in situations where we need to create an array of specific shape and fill it with zeros before populating it with other values.