Numpy full_like() Function
In Numpy, full_like() function is used to create a new NumPy array with the same shape and type as an existing array, but filled with a specified value. This function is used when we want to initialize an array with a constant value while keeping the shape and data type of a reference array.
Syntax
Following is the syntax of the Numpy full_like() function −
numpy.full_like(arr, fill_value, dtype=None, order='K', subok=True, shape=None)
Parameters
Following are the parameters of the Numpy full_like() function −
- arr - Array whose shape and type define the shape and type of the output array.
- fill_value - The value to fill the array with.
- dtype - The desired data type for the returned array. If None, the data type of arr is used.
- subok - If True, then the newly created array will use the subclass type of arr.
- shape - Overrides the shape of the result if specified.
- order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
- 'C': C-style row-major order.
- 'F': Fortran-style column-major order.
- 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
- 'K': This is the default value keep the order as close as possible to the input.
Return Values
This function returns an array with the same shape and type as the input array, but filled with the specified value.
Example
Following is a basic example to create a NumPy array filled with a specified value with the same shape as an existing array using Numpy full_like() function −
import numpy as np
Numpy_Array = np.array([[1, 2, 3], [4, 5, 6]])
filled_array = np.full_like(Numpy_Array, fill_value=7)
print("Numpy Array filled with 7 -\n", filled_array)
Output
Following is the output of the above code:
Numpy Array filled with 7 - [[7 7 7] [7 7 7]]
Example : Specifying Data Type
We can specify the data type of the array using the dtype parameter. For example, we can create an array of floats filled with a specified value.
In the following example, the array is filled with the value 3.14 and the data type is set to float32 −
import numpy as np
array_b = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32)
filled_array_float = np.full_like(array_b, fill_value=3.14, dtype=np.float32)
print("Numpy Array filled with 3.14 (Float Data Type) -\n", filled_array_float)
Output
Following is the output of the above code −
Numpy Array filled with 3.14 (Float Data Type) - [[3.14 3.14 3.14] [3.14 3.14 3.14]]
Example : Multi-dimensional Array
The numpy.full_like() function can also be used to create multi-dimensional arrays filled with a specified value, while matching the shape of an existing multi-dimensional array.
In the following example, we have created a 3D array filled with the value 5, using the shape of a 3x3x3 reference array −
import numpy as np
my_array = np.ones((3, 3, 3))
filled_3d_array = np.full_like(my_array, fill_value=5)
print("3D Numpy Array filled with 5 -\n", filled_3d_array)
Output
Following is the output of above code −
3D Numpy Array filled with 5 - [[[5. 5. 5.] [5. 5. 5.] [5. 5. 5.]] [[5. 5. 5.] [5. 5. 5.] [5. 5. 5.]] [[5. 5. 5.] [5. 5. 5.] [5. 5. 5.]]]