 # Numpy zeros_like Function Implementation in Python with Examples

Sometime you have to create a empty array or zero numpy array while coding. Using the numpy zeros_like method can solve it. What is the use of it ? It allows you to create array of zeros with the same dimension as the dimension of the input array. In this entire tutorial I will show you the implementation of the numpy zeros_like method.

## Syntax and Parameter for the Numpy zeros_like

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

Parameters :

a: Input array

dtype: Type of the output array you want. int or float e.t.c.

order : Overrides the memory layout of the result. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible.

subok : If True, then the newly created array will use the sub-class type of ‘a’, otherwise it will be a base-class array. Defaults to True.

shape: If it is true then it will overrides the shape of the results.

## Examples for the impmentation of Numpy zeros_like

### Example 1 : Creating array of zeros for single dimension array.

In this example I will create a single dimentsion numpy array and the find the array of zeros from it. Execute the following code to get the output.

``````import numpy as np
array_1d = np.arange(15)
np.zeros_like(array_1d)``````

You will get the output as an arrays of zeros with the same dimension as the input array.

### Example 2 : Creating array of zeros for two or multi dimensional array.

Now lets create arrays of zeros for 2 Dimensional array. The method is same. You have to just pass the input array to the numpy.zeros_like() method. Run the below code to implement this example.

``````import numpy as np
array_2d = np.arange(15).reshape(5,3)
np.zeros_like(array_2d)``````

Below is the output you will get.

## Where you can use Numpy zeros_like() method ?

You can use zeros_like() method when you want to ouput resultant arrays into it. The use of it won’t allocate or free any memory, which can save you a lot of time. For example I am finding median of the numpy array and outputting the result into arrays of zeros.

``````import numpy as np
array_2d = np.arange(15).reshape(5,3)
m = np.median(array_2d,axis=0)
dummy = np.zeros_like(m)
np.median(array_2d,axis=0,out=dummy)``````

Output ## Difference between Numpy zeros and Numpy zeros_like

You have to define the dimension for the numpy.zeros() method. But in case of zeros_like() dimensions is taken from the existing or input array.

Source:

Offical Numpy zeros_like() Documentation 