BetterDocs
Home
Docs

Creation | np.full_like()

Method:

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

Creates an array filled with fill_value and same shape as a.

Returns:

np.ndarray

Parameters:

a: array-like-

The reference array whose shape and type will be used.

import numpy as np

a = np.array([[1, 2], [3, 4]])
arr = np.full_like(a=a, fill_value=20)
print(arr)
'''
Output:
[[20 20]
 [20 20]]
'''

fill_value: (array-like, int, float, bool, str)-

Value to fill the array with.

import numpy as np

a = np.array([[1, 2], [3, 4]])
full_arr = np.full_like(a=a, fill_value=[1, 0])
print(full_arr)
'''
Output:
[[1 0]
 [1 0]]
'''

When passing array-like, the shape of must be less than or equal to that of your shape of a.

dtype: data-type, Optional-

Specifies the data-type of the array elements. If not provided, it’s inferred from a.

import numpy as np

a = np.array([[1, 2], [3, 4]])
full_arr = np.full_like(a=a, fill_value=0, dtype=np.float32)
print(full_arr)
'''
Output:
[[0. 0.]
 [0. 0.]]
'''

Values: +

order: {'C', 'F', 'K', 'A'}, Optional-

Specifies the memory layout order for multi-dimensional arrays.

order = K (default) +

order = C +

order = F +

order = A +

By applying any of the above mentioned orders, your answer will never change as you will be ordering an all-zeros matrix.

.ravel()  is a NumPy function used to flatten a multi-dimensional array into a 1D array.

subok: (True or False), Optional-

It controls whether the output array will retain the subclass type of the input array (if it’s a subclass of np.ndarray) or be cast to a base np.ndarray.

subok = True (default) +

subok = False +

In NumPy, the .view()  method creates a new "view" of an existing array with a different data type or subclass without copying the data.

This means the new array points to the same data as the original array but can be accessed or interpreted differently.

shape: shape, Optional-

Overrides the shape of the a array.

import numpy as np

a = np.full_like([[1, 2], [3, 4]], fill_value=[0, 8], shape=(3,2))
print(a)
'''
Output:
[[0 8]
 [0 8]
 [0 8]]
'''

device: None, Optional-

In NumPy 2.1, the device parameter in np.full_like() is still in an early stage and currently only accepts "cpu" as a valid option. At this time, it is primarily there for future development, allowing the function to eventually support GPUs or other devices, but for now, it doesn't support GPU IDs directly.


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy