Updated March 28, 2023

## Introduction to NumPy zeros_like

Whenever we have an array whose values must be replaced with all zeroes and the array size and shape must be retained as the original array, we make use of a function called zeros-like function in numpy. And these zeros like functions takes four parameters arrayname, datatype, memory order, and subok, among which the datatype and subok parameters are optional, and datatype represents the data type of the value stored in the array whose name is represented by the first parameter arrayname, memoryorder represents the order in the memory and subok represents a Boolean value which is true if the array returned by using zeros like function is a subclass of the input array and false if the returned array is same as the original array. In this topic, we are going to learn about NumPy zeros_like.

### Syntax

The syntax for NumPy zeros_like function in Python is as follows:

`numpy.zeros_like(arrayname, datatype, memoryorder, subok)`

where arrayname is the name of the array whose values must be replaced with zeros without a change in the size and shape of the array,

The data type is the data type of the values stored in the array. The default datatype for the given values in the array is float. This parameter is optional.

Memoryorder represents the order in the memory. The subok represents a Boolean value that is true if the array returned by using zeros like function is a subclass of the input array and false if the returned array is the same as the original array. This parameter is optional.

### Working of NumPy zeros_like function

- Whenever we have an array whose values must be replaced with all zeroes and the array size and shape must be retained as the original array, we make use of a function called zeros like function in numpy.
- The zeros like functions take four parameters arrayname, datatype, memoryorder, and subok, among which the datatype and subok parameters are optional.
- datatype represents the data type of the value stored in the array whose name is represented by the first parameter arrayname.
- memoryorder represents the order in the memory.
- subok represents a Boolean value which is true if the array returned by using zeros like function is a subclass of the input array and false if the returned array is the same as the original array.

### Examples of NumPy zeros_like

Different examples are mentioned below:

#### Example #1

Python program to demonstrate NumPy zeros like function to create an array using array function in numpy and then using zeros like function to replace the elements of the array with zeros:

**Code:**

```
#importing the package numpy
import numpy as n
#Creating an array by making use of array function in NumPy and storing it in a variable called orgarray
orgarray = n.array([[1,2],[3,4]])
#Displaying the elements of orgarray followed by one line space by making use of \n
print ("The elements of the given array are:")
print (orgarray)
print ("\n")
#using zeros like function of NumPy and passing the created array as the parameter to that function to replace all the elements of the array with zeros and store it in a variable called zerosarray
zerosarray = n.zeros_like(orgarray, float)
#Displaying the array consisting of all zero elements
print ("The array with all its elements zero after using zeros like function is as follow:")
print (zerosarray)
```

**Output:**

In the above program, we are importing the package numpy, which allows us to make use of the functions array and zeros_like. Then we are creating an array called orgarray by making use of the array function in numpy. Then the elements of the array orgarray are displayed on the screen. Then we are making using zeros_like function, and the newly created array orgarray is passed as a parameter to the function to convert all the elements of the array to zeros without changing the size and shape of the array, and the resulting array is stored in a variable called zerosarray. Finally, the elements of the zerosarray are displayed on the screen.

#### Example #2

Python program to demonstrate NumPy zeros like function to create an array using array function in numpy and then using zeros like function to replace the elements of the array with zeros:

**Code:**

```
#importing the package numpy
import numpy as n
#Creating an array by making use of array function in NumPy and storing it in a variable called orgarray
orgarray = n.array([[5,6],[7,8]])
#Displaying the elements of orgarray followed by one line space by making use of \n
print ("The elements of the given array are:")
print (orgarray)
print ("\n")
#using zeros like function of NumPy and passing the created array as the parameter to that function to replace all the elements of the array with zeros and store it in a variable called zerosarray
zerosarray = n.zeros_like(orgarray, int)
#Displaying the array consisting of all zero elements
print ("The array with all its elements zero after using zeros like function is as follow:")
print (zerosarray)
```

**Output:**

In the above program, we are importing the package numpy, which allows us to make use of the functions array and zeros_like. Then we are creating an array called orgarray by making use of the array function in numpy. Then the elements of the array orgarray are displayed on the screen. Then we are making using the zeros_like function. The newly created array orgarray is passed as a parameter to the function to convert all the elements of the array to zeros without changing the size and shape of the array. The datatype int is also passed as the parameter, which displays the zeros in the resulting array as integer values. Then the resulting array is stored in a variable called zerosarray. Finally, the elements of zerosarray are displayed on the screen.

### Conclusion

In this tutorial, we understand the concept of NumPy zeros like function in Python through definition, the syntax of zeros like function, and the working of zeros like functions through programming examples and their outputs.

### Recommended Articles

This is a guide to NumPy zeros_like. Here we discuss the Working of NumPy zeros_like function and Examples along with the codes and outputs. You may also look at the following articles to learn more –