Nlp c1 w 4

When creating a post, please add:

  • 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)

Hi @Sudipta_Mukherjee1

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

Cheers

now it is showing name error please help

Please help anyone

@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).

Cheers

now i get different result

i am not getting the result

{moderator edit - solution code removed}

Hi @Sudipta_Mukherjee1

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.

But i changed the last line still i get different result

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”?

Try this and watch what happens:

myList = list(range(8))
print(myList)
print(myList[0:3])

[quote=“arvyzukai, post:5, topic:552345”]
sorted_ids = sorted_ids[::-1]
[/quote]nothinh happened sir

{mentor edit: notebook removed}

I answered that question above.

Please do not share your notebook on the forum. That’s not allowed by the Code of Conduct.

performing those operations i get different result