I use below code to create a empty matrix:
import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(x)
y =np.empty_like(x)
print(y)
# I get below data:
[[2097184 2097184 2097184]
[2097184 2097184 2097184]
[2097184 2097184 2097184]
[2097184 2097184 2097184]]
why the 2097184 stand for empty?
CodePudding user response:
It doesn't stand for anything. From the documentation:
This function does not initialize the returned array; to do that use
zeros_like
orones_like
instead. It may be marginally faster than the functions that do set the array values.
So the contents of the array are whatever happens to be in the memory that it used for it. In this case, it was a bunch of 2097184
values. The next time you try it you'll probably get something different.
You use this when you don't care what's in the array, because you're going to overwrite it.
CodePudding user response:
The empty_like
method does not initialize the array (that's why it's very faster than zeros_like
and ones_like
), so the shape of the array is exactly the same as x
, but the values are uninitialized and actually are almost random values from the memory place allocated to the array.