w3resource

NumPy array creation: empty_like() function

empty_like() function

The empty_like() function is used to create a new array with the same shape and type as a given array.

Syntax:

numpy.empty_like(prototype, dtype=None, order='K', subok=True)
NumPy array: empty_like() function

Version: 1.15.0

Parameter:

Name Description Required /
Optional
prototype The shape and data-type of prototype define these same attributes of the returned array. Required
dtype Overrides the data type of the result. optional
order Overrides the memory layout of the result. ‘C’ means Corder, ‘F’ means F-order, ‘A’ means ‘F’ if prototype is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of prototype as closely as possible. New in version 1.6.0. optional
subok If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. optional

Return value:

[ndarray] Array of uninitialized (arbitrary) data with the same shape and type as prototype

Example: NumPy.empty_like()

>>> import numpy as np
>>> a = ([1,2,3], [4,5,6])
>>> np.empty_like(a)
array([[139706697202600,        28351168, 139706713266976],
       [139706682360248, 139706669155640, 139706713256944]])
>>>
>>> a = np.array([[1., 2., 3.],[4.,5.,6.]])
>>> np.empty_like(a)
array([[  6.92340539e-310,   9.95630813e-317,   6.92340619e-310],
       [  6.92340619e-310,   1.66406600e-287,   9.67127608e-292]])
>>> 

Pictorial Presentation:

NumPy array: empty_like() function
NumPy array: empty_like() function

Example: NumPy.empty_like() where dtype is int

>>> import numpy as np
>>> a = np.empty_like([2, 2], dtype = int)
>>> print(a)
[139942630210440 139942630210440]
>>> 
>>> a = np.empty_like([2, 2], dtype = float)
>>> print(a)
[  6.93999521e-310   6.93999521e-310]

Example: NumPy.empty_like() where order is 'C' or 'F'

>>> import numpy as np
>>> a = np.empty_like([2, 2], dtype = int, order='C')
>>> print(a)
[140209094482824 140209094482824]
>>> a = np.empty_like([2, 2], dtype = int, order='F')
>>> print(a)
[0 0]
>>> 

Example: NumPy.empty_like() using subok parameter (True/False)

>>> import numpy as np
>>> a = np.empty_like([2, 2], dtype = int, order='C', subok=True)
>>> print(a)
[0 0]
>>> a = np.empty_like([2, 2], dtype = int, order='C', subok=False)
>>> print(a)
[0 0]
>>> 

Python - NumPy Code Editor:

Previous: empty()
Next: eye()