0

For a numpy array of shape (n, 2), n points with 2 coordinates, what is the best way (fastest for large n) to test if a particular point is already in the array. E.g.

test_point = np.array([1, 2])
array_A = array([[1, 3], [2, 2], [2, 1]]) # test on this should return False
array_B = array([[1, 2], [2, 2], [2, 1]]) # test on this should return True
fales
  • 89
  • 7
  • 1
    Do you actually have to use ``numpy`` for this? For direct or random access (which do not really benefit from vectorisation and locality), Python's builtin types are very powerful – a ``set`` of ``tuple``s should do this efficiently. – MisterMiyagi Oct 23 '21 at 10:22
  • @MisterMiyagi Not necessarily, however, I want the output to be of this form and the points are given as numpy arrays. – fales Oct 23 '21 at 11:10
  • @Ali_Sh Thank you, this pretty much what I have been looking for! – fales Oct 23 '21 at 11:13
  • @fales your welcome. Besides using just python and numpy, there are other libraries such as [numba](https://colab.research.google.com/drive/1ieMvWKKY3-NkTovu9iVab7h5XnAVnCiU?usp=sharing), which could result in more faster runs. – Ali_Sh Oct 23 '21 at 11:45

2 Answers2

1
(array_A == test_point).all(axis=1).any()

Explanation:

array_A == test_point applies broadcasting rules and returns array of booleans with shape equal to array_A.
.all(axis=1) returns True for rows if all values are True
.any() returns True if any value in column is True

Alexander Volkovsky
  • 2,588
  • 7
  • 13
-1

Kindly verify if this is what you were anticipating:

import numpy as np
test = np.array([1,2])
sample = np.array([[1, 2], [2, 2]])
result = any([all(sample[i,:] == test) for i in range(sample.shape[0])])