numpy.zeros in Python

In this article, we shall learn about numpy.zeros function. It generates ndarray filled with zeros .

Syntax

Let’s go through each of these parameters.

shape

Using shape parameter, you can specify shape of the ndarray of zeros that you want to generate. You may provide either an int or a tuple of ints for this parameter.

dtype

By default, the data type used in the ndarray of zeros is ‘float64’.

Using dtype parameter of numpy.zeros function, you can specify the data type for the ndarray filled with zeros.In the example below, I generated ndarray of ‘integer’ zeros.

Does the above example mean we can generate zeros of only a particular dtype using np.zeros ? Certainly not.In the below code snippet, I generated a ‘mixed’ ndarray of zeros containing both ‘float’ zeros and ‘int’ zeros.

When you print the dtype of the ndarray ‘a’, you can see that x is <f4 and y is <i8. <f4 means a little-endian float of 4 bits and <i8 means a little-endian int of 8 bits.

order

The order parameter is used to specify whether to store the multi-dimensional ndarray in row-major (C-style) or column-major (Fortran-style) order in memory.

Hope this article gave you a good description on the numpy.zeros function in Python.

Recent Posts

Menu