0

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?

4
  • Is y a 1d vector? Commented Sep 7, 2020 at 13:07
  • 1
    It sounds like you just want y[your_array] Commented Sep 7, 2020 at 13:08
  • @alani Yeah, that's it! For some reason I thought it would not work for multicolumn X... please post an answer so I can accept. Commented Sep 7, 2020 at 13:09
  • 1
    @qalis Okay, have done. Commented Sep 7, 2020 at 13:12

1 Answer 1

1

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.]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.