5.2.4. Creating arrays of zeros or ones#
To create arrays filled with zeros or ones, we use the np.zeros and np.ones functions, which both take the shape of the array to create:
import numpy as np
np.zeros((2, 5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.ones((3, 4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
Tip
Note the double parenthesis. The argument of np.zeros and np.ones is a shape, which is a tuple. Writing np.zeros(2, 5)
would generate an error, since this is not one tuple argument, but two integer arguments.