1. Introduction

NumPy provides four useful functions to create a new array based on an existing array's shape and data type.

Function Purpose
np.zeros_like() Creates an array filled with zeros.
np.ones_like() Creates an array filled with ones.
np.empty_like() Creates an array without initializing its elements to specific values.
np.full_like() Creates an array filled with a specified value.
The main advantage: We do not need to specify the dimensions manually. NumPy takes the shape from an existing array.

2. Common example for all four functions

First, create an original array:

import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) print(arr)

Output:

[[10 20 30] [40 50 60]]

Our original array has:

  • Shape: (2, 3)
  • Number of dimensions: 2
  • Number of elements: 6
  • Data type: int64 or another integer type, depending on the platform and input.

We will use this array to understand all four functions.


3. np.zeros_like()

Definition: Returns a new array filled with zeros, using the shape and, by default, the data type of an existing array.

Syntax

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

Here, a is the existing array.

Example 1: Basic usage

import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) result = np.zeros_like(arr) print(result)

Output:

[[0 0 0] [0 0 0]]

Explanation:

  • zeros_like() creates a new array.
  • It copies the shape (2, 3).
  • Every element is initialized to zero.
  • The output data type defaults to the input array's data type.

Example 2: One-dimensional array

arr = np.array([5, 10, 15, 20]) result = np.zeros_like(arr) print(result)

Output: [0 0 0 0]

Example 3: Change the data type

arr = np.array([10, 20, 30]) result = np.zeros_like(arr, dtype=float) print(result) print(result.dtype)

Output:

[0. 0. 0.] float64

The dtype=float argument overrides the default data type.

Real-world application

Suppose we want to initialize a matrix to store students' marks before calculating the results.

marks = np.array([[80, 90, 70], [60, 75, 85]]) result = np.zeros_like(marks) print(result)

Remember: zeros_like() is useful when we need a zero-filled array with the same dimensions as an existing array.


4. np.ones_like()

Definition: Returns a new array filled with ones, using the shape and, by default, the data type of an existing array.

Syntax

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

Example 1: Basic usage

import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) result = np.ones_like(arr) print(result)

Output:

[[1 1 1] [1 1 1]]

The shape remains (2, 3), but all elements become 1.

Example 2: One-dimensional array

arr = np.array([100, 200, 300, 400]) result = np.ones_like(arr) print(result)

Output: [1 1 1 1]

Example 3: Using a floating-point data type

arr = np.array([10, 20, 30]) result = np.ones_like(arr, dtype=float) print(result)

Output: [1. 1. 1.]

Example 4: Creating a mask

In data processing, an array of ones can represent elements that are initially active or selected.

data = np.array([15, 25, 35, 45]) mask = np.ones_like(data, dtype=bool) print(mask)

Output: [ True True True True]

The dtype=bool argument makes each element Boolean. The integer value 1 is converted to True.

Remember: ones_like() is useful when we need to initialize every element to one, including in mathematical calculations and array-based algorithms.

5. np.empty_like()

Definition: Returns a new array with the same shape and, by default, the same data type as an existing array, but without initializing its elements to specific values.

Syntax

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

Example 1: Basic usage

import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) result = np.empty_like(arr) print(result)

The output might look like this:

[[ 0 0 0] [ 0 0 0]]
⚠️ Important: Your output may contain different numbers, including very large or very small values. The values are unpredictable because NumPy does not initialize the array elements to zero or any other specific value.

Why does empty_like() behave this way?

When NumPy creates an empty array, it allocates memory for the elements but does not initialize them to specific values. This can be faster than zeros_like() or ones_like() when we intend to overwrite every element before reading it.

Example 2: Fill the array after creation

arr = np.array([10, 20, 30, 40]) result = np.empty_like(arr) result[:] = 100 print(result)

Output: [100 100 100 100]

Here, result[:] = 100 replaces every element, so the previous uninitialized contents no longer matter.

Example 3: Using a loop

arr = np.array([10, 20, 30, 40]) result = np.empty_like(arr) for i in range(len(arr)): result[i] = arr[i] * 2 print(result)

Output: [20 40 60 80]

Every element is assigned before the result is printed.

Important precautions
  • Never assume that empty_like() returns zeros.
  • Do not read uninitialized elements before assigning valid values.
  • Use it when you can populate all elements yourself.

Remember: empty_like() is useful for performance-sensitive code where every element will be overwritten before it is used.


6. np.full_like()

Definition: Returns a new array with the same shape as an existing array, filled with a specified value.

Syntax

np.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)

The most important argument is fill_value, which specifies what value should be placed in every element.

Example 1: Fill with 99

import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) result = np.full_like(arr, 99) print(result)

Output:

[[99 99 99] [99 99 99]]

Example 2: Fill with zero

arr = np.array([10, 20, 30, 40]) result = np.full_like(arr, 0) print(result)

Output: [0 0 0 0]

This produces the same element values as zeros_like(arr) with the default data type.

Example 3: Fill with a floating-point value

arr = np.array([10, 20, 30]) result = np.full_like(arr, 2.5, dtype=float) print(result)

Output: [2.5 2.5 2.5]

The explicit dtype=float allows the decimal values to be represented as floating-point numbers. Without it, the default integer data type could convert the fill value to an integer, losing the fractional part.

Example 4: Fill with a Boolean value

arr = np.array([10, 20, 30, 40]) result = np.full_like(arr, True, dtype=bool) print(result)

Output: [ True True True True]

Remember: full_like() is useful when we need an array of the same shape as an existing array, but filled with a value of our choice.

7. Comparison of all four functions

Use the same original array for each example:

arr = np.array([10, 20, 30])
Zeros
np.zeros_like(arr)

[0 0 0]

Every element is zero.

Ones
np.ones_like(arr)

[1 1 1]

Every element is one.

Empty
np.empty_like(arr)

Uninitialized values

Elements are not initialized to specific values.

Full
np.full_like(arr, 7)

[7 7 7]

Every element is the chosen value, seven.

Quick revision table

Feature zeros_like() ones_like() empty_like() full_like()
Shape from input Yes Yes Yes Yes
Default dtype from input Yes Yes Yes Yes
Initial element values Zero One Uninitialized Specified value
Custom dtype Yes Yes Yes Yes
Best use Zero initialization One initialization Manual population Custom initialization

Note: All four functions accept an optional shape argument in modern NumPy versions, allowing you to override the shape of the output array.


8. Difference between like() and regular array creation

NumPy also provides zeros(), ones(), empty() and full().

The key difference is that the regular functions require a shape, whereas the *_like() functions normally take an existing array.

import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) # Specify the dimensions manually a = np.zeros((2, 3)) # Derive the dimensions from an existing array b = np.zeros_like(arr) print(a) print(b)

Both produce:

[[0. 0. 0.] [0. 0. 0.]]
Important Difference
np.zeros((2, 3)) defaults to a floating-point data type (float64), while np.zeros_like(arr) defaults to the data type of arr, which is an integer type in this example.

9. Practice programs

Try these exercises before checking the solutions.

Quick Quiz

0 of 5

1. Create an array of zeros with the same shape as [5, 10, 15, 20].

np.zeros_like(arr)
np.ones_like(arr)
np.empty_like(arr)

2. What is the output of np.ones_like(np.array([4, 8, 12]))?

[0 0 0]
[1 1 1]
[4 8 12]

3. Which function is designed to create an array without initializing elements to specific values?

np.full_like()
np.empty_like()
np.zeros_like()

4. Which function creates an array filled with 50?

np.full_like(arr, 50)
np.ones_like(arr)
np.empty_like(arr)

5. What does np.zeros_like(arr) normally preserve from arr?

Only its elements
Its shape and data type
Only its values

Quiz Completed!

You answered 0 out of 5 correctly.

Programming exercises

  • Create a two-dimensional array and generate a zero-filled array using zeros_like().
  • Create a Boolean array filled with True using ones_like().
  • Create an array using empty_like() and fill it with the squares of the original elements.
  • Create a floating-point array filled with 5.5 using full_like().
  • Create an integer array, then compare the output data types of zeros_like(arr) and zeros_like(arr, dtype=float).