# NumPy zeros_like – A Complete Guide

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

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.