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. |
2. Common example for all four functions
First, create an original array:
Output:
Our original array has:
- Shape:
(2, 3) - Number of dimensions:
2 - Number of elements:
6 - Data type:
int64or 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
Here, a is the existing array.
Example 1: Basic usage
Output:
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
Output: [0 0 0 0]
Example 3: Change the data type
Output:
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.
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
Example 1: Basic usage
Output:
The shape remains (2, 3), but all elements become 1.
Example 2: One-dimensional array
Output: [1 1 1 1]
Example 3: Using a floating-point data type
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.
Output: [ True True True True]
The dtype=bool argument makes each element Boolean. The integer value 1 is converted to True.
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
Example 1: Basic usage
The output might look like this:
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
Output: [100 100 100 100]
Here, result[:] = 100 replaces every element, so the previous uninitialized contents no longer matter.
Example 3: Using a loop
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
The most important argument is fill_value, which specifies what value should be placed in every element.
Example 1: Fill with 99
Output:
Example 2: Fill with zero
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
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
Output: [ True True True True]
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:
Zeros
[0 0 0]
Every element is zero.
Ones
[1 1 1]
Every element is one.
Empty
Uninitialized values
Elements are not initialized to specific values.
Full
[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.
Both produce:
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
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?
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
Trueusingones_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.5usingfull_like(). - Create an integer array, then compare the output data types of
zeros_like(arr)andzeros_like(arr, dtype=float).