NumPy zeros - How to create zero vector or matrix and how to use dtype option

You can create a zero vector or matrix to use numpy.zeros.

import numpy

a = numpy.zeros(2)

print(a)  # [0. 0.]
print(type(a))  # <class 'numpy.ndarray'>

numpy.zeros returns a numpy.ndarray object, all the elements of which are 0.

Examples

import numpy

a1 = numpy.zeros(5)
a2 = numpy.zeros((2, 4))
a3 = numpy.zeros((2, 3), dtype=int)

print(a1)  # [0. 0. 0. 0. 0.]
print(a2)
'''
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
 '''
print(a3)
'''
[[0 0 0]
 [0 0 0]]
'''

As you see a2, if the argument is tuple, the return is a matrix. dtype is optional and the elements of a return vector or matrix are int if it is int.

NumPy Array

NumPy Tutorial