NumPy empty_like(): Allocating Uninitialized Arrays from Prototype Geometries

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().

ParameterDescription
prototypeInput array or object whose shape, data-type, and layout attributes are mirrored
dtypeData-type of the returned array; overrides the prototype dtype if specified
orderMemory layout specification: 'C' (row-major), 'F' (column-major), 'A' (aligned), or 'K' (keep prototype layout)
subokIf True, the newly created array inherits subclass attributes of the prototype
shapeOverride shape tuple (optional); replaces the prototype shape while retaining prototype dtype attributes

Syntax and Parameters 🔝

numpy.empty_like(prototype, dtype=None, order='K', subok=True, shape=None)

Legacy vs Modernized Code Comparison 🔝

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.

Basic Prototype Cloning 🔝

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

Overriding dtype 🔝

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: 

Overriding Shape Geometries 🔝

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)

Crucial Uninitialized Memory Warning 🔝

Practice in Google Colab 🔝

Test prototype cloning, dtype overrides, shape adjustments, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related array creation functions: empty(), zeros(), ones(), full(), eye(), and linspace().




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer