What is the numpy empty_like() Method

Numpy.empty_like() method “returns a new array with the same shape and type as a given array”.

Syntax

``````numpy.empty_like(shape, order, dtype, subok)
``````

Parameters

It takes four parameters, out of which 2 parameters are optional.

1. shape: It represents the number of rows.
2. order: It represents the order in the memory. (C_contiguous or F_contiguous).
3. dtype: It’s the data type, and it is optional and has a float value by default.
4. bool: It checks if we have to create the sub-class of the main array or not.

Return Value

It returns the ndarray of the same shape and size.

Example 1: How does the np.empty_like() method work

``````import numpy as np

data = ([11, 21, 31], [41, 51, 61])
res = np.empty_like(data, dtype=int)
print("\nMatrix a : \n", res)``````

Output

``````Matrix a :
[[0 0 0]
[0 0 0]]``````

Example 2: How to Use the numpy empty_like() method

``````import numpy as np

arr = np.empty_like([4, 4], dtype=int)
print("\nMatrix arr : \n", arr)

mArr = arr = ([12, 23, 43, 33], [46, 15, 61, 1], [3, 4, 6,7],
[66, 31, 35, 73])
print("\nMatrix mArr : \n", np.empty_like(mArr))
``````

Output

``````Matrix arr :
[2854797676290708764 8240043042726748728]

Matrix mArr :
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
``````

That’s it!

This site uses Akismet to reduce spam. Learn how your comment data is processed.