SoFunction
Updated on 2025-04-15

Detailed explanation of the usage of np.zeros_like function in python

Preface

np.zeros_likeis a function in the NumPy library that creates an array of the same shape and type as a given array and initializes all elements to 0.

grammar:

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

parameter:

  • a: The input array. The new array will have the sameaSame shape and type.
  • dtype(Optional): Specifies the data type to return the array. If not specified, the default is the input array.adata type.
  • order(Optional): Specify the memory layout of the return array.'C'is the main sequence (C-style),'F'is the main order (Fortran-style). Default is'K', means that the memory layout of the return array should be consistent with the input array as much as possible.
  • subok(Optional): IfTrue, the returned array will retain the type of the subclass. IfFalse, then the returned array isndarraytype. The default value isTrue

Return value:

Returns a new array with the same shape and type as the input array, and all elements of the array are initialized to 0.

Example of usage:

1. Create a zero array of the same shape as an existing array

import numpy as np

# Create an original arrayarr = ([[1, 2, 3], [4, 5, 6]])

# Create an array of zeros with the same shape as arrarr_zeros = np.zeros_like(arr)

print(arr_zeros)
# Output:# [[0 0 0]
#  [0 0 0]]

In this example,arr_zerosIt's one witharrArray of the same shape, and all elements are initialized to 0.

2. Specify the data type dtype

You can specify the data type of the new array without having to rely on the type of the input array. For example, if you want to return an array of floating point type:

arr_zeros_float = np.zeros_like(arr, dtype=float)

print(arr_zeros_float)
# Output:# [[0. 0. 0.]
#  [0. 0. 0.]]

Here, the returned array type isfloat, even if the original arrayarrThe element of  is an integer type.

3. Comparison with ()

np.zeros_like()and()Similar, butnp.zeros_like()A zero array will be created based on the shape and type of the existing array, and()The shape needs to be explicitly specified.

# Create a 2x3 zero array usingarr_zeros_manual = ((2, 3))

# Create an array of zeros based on arr using np.zeros_likearr_zeros_like = np.zeros_like(arr)

print(np.array_equal(arr_zeros_manual, arr_zeros_like))  # Output: True

Both create the shape as(2, 3)Array of zeros, butnp.zeros_like()Based on existing arrayarrThe shape and type of   automatically generates zero arrays.

4. Specify memory layout

passorderParameters, you can specify the memory layout that returns the array. By default,np.zeros_like()Can use'K', that is, try to be consistent with the input array, but you can also specify it as'C'or'F'

arr_zeros_c = np.zeros_like(arr, order='C')  # Use row-main orderarr_zeros_f = np.zeros_like(arr, order='F')  # Use column master order
print(arr_zeros_c.flags['C_CONTIGUOUS'])  # Output: True, indicating that it is C-style storageprint(arr_zeros_f.flags['F_CONTIGUOUS'])  # Output: True, indicating that it is Fortran-style storage

5. The difference between

()Used to create a zero array based on a given shape, andnp.zeros_like()Used to create an array of zeros with the same shape and data type as an existing array.np.zeros_like()Compare()More flexible because it can inherit the shape and type of an existing array without you re-specifying.

Summarize:

  • np.zeros_like(a): Create a withaA array of zeros with the same shape and data type.
  • Optional parametersdtypeYou can specify the data type that returns the array.
  • orderControls the memory layout of the returned array (default isK)。
  • It is often used to create zero arrays based on existing arrays, especially when the shape and type need to be consistent.

This approach is great for initializing an array of zeros with the same shape as other arrays when performing array calculations.

This is the end of this article about the detailed explanation of the usage of np.zeros_like function in python. This is the end of this article. For more related contents of python np.zeros_like function, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!