Introduction
The numpy.full_like()
function is a part of the NumPy library, a fundamental package for scientific computing in Python. It provides a straightforward method to create a new array with the same shape and type as a given array but filled with a specified value. This tutorial aims to elucidate the use of this function through various examples, showcasing its versatility and efficiency in array manipulations.
The Fundamentals of numpy.full_like()
Before diving into examples, let’s understand what numpy.full_like()
precisely does. The function syntax is as follows:
numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)
Where:
a
– Input array whose shape and dtype you want the new array to mimic.fill_value
– The value to fill the new array with.dtype
– Optional. The desired data-type for the array. Overrides the data type ofa
.order
– Memory layout of the array. ‘C’ for C-style row-major order, ‘F’ for Fortran-style column-major. ‘K’ keeps the same asa
.subok
– Optional. If True, then the newly created array will also be a subclass ofa
ifa
is a subclass of ndarray.shape
– Optional. Specifies a custom shape for the new array.
Example 1: Basic Usage
import numpy as np
# Create an existing array
original_array = np.array([[1, 2], [3, 4]])
# Use full_like to create a new array filled with -1
filled_array = np.full_like(original_array, -1)
print(filled_array)
Output:
[[-1, -1]
[-1, -1]]
This example demonstrates how to use numpy.full_like()
to quickly fill a new array with a specific value, mirroring the shape of an original array.
Example 2: Specifying Data Type
import numpy as np
# Original array
original_array = np.arange(4).reshape((2,2))
# Creating a new float array filled with 0.99
filled_array = np.full_like(original_array, 0.99, dtype=np.float64)
print(filled_array)
Output:
[[0.99 0.99]
[0.99 0.99]]
In this example, we’ve overridden the data type of the original array to create a float array filled with 0.99, demonstrating the dtype
parameter’s functionality.
Example 3: Using custom shape
import numpy as np
# Original array
original_array = np.array([[1, 2, 3], [4, 5, 6]])
# Creating a new array with custom shape filled with '7'
filled_array = np.full_like(original_array, 7, shape=(3, 3))
print(filled_array)
Output:
[[7 7 7]
[7 7 7]
[7 7 7]]
This example showcases the use of the shape
parameter to create a new array with a custom shape, different from the original array, but filled with a specified value.
Example 4: Preserving subclass
import numpy as np
class MyArray(np.ndarray):
# subclass of ndarray
# Create an instance of MyArray
original_subclass = np.empty((2, 2)).view(MyArray)
# Using full_like while preserving subclass
filled_subclass = np.full_like(original_subclass, 8, subok=True)
print(filled_subclass)
Output:
[[8 8]
[8 8]]
This advanced example illustrates the subok
parameter in action. By setting subok=True
, we ensure that the newly created array also becomes an instance of the MyArray
class, thus preserving the subclass of the original array.
Conclusion
Through these examples, we’ve explored the diverse functionalities of the numpy.full_like()
function. From straightforward fill operations to custom type and shape adjustments, numpy.full_like()
proves to be an indispensable tool for array manipulations in scientific computing with Python.