BetterDocs
Home
Docs

Creation | np.empty_like()

Method:

np.empty_like(prototype, dtype=None, order='K', subok=True, shape=None, *, device=None)

Creates a new array with the same shape as the prototype, but without initializing values.

Returns:

np.ndarray

Parameters:

prototype: array-like-

The reference array whose shape and type will be used.

import numpy as np

a = np.array([[1, 2], [3, 4]])
empty_arr = np.empty_like(prototype=a)
print(empty_arr)  # Values are uninitialized
'''
Output: (Garbage Values)
[[    100981028852064                   0]
 [7306090318617649184 8386106492501113458]]
'''

dtype: data-type, Optional-

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

import numpy as np

a = np.array([[1, 2], [3, 4]])
empty_arr = np.empty_like(a, dtype=np.float32)
print(empty_arr)  # Values are uninitialized
'''
Output: (Garbage Values)
[[ 1.28442770e+07 -7.59295479e-38]
 [ 1.06929976e-04  4.41717302e-41]]
'''

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 +

.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 prototype array.

import numpy as np

a = np.empty_like([[1, 2], [3, 4]], shape=(2,1))
print(a)  # Values are uninitialized
'''
Output:
[[4575657222473777152]
 [4575657222473777152]]
'''

device: None, Optional-

In NumPy 2.1, the device parameter in np.empty_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