numpy.full_like() function is used to create a new NumPy array filled with a specified value while using an existing array as a reference. By default, the new array has the same shape and data type as the reference array. It is useful when:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
result = np.full_like(arr, 7)
print(result)
Output
[[7 7 7] [7 7 7]]
Explanation:
- Input array has 2 rows and 3 columns.
- New array has the same shape.
- Output also uses an integer data type.
- Every element is filled with 7.
Syntax
numpy.full_like( a, f ill_value, dtype=None, order='K', subok=True, shape=None )
Parameters:
- a: The input array used as a reference for the shape and data type.
- fill_value: The value used to fill every element of the new array.
- dtype: Specifies the data type of the output array. By default, the data type of a is used.
- order: Specifies the memory layout of the output array.
- subok: Determines whether a subclass of the input array should be preserved.
- shape: Specifies a different shape for the output array.
Using a Different Fill Value
This parameter determines the value stored in every element.
import numpy as np
arr = np.array([10, 20, 30, 40])
result = np.full_like(arr, 5)
print(result)
Output
[5 5 5 5]
Explanation:
- Input array contains four elements.
- Output array also contains four elements.
- Every element is initialized with 5.
Changing Data Type
By default, numpy.full_like() uses the data type of the input array. The dtype parameter can be used to change it.
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.full_like(arr, 2.5, dtype=float)
print(result)
Output
[2.5 2.5 2.5 2.5]
Explanation:
- Original array contains integers.
- dtype=float changes the output data type to floating point.
- Decimal value 2.5 is retained.
Specifying a Different Shape
Shape parameter can override the shape of the input array.
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.full_like(arr, 9, shape=(2, 2))
print(result)
Output
[[9 9] [9 9]]
Explanation:
- Input array has shape (4,).
- shape=(2, 2) changes the output dimensions.
- Four elements are arranged into 2 rows and 2 columns.
Difference Between numpy.full() and numpy.full_like()
Main difference is how the shape of the output array is specified. numpy.full() takes the required shape directly.
import numpy as np
result = np.full((2, 3), 8)
print("Using np.full():", result)
arr = np.array([[1, 2, 3], [4, 5, 6]])
result_like = np.full_like(arr, 8)
print("Using np.full_like():", result_like)
Output
Using np.full(): [[8 8 8] [8 8 8]] Using np.full_like(): [[8 8 8] [8 8 8]]
numpy.full_like() vs numpy.zeros_like()
Both functions create an array based on another array. The difference is the value used to initialize the elements.
- numpy.zeros_like() fills the array with zeros.
- numpy.full_like() fills the array with a specified value.
import numpy as np
arr = np.array([[1, 2],
[3, 4]])
print(np.zeros_like(arr))
print(np.full_like(arr, 6))
Output
[[0 0] [0 0]] [[6 6] [6 6]]