numpy.zeros_like() in Python: Creating an Array of Zeros

The numpy.zeros_like() function in NumPy creates an array of zeros with the same shape and data type as a specified existing array. This function is handy when we want to initialize an array with zeros but maintain an already existing array’s structure (shape and data type).

Syntax of numpy.zeros_like() Function

When we provide the original array or the input array as an argument into the numpy.zeros_like() function then we can effortlessly create zero-filled arrays with matching dimensions and data types which will also eliminate the need to explicitly specify these attributes.

Syntax:

numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)

Parameters:

  • a: The input array whose shape and datatype will be used to create the new array of zeros.
  • dtype (optional): The desired data type for the new array. If not specified, the data type of the input array is used.
  • order (optional): Specifies the memory layout of the result. The default is ‘K’ which means the order is determined by the input array’s memory layout.
  • subok (optional): If True, then sub-classes will be passed through. If False, the returned array will always be a base-class array.
  • shape (optional): Deprecated parameter. If present, it overrides the shape of the input array.

Return:

A new array of zeros with the same shape and data type as the input array.

Examples of numpy.zeros_like() Function

Let us now look at some examples to demonstrate the use of numpy.zeros_like() function in Python.

Creating a 1D Zeros Array

In this example, we will only pass a one-dimensional array as the input into the numpy.zero_like() function which will give us an array of zeros with the same shape and datatype as the input array.

import numpy as np
A = np.array([21,52,93])
B=np.zeros_like(A)
print("Input Array: ",A)
print("Output Array: ",B)

Here we have imported the NumPy library as np and then with its help we created a one-dimensional array and saved the result into a variable A. Then we passed the array A into the numpy.zeros_like() function to convert the A array into a zeros array with the same shape and same data type and at last we have printed the variables A and B.

Output:

Creating a 1D Zeros Array

We can see in the output that all the elements from array A were converted into zeros in array B. Both the input array: A and zeros array: B have the same shape and data type.

Creating a 2D Zeros Array

Here we will additionally import the random module from the NumPy Library. Then by using the random() function we will create a two-dimensional random array having 3 rows and 4 columns and save the result into a C variable. Then we will convert this 2D random array into a zeros array with the same shape and data type by passing it into the numpy.zero_like() function and we will save the result into the variable D.

import numpy as np
from numpy import random
C = np.random.random((3,4))
D = np.zeros_like(C)
print("Input Array: ",C)
print("Output Array: ",D)

Output:

Creating a 2D Zeros Array

In the above output, we can see that we got an array of 3 rows and 4 columns filled with zeros of float data type as the input array we have passed was also of float data type.

Changing the Default Data Type

As we know the default data type of the zeros array is the same as the datatype of the input array we pass. Now if we want to change the datatype of our array D then we can pass a second parameter dtype with some other data type into the numpy.zeros_like() function.

import numpy as np
D=np.zeros_like(C,dtype=int)
print("Output Array: ",D)

In the above example, we were getting the zeros array of data type float. But here we have passed dtype as int into the numpy.zeros_like() function means we have changed the data type of the zeros array from float to integer data type.

Output:

Changing the Default Data Type

We can see that we got a zeros array but this time all the zeros are of integer data type.

Another similar article: numpy.zeros() in Python

Summary

In this tutorial, we have discussed numpy.zeros_like() function provided by Python’s NumPy library and also explored how to create a 1D and 2D zeros array using it. We also learned to change the default data type of the zeros array. After reading this tutorial, we hope you can easily create an array filled with zeros using numpy.zeros_like() in Python.

Reference

https://numpy.org/doc/stable/reference/generated/numpy.zeros_like.html

Priyanshu Singh
Priyanshu Singh
Articles: 41