Numpy.zeros_like() in Python
The zeros_like() method of Python numpy class returna an array of zeros with the same shape and type as that of specified array.
Syntax
1 2 3 |
numpy.zeros_like(a, dtype=None, order='K', subok=True) |
Parameter
The numpy.zeros_like() method consists of four parameters, which are as follows:
arrray : This parameter represents an array_like input
subok :It true is invoked then it represents newly created array will be sub-class of array else it represents a base-class array
order :The order parameter can be either C_contiguous or F_contiguous. C order means that operating row-rise on the array will be slightly quicker
FORTRAN-contiguous order in memory (the first index varies the fastest). F order means that column-wise operations will be faster.
dtype :It is an optional parameter. It depicts the data type of returned array, and by default, it is a float.
Return
This method returns an array of zeros having given shape, order and datatype.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
# Python Programming giving an example for # numpy.zeros_like method importnumpy as numpy array = numpy.arange(8).reshape(4, 2) print("Original array : \n", array) obj1 = numpy.zeros_like(array, float) print("\nMatrix : \n", obj1) array = numpy.arange(7) obj2 = numpy.zeros_like(array) print("\nMatrix : \n", obj1) |
Output
Original array :
1 2 3 4 5 6 |
[[0 1] [2 3] [4 5] [6 7]] |
Matrix :
1 2 3 4 5 6 |
[[ 0. 0.] [ 0. 0.] [ 0. 0.] [ 0. 0.]] |
Matrix :
1 2 3 4 5 6 |
[[ 0. 0.] [ 0. 0.] [ 0. 0.] [ 0. 0.]] |