{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/7784","repository_url":"https://api.github.com/repos/numpy/numpy","labels_url":"https://api.github.com/repos/numpy/numpy/issues/7784/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/7784/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/7784/events","html_url":"https://github.com/numpy/numpy/issues/7784","id":162808627,"number":7784,"title":"np.in1d support tolerance parameter for float comparisons","user":{"login":"saullocastro","id":3889819,"avatar_url":"https://avatars.githubusercontent.com/u/3889819?v=3","gravatar_id":"","url":"https://api.github.com/users/saullocastro","html_url":"https://github.com/saullocastro","followers_url":"https://api.github.com/users/saullocastro/followers","following_url":"https://api.github.com/users/saullocastro/following{/other_user}","gists_url":"https://api.github.com/users/saullocastro/gists{/gist_id}","starred_url":"https://api.github.com/users/saullocastro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saullocastro/subscriptions","organizations_url":"https://api.github.com/users/saullocastro/orgs","repos_url":"https://api.github.com/users/saullocastro/repos","events_url":"https://api.github.com/users/saullocastro/events{/privacy}","received_events_url":"https://api.github.com/users/saullocastro/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":11,"created_at":"2016-06-28T23:16:00Z","updated_at":"2016-07-01T06:52:32Z","closed_at":null,"body":"I would like to add a tolerance parameter to `np.in1d` to support robust float comparisons.\r\n\r\nOriginal function:\r\nhttps://github.com/numpy/numpy/blob/master/numpy/lib/arraysetops.py#L305\r\n\r\nHere is one example that would require an error-tolerant comparison:\r\n\r\n    a = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 ], dtype=np.float32)\r\n    b = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 ], dtype=np.float64)\r\n    print(np.in1d(a.astype(np.float64), b))\r\n    #array([False, False, False, False,  True, False, False], dtype=bool)\r\n\r\nWhat do you guys think of this suggestion?"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/229868589","html_url":"https://github.com/numpy/numpy/issues/7784#issuecomment-229868589","issue_url":"https://api.github.com/repos/numpy/numpy/issues/7784","id":229868589,"user":{"login":"jaimefrio","id":3343990,"avatar_url":"https://avatars.githubusercontent.com/u/3343990?v=3","gravatar_id":"","url":"https://api.github.com/users/jaimefrio","html_url":"https://github.com/jaimefrio","followers_url":"https://api.github.com/users/jaimefrio/followers","following_url":"https://api.github.com/users/jaimefrio/following{/other_user}","gists_url":"https://api.github.com/users/jaimefrio/gists{/gist_id}","starred_url":"https://api.github.com/users/jaimefrio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jaimefrio/subscriptions","organizations_url":"https://api.github.com/users/jaimefrio/orgs","repos_url":"https://api.github.com/users/jaimefrio/repos","events_url":"https://api.github.com/users/jaimefrio/events{/privacy}","received_events_url":"https://api.github.com/users/jaimefrio/received_events","type":"User","site_admin":false},"created_at":"2016-07-01T06:52:32Z","updated_at":"2016-07-01T06:52:32Z","body":"This seems to work:\r\n\r\n```Python\r\nimport numpy as np\r\n\r\ndef in1d(a, b, tol=1e-6):\r\n    a = np.unique(a)\r\n    intervals = np.empty(2*a.size, float)\r\n    intervals[::2] = a - tol\r\n    intervals[1::2] = a + tol\r\n    overlaps = intervals[:-1] >= intervals[1:]\r\n    overlaps[1:] = overlaps[1:] | overlaps[:-1]\r\n    keep = np.concatenate((~overlaps, [True]))\r\n    intervals = intervals[keep]\r\n    return np.searchsorted(intervals, b, side='right') & 1 == 1\r\n\r\na = np.array([1, 1.1, 1.3, 1.6, 2.0, 2.5, 3, 3.4, 3.8])\r\nb = np.linspace(0, 10, 101)\r\nc = (((b >= 0.8) & (b < 2.2)) |\r\n     ((b >= 2.3) & (b < 2.7)) |\r\n     ((b >= 2.8) & (b < 4.0)))\r\nassert np.all(in1d(a, b, tol=0.2) == c)\r\n```\r\n\r\nIt defines inclusion as being in `[a[i] - tol, a[i] + tol)`, to get the interval to be closed (or open) on both ends you would need to either make two calls to `np.searchsorted` with different `side` kwargs or do something (untested!!!) like:\r\n\r\n```Python\r\nindices = np.searchsorted(intervals, b)\r\nret = indices & 1 == 1\r\nmissed_ret = b[~ret] == a[indices[~ret]]\r\nret[~ret] |= missed_ret\r\nreturn ret\r\n```\r\n"}}
{"actor":{"display_login":"jaimefrio"}}