The shape override gives the result dimensions of (3, 2), while the input supplies its dtype. I find that split striking: changing the shape does not change the default dtype.
Consider the zero-filled result NumPy makes from an existing array. Ask which defaults come from that template.
What is NumPy zeros_like?
NumPy’s zeros_like function creates a zero-filled array from an existing array, carrying over its shape and data type unless you override either property.
That makes zeros_like useful when the next calculation needs a zero array aligned with an array you already have. NumPy’s zeros function is the alternative when you want to specify the dimensions directly.
| Default from the input | Meaning |
|---|---|
| shape | The output dimensions match the template. |
| dtype | The output uses the template’s element type. |
Prerequisites
The examples assume Python, NumPy, and basic array creation.
- Import NumPy with the conventional np alias.
- Use zeros_like when an existing array should define the output shape and default data type.
Step 1: Create zeros from an existing array
Use the input array as the template when each zero should line up with one existing element. This example starts with a 2-by-3 integer array, then prints both arrays so you can compare their dimensions.
import numpy as np
values = np.arange(6).reshape(2, 3)
zeros = np.zeros_like(values)
print("template:\n", values)
print("zeros:\n", zeros)
print("shape and dtype:", zeros.shape, zeros.dtype)
The result has shape (2, 3) and dtype int64, inherited from values. I ran this example with NumPy 2.4.6, and the printed zero array kept both rows and all three columns.
Step 2: Override the dtype or shape
Pass dtype when the zero values need a different numeric type, or shape when the result needs different dimensions. Both options override the corresponding property that would otherwise come from the template.
| Call | Effect |
|---|---|
| np.zeros_like(values) | Uses values.shape and values.dtype |
| np.zeros_like(values, dtype=np.float64) | Keeps the shape and uses float64 |
| np.zeros_like(values, shape=(3, 2)) | Uses the supplied shape and the input dtype |
float_zeros = np.zeros_like(values, dtype=np.float64)
reshaped_zeros = np.zeros_like(values, shape=(3, 2))
print(float_zeros.dtype)
print(reshaped_zeros.shape)
The output is float64 for the first call and (3, 2) for the second. The shape override changes the dimensions, so it no longer matches the template even though the input still supplies the default dtype.
Choose zeros_like or zeros
Choose zeros_like when an existing array should supply the shape and, unless overridden, the dtype. Choose zeros when you already know the dimensions and want to state them directly.
- Template array available: use zeros_like(template).
- Dimensions known independently: use zeros(shape).
For example, zeros_like(values) follows the array’s current dimensions, while zeros((2, 3)) spells out the requested dimensions. See the NumPy zeros guide for the shape-based call.
Keep the template’s metadata in view
The input does more than provide a count of elements. Its shape and dtype set the defaults, so integer input produces integer zeros unless you pass another dtype.
NumPy also accepts order to control memory layout, subok to choose whether to preserve an input subclass, and device for Array API interoperability. The current NumPy reference says device must be cpu when supplied, so do not treat this argument as general GPU placement.
check = np.zeros_like(values)
print(check.shape == values.shape)
print(check.dtype == values.dtype)
Both checks print True for the default call. Keep the template when its metadata expresses the output you need, and pass only the overrides that should differ.
NumPy zeros_like questions
What does NumPy zeros_like do?
It creates an array filled with zeros, using an input array’s shape and data type by default.
How do I make zeros_like return floats?
Pass a floating-point dtype, such as dtype=np.float64, to override the input array’s data type.
How is zeros_like different from zeros?
zeros_like takes an array template and inherits its shape and dtype by default. zeros takes the requested shape directly.

