Reputation: 2109
How can I compare whether two numpy arrays are exactly identical in memory, so that e.g.
np.array([0,1]) == np.array([0,1])
is True, but
np.array([0,1]) == np.array([[0,1]])
np.array([0,1], dtype=np.int32) == np.array([0,1], dtype=np.int64)
are both False. np.array_equal
doesn't have a compare_dtypes
option. I guess there might be other ways for the memory representation of an array to differ too (e.g. endian-ness)
Upvotes: 2
Views: 451
Reputation: 2109
Currently I'm using pickle.dumps
:
import pickle
a1 = np.array([0,1], dtype=np.int32)
a2 = np.array([0,1], dtype=np.int64)
pickle.dumps(a1) == pickle.dumps(a2)
But it seems a bit of a hack.
Upvotes: 0
Reputation: 53079
Depending on which aspects you want to cover the minimum would be comparing x.dtype
(this includes endianness) x.shape
and x.strides
.
You may also want to look at some flags. For example, x.flags.aligned
may be considered part of the memory layout in a broad sense as may be x.flags.writeable
(and perhaps x.flags.owndata
).
The C/F_CONTIGUOUS
flags, on the other hand, are redundant once you know shape and strides, and finally, there are UPDATEIFCOPY
and WRITEBACKIFCOPY
which I don't understand well enough to comment on.
Upvotes: 1
Reputation: 88276
You could use itemsize
to compare them in terms of their length in bytes:
a1 = np.array([0,1], dtype=np.int32)
a2 = np.array([0,1], dtype=np.int64)
a1.itemsize == a2.itemsize
# False
If you want to compare both their size and content you could examine the raw contents of data memory with ndarray.tobytes
:
a1.tobytes() == a2.tobytes()
# False
Upvotes: 1