NumPy zeros_like – A Complete Guide

NumPy Zeros Like Cover Image

In this tutorial, we will be learning about the NumPy zeros_like method and also seeing a lot of examples regarding the same. So let us begin!

Recommended Read: NumPy zeros – A Complete Guide


What is NumPy zeros_like?

The zeros_like method in NumPy is a function that returns an array of zeros having the same shape and size as the given array.


Syntax of zeros_like

numpy.zeros_like(adtype=Noneorder=’K’subok=Trueshape=None)

ParameterDescriptionRequired/Optional
a (array_like)An object which defines the shape and data type of the array to be returned.Required
dtype (data type)The data type of the desired array. Overrides the data type of the result.Optional
orderThe desired order in which the multi-dimensional data is to be stored in the memory. It can be row-major (‘C’), column-major (‘F’), ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ implies matching the layout of a as much as possible.Optional
subok (bool)Determines whether the newly created array will use the sub-class type of a (subok=True) or will be a base class array (subok=False).
The default value is True.
Optional
shapeThe shape of the desired array. Overrides the shape of the result.Optional

Returns:
An array with the same shape and data type as the given array.


Examples of Numpy zeros_like function

Let’s now take a look at how the numpy.zeros_like() function works and what is the expected output for different types of inputs.

1-dimensional array using zeros_like

import numpy as np

a = np.arange(10)
print("a =", a)

b = np.zeros_like(a)
print("b =", b)

Output:

a = [0 1 2 3 4 5 6 7 8 9]
b = [0 0 0 0 0 0 0 0 0 0]

2-dimensional array using zeros_like

N x N array

import numpy as np

a = np.arange(10).reshape(2, 5)
print("a =\n", a)

b = np.zeros_like(a)
print("b =\n", b)

Output:

a =
 [[0 1 2 3 4]
 [5 6 7 8 9]]
b =
 [[0 0 0 0 0]
 [0 0 0 0 0]]

1 x N array

import numpy as np

a = np.arange(10).reshape(1, 10)
print("a =\n", a)

b = np.zeros_like(a)
print("b =\n", b)

Output:

a =
 [[0 1 2 3 4 5 6 7 8 9]]
b =
 [[0 0 0 0 0 0 0 0 0 0]]

N x 1 array

import numpy as np

a = np.arange(10).reshape(10, 1)
print("a =\n", a)

b = np.zeros_like(a)
print("b =\n", b)

Output:

a =
 [[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]
b =
 [[0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]]

1-dimensional float-type array

import numpy as np

a = np.arange(10)
print("a =", a)

b = np.zeros_like(a, dtype=float)
print("b =", b)

Output:

a = [0 1 2 3 4 5 6 7 8 9]
b = [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

2-dimensional float-type array

import numpy as np

a = np.arange(10).reshape(2, 5)
print("a =\n", a)

b = np.zeros_like(a, dtype=float)
print("b =\n", b)

Output:

a =
 [[0 1 2 3 4]
 [5 6 7 8 9]]
b =
 [[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]

Difference between zeros and zeros_like

Note that, in the zeros method we are creating a new array of our desired shape and data type having all the values as 0. But, here, we are directly passing an array or an array-like object to get an array of the same shape and data type. The NumPy zeros_like function takes more time than the NumPy zeros function to produce an array with all 0.


Conclusion

That’s all! In this tutorial, we learned about the Numpy zeros_like method and practiced different types of examples using the same.


Reference