Reputation: 3999
I'm trying to find the best match of a 1D array from a 2D array.
arr1 = np.array([[1, 1, 1], [2, 2, 2]])
arr2 = np.array([1.1, 1.1, 1.1])
how can I get it to return the best match i.e. row 0 preferably as the index of the row?
Upvotes: 0
Views: 114
Reputation: 35626
Assuming "closest" means smallest absolute difference to, try something like:
idx = np.abs(arr1 - arr2).sum(axis=1).argmin() # 0
Absolute difference np.absolute:
np.abs(arr1 - arr2)
[[0.1 0.1 0.1]
[0.9 0.9 0.9]]
Row-wise total difference sum:
np.abs(arr1 - arr2).sum(axis=1)
[0.3 2.7]
Min-Index argmin:
np.abs(arr1 - arr2).sum(axis=1).argmin()
0
Upvotes: 1