What is np.zeros and np.ones?

You can create a matrix full of zeroes or ones using np.zeros and np.one commands respectively. It can be used when you initialized the weights during the first iteration in TensorFlow and other statistic tasks.

The syntax is

Numpy Zero

numpy.zeros(shape, dtype=float, order='C')

Numpy Once

numpy.ones(shape, dtype=float, order='C')

Here,

Shape: is the shape of the array

Dtype: is the datatype. It is optional. The default value is float64

Order: Default is C which is an essential row style.

Example numpy zero

import numpy as np
np.zeros((2,2))

Output:

array([[0., 0.],
          [0., 0.]])

Example numpy zero with datatype

 import numpy as np
np.zeros((2,2), dtype=np.int16)

Output:

array([[0, 0],
         [0, 0]], dtype=int16)

Example numpy one 2D Array with datatype

import numpy as np
np.ones((1,2,3), dtype=np.int16)			
array([[[1, 1, 1],        
       [1, 1, 1]]], dtype=int16)			

 

YOU MIGHT LIKE: