Reputation: 1533
I'm writing a k nearest neighbor classifier in Numpy and faiss (Facebook kNN library). For point classification, I receive:
[[ 9240 4189 8702]
[ 2639 1052 13565]
[10464 14220 13980]
...
[12014 12063 1331]
[ 6719 5832 8827]
[ 1793 5455 12328]]
Each row is an index of the y
vector. I need to reference the values for this matrix in the y
vector, so e. g. I will swap 9240
in the matrix for y[9240]
value, for example 1 (positive class).
Can I do that without Python loop, i. e. can this be done with Numpy only?
Upvotes: 0
Views: 72
Reputation: 13079
You can index a 1-d array with an array of indices. The result is the same shape as the array of indices. For example:
>>> a
array([[1, 2, 2],
[3, 3, 2]])
>>> y
array([ 1000., 1020., 1040., 1060.])
>>> y[a]
array([[ 1020., 1040., 1040.],
[ 1060., 1060., 1040.]])
Upvotes: 1