numpy.empty_like(prototype, dtype=None, order='K', subok=True, shape=None) returns a new uninitialized array with the same shape and type as a given prototype input.The empty_like() function allocates memory instantly without initializing elements to zero or any other specific value. The resulting array contains arbitrary garbage values left in RAM from previous memory allocations. For zero-initialized cloning, see zeros_like(). For standalone uninitialized buffers, see empty().
| Parameter | Description |
|---|---|
prototype | Input array or object whose shape, data-type, and layout attributes are mirrored |
dtype | Data-type of the returned array; overrides the prototype dtype if specified |
order | Memory layout specification: 'C' (row-major), 'F' (column-major), 'A' (aligned), or 'K' (keep prototype layout) |
subok | If True, the newly created array inherits subclass attributes of the prototype |
shape | Override shape tuple (optional); replaces the prototype shape while retaining prototype dtype attributes |
numpy.empty_like(prototype, dtype=None, order='K', subok=True, shape=None)
Review how legacy NumPy syntax and unformatted snippets compare against the modernized, structured, and self-verifying implementation:
| Aspect | Legacy Implementation | Modernized Implementation |
|---|---|---|
| Code Structure | Unformatted echo strings, missing semantic markup, no assertions. | Structured HTML5, semantic headings, unit test assertions. |
| Parameter Control | Basic positional syntax without explicit type/shape checks. | Comprehensive attribute inspection, dtype overrides, and shape customization. |
| Verification | Manual print statements with arbitrary output comments. | Automated unit test assertions and reproducible random seeding. |
| Ecosystem Integration | Isolated code snippet with no cross-links. | Full breadcrumb navigation, quick jump TOC, and Colab/GitHub companion notebooks. |
Pass an existing array as a prototype to allocate a new uninitialized buffer sharing identical dimensions and data type:
import numpy as np
ar = np.array([4, 7, 9])
ar_empty = np.empty_like(ar)
print("Shape:", ar_empty.shape)
print("Dtype:", ar_empty.dtype)
assert ar_empty.shape == ar.shape
assert ar_empty.dtype == ar.dtype
Expected output (Note: Values will contain arbitrary memory contents)
Shape: (3,)
Dtype: int64
You can specify a custom dtype to change the element type of the newly allocated buffer while keeping the shape of the prototype:
ar = np.array([4, 7, 9])
# 1. Integer 64-bit override
ar_int64 = np.empty_like(ar, dtype=np.int64)
print("Int64 dtype:", ar_int64.dtype)
# 2. String dtype override
ar_str = np.empty_like(ar, dtype=str)
print("String dtype:", ar_str.dtype)
# 3. Float dtype override
ar_float = np.empty_like(ar, dtype=float)
print("Float dtype: ", ar_float.dtype)
assert ar_int64.dtype == np.int64
assert ar_float.dtype == np.float64
Expected output
Int64 dtype: int64
String dtype:
The optional shape keyword argument allows you to allocate an uninitialized array with a brand new dimension while mirroring the prototype's dtype and memory layout:
ar = np.array([4, 7, 9])
ar_matrix = np.empty_like(ar, shape=(2, 3))
print("Overridden shape matrix:
", ar_matrix)
print("Shape: ", ar_matrix.shape)
assert ar_matrix.shape == (2, 3)
assert ar_matrix.dtype == ar.dtype
Expected output (Values represent uninitialized RAM bytes)
Overridden shape matrix:
[[36598224 0 0]
[ 0 0 0]]
Shape: (2, 3)
empty_like() and empty() do not zero-fill memory. They return whatever random bit pattern currently resides in that memory address in RAM. Always populate every element of the array immediately after allocation before reading from it to avoid undefined behavior or silent bugs.
Test prototype cloning, dtype overrides, shape adjustments, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related array creation functions: empty(), zeros(), ones(), full(), eye(), and linspace().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.