NumPy full_like() function – Return a full array with the same shape and type as a given array

Numpy Full Like

NumPy is a powerful and widely-used library for scientific computing in Python. It provides numerous functions and tools for working with numerical data and arrays, including the full_like() function. This function allows you to create a new array with the same shape and data type as a given array, but filled with a specific value.

This can be useful in a variety of situations, such as when you need to create an array of a certain shape and type to hold placeholder or default values. In this article, we will explore the use and syntax of the full_like() function in detail, and provide examples of how it can be used in practice.

Also read: NumPy full() function

What is full_like() in NumPy?

full_like() is a function in the NumPy library that creates a new array with the same shape and data type as a given array, but filled with a specified value. It takes in two required arguments:

  1. a: the array to use as a template for the shape and data type of the new array.
  2. fill_value: the value to fill the new array with.

Syntax of Numpy full_like()

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

Parameters

  • a: array_like
    • Required
    • These identical features of the returning array are defined by the shape and data type of a.
  • fill_value: array_like
    • Required
    • The value to be initialized the element with of the new array.
  • dtype: data-type
    • Optional
    • Overrides the data type of the result.
  • order: {‘C’, ‘F’, ‘A’, or ‘K’}
    • Optional
    • Changes the result’s memory layout. “C” stands for C-order, “F” for F-order, and “A” stands for “C” unless “a” is Fortran contiguous. “K” denotes a layout that closely resembles a.
  • subok: bool
    • Optional
    • The newly generated array will use the sub-class type of an if True; else, a base-class array will be used. Usually sets to True.
  • shape: int or sequence of integers
    • Optional
    • Changes the result’s form. Will attempt to maintain order if order=’K’ and the number of dimensions remain unchanged; otherwise, order=’C’ is assumed.

Implementation of Numpy full_like()

Make sure to import the NumPy package in your IDE before implementing the function. To do so run the following line of code in your IDE.

import numpy as np

Example 1. Passing only required parameters

x = ([1,2,3],[4,4,6])
print(x)
print("\n")
np.full_like(x, 3)


y = ([1,2,3],
     [4,5,6])
print(y)
print("\n")
np.full_like(y,([9,8,7],[6,5,4]))
Example 1
Example 1

Example 2. Passing other parameters

w = ([1,2,3],[4,5,6],[7,8,9])
print(w)
print("\n")
np.full_like(w, 5, dtype=np.float16, order='C')

z = ([1,2,3],[4,5,6])
print(z)
print("\n")
np.full_like(z, (1+3j), dtype = float, order='F')
Example 2
Example 2

Example 3: Putting it all together with numpy.full_like()

Here’s a more complete example of using the numpy.full_like() method that includes all the different ways you can use it.

import numpy as np

# Create a random array of integers from 0 to 9
a = np.random.randint(10, size=(2, 3, 4))
print(f"Original array: \n{a}")

# Create a new array of zeros with the same shape and data type as a
b = np.full_like(a, 0)
print(f"Array of zeros with same shape and data type as a: \n{b}")

# Create a new array of ones with the same shape and data type as a
c = np.full_like(a, 1, dtype=np.float64)
print(f"Array of ones with same shape and data type as a, but with data type float64: \n{c}")

# Create a new array with the same shape and data type as a, but filled with the maximum value of a's data type
d = np.full_like(a, np.amax(a))
print(f"Array with same shape and data type as a, filled with the maximum value of a's data type: \n{d}")

# Create a new array with the same shape and data type as a, but filled with the minimum value of a's data type
e = np.full_like(a, np.amin(a))
print(f"Array with same shape and data type as a, filled with the minimum value of a's data type: \n{e}")

Output

Original array: 
[[[2 9 7 3]
  [9 8 6 4]
  [1 0 8 9]]

 [[3 9 9 7]
  [0 8 0 3]
  [5 5 4 3]]]
Array of zeros with same shape and data type as a: 
[[[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]

 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]]
Array of ones with same shape and data type as a, but with data type float64: 
[[[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]]
Array with same shape and data type as a, filled with the maximum value of a's data type: 
[[[9 9 9 9]
  [9 9 9 9]
  [9 9 9 9]]

 [[9 9 9 9]
  [9 9 9 9]
  [9 9 9 9]]]
Array with same shape and data type as a, filled with the minimum value of a's data type: 
[[[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]

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

Conclusion

In conclusion, the full_like() function in NumPy is a useful tool for creating new arrays with the same shape and data type as a given array, but filled with a specific value. This can be useful in a variety of situations, such as when you need to create an array of a certain shape and type to hold placeholder or default values. Whether you are working with numerical data in scientific computing or just need to manipulate arrays in your Python scripts, the full_like() function is a valuable addition to your toolkit.

Reference

https://numpy.org/doc/stable/reference/generated/numpy.full_like.html