0

Given a 2D numpy array containing a number of points:

import numpy as np

points = np.array([
    [0, 2],
    [1, 3],
    [2, 3],
    [2, 5],
    [3, 4],
    [3, 5],
    [3, 7],
    [4, 7],
    [4, 8],
    [5, 6],
    [5, 9],
    [6, 7],
    [6, 9],
    [7, 8],
    [7, 9],
])

What is most performant way to check if a given point (e.g. [6,8]) is in the array?

For example:

y = []
for i in points:
    y.append(np.array_equal([6, 8], i))

True in y

yields False, as expected.


np.isin([6,8], points) is unacceptable, as it checks the columns independently, yielding array([ True, True]).

Boseong Choi
  • 2,566
  • 9
  • 22
Max
  • 695
  • 5
  • 18
  • Why must it be a Numpy array? Can't you use a set of tuples? – AKX Mar 17 '20 at 07:11
  • @AKX In my implementation, the points are a slice of a larger array, so Numpy is much preferred. Of course, a tuple-based answer would still be helpful. – Max Mar 17 '20 at 07:13

0 Answers0