Week # must be added in the tags option of the post.
Link to the classroom item you are referring to:
Description (include relevant info but please do not post solution code or your entire notebook) GET reverse value when finding nearest neighbour
(Solution code removed, as posting it publicly is against the honour code of this community, regardless if it is correct or not. You can share the errors you get)
Reversing the array can be achieved with: reversed_arr = arr[::-1]
So you deleted one line (or it got missing):
# sort the similarity list and get the indices of the sorted list
sorted_ids = None
# Reverse the order of the sorted_ids array
sorted_ids = None
# get the indices of the k most similar candidate vectors
k_idx = None
@Sudipta_Mukherjee1 don’t get me wrong, because I don’t want to be rude, but from what I see, I think that you lack basic Python/programming knowledge and this specialization (especially later courses) could be too soon too hard for you.
The line I showed you (reversed_arr = arr[::-1]) has different variable names - in your case, instead of arr you would use sorted_ids. Also you did double assignment (two = in one line is not what would someone with basic programming knowledge would have done; in this case, you don’t need reversed_arr because sorted_ids stands for it). Or put it simply: sorted_ids = sorted_ids[::-1]
is what you’re looking for (it reverses the order of sorted_ids).
Almost there:
Your last line is wrongly indexed, it should be sorted_ids[:k] (since you want up to k top nearest neighbors, while your current slice gives you last k neighbors)
Cheers
p.s. Please remove your last image, since it’s against the code of conduct.
That just reverses the list again, which is not what you want. You want to “peel off” the first k elements of the list, right? How do you give an index expression that says “start at 0 and end at k”?