Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I check that two slices of numpy arrays are the same (or overlapping)?

Tags:

python

numpy

I would like to check if two ndarrays are overlapping views of the same underlying ndarray.

To check that two slices are exactly the same, I can do something like:

a.base is b.base and a.shape == b.shape and a.data == b.data

The comparison of buffers seemed to work in one simple case -- can anyone tell me if it works in general?

Unfortunately, this wont work for overlapping slices, and I haven't figured out how to extract from the buffer exactly what its offset is in the underlying data -- perhaps someone can help me with this?

Also, say a and b are slices of x, and c is a slice of b. As the underlying data is the same, I would also like to detect overlaps between c and a. It would seem that I should be able to get away with comparing just buffer and shape... if anyone could tell me exactly how, I would be grateful.

like image 874
shaunc Avatar asked May 25 '12 02:05

shaunc


People also ask

How do you check if two NumPy arrays are the same?

To check if two NumPy arrays A and B are equal: Use a comparison operator (==) to form a comparison array. Check if all the elements in the comparison array are True.

How do I compare two NumPy arrays in Python?

Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.

Can you slice NumPy arrays?

Slicing 1-Dimensional NumPy ArraysUsing slicing operation we can extract elements of a 1-D NumPy array. For example, arr[1:6] syntax to slice elements from index 1 to index 6 from the following 1-D array.

How do you check that NumPy array owns the data or not?

Check if Array Owns its DataEvery NumPy array has the attribute base that returns None if the array owns the data. Otherwise, the base attribute refers to the original object.


2 Answers

numpy.may_share_memory() is the best heuristic that we have at the moment. It is conservatively heuristic; it may give you false positives, but it will not give you false negatives. I think there might be ways to improve the heuristic to be 100% correct. If they pan out, they will be folded into that function, so that's the best way forward.

like image 147
Robert Kern Avatar answered Sep 18 '22 13:09

Robert Kern


It might be possible to compare where the indices live in memory using the ctypes property of the arrays. It might take some work, so you might want to step back and see if there is a different way of solving your problem.

like image 30
Ken Avatar answered Sep 18 '22 13:09

Ken