NLP C1 W 4 value error

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) I am getting reverse value
1 Like

The problems is most probably with your implementation of nearest_neighbor, go back and recheck it!

1 Like

Of course the correct answer is:

[[2 0 1]
 [1 0 5]
 [9 9 9]]

In the comments in the template code, it told you to do this:

# Reverse the order of the sorted_ids array

If you totally omit that operation in the sequence, then this is the incorrect answer you get:

[[-2  5  3]
 [ 6 -9  5]
 [ 9  9  9]]

Which is different than the wrong answer you got. Interesting. I haven’t managed to come up with a theory for what went wrong to get the answer you show. I will try to put some instrumentation in my code to see if that provides any clues.

1 Like

Ok, here’s what I get with added print statements:

candidates
[[ 1  0  5]
 [-2  5  3]
 [ 2  0  1]
 [ 6 -9  5]
 [ 9  9  9]]
unsorted similarity [0.8320502943378437, 0.11470786693528087, 0.9486832980505138, 0.6527299120066193, 0.8164965809277259]
sorted_ids [1 3 4 0 2]
reversed sorted_ids [2 0 4 3 1]
[[2 0 1]
 [1 0 5]
 [9 9 9]]

The point of the sort is that it sorts from smaller to larger and that’s why we need to reverse the list of course. Then at the very last step, all you need to do is to “index off” the first k elements of that reversed list in their existing order. What is the syntax for doing that?

So what do you see if you add those print statements? Where does your implementation go off the rails?

1 Like

i got this now please help sir i am stuck here

Arvydas gave you the answer on the public thread. This line is wrong:

k_idx = sorted_ids[-k:]

A negative index in python counts backwards from the end of the list, so the index -k gives you the last k elements of the list. But the whole point is that you want the first k values, right? So how would you express that?

i changed the last line still get different result

please help me sir

I answered this on the public version of this thread already, but you want the first k elements of the list, right? What you said in your current code is to start at k. How would you say “start at 0 and end at k”? Here’s the example I gave in that other thread:

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

Try running that code and watch what happens.

performing those operation i get different results

{moderator edit - solution code removed}

My code is just a sample to demonstrate the technique of indexing off the first 3 elements of the list. You need to adapt that technique to extract the first k elements of your sorted_ids.

What you are doing is this:

k_idx = sorted_ids[k:]

As I pointed out earlier, what that does is starts at k and takes the rest of the list. That is different than starting at the beginning and stopping at k, right?

Here is some sample code. This is not a solution to your problem. This is just demonstrating the idea of how indexing works. Here’s an example list:

aList = list(range(12))
print(f"aList = {aList}")
aList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Ok, we have a list with 12 elements. Now we index it the way you did:

print(f"aList[4:] = {aList[4:]}")
aList[4:] = [4, 5, 6, 7, 8, 9, 10, 11]

So that does what I said above: it starts at index 4 and gives you the rest of the list. But that’s not what you want in this case, right?

Now watch this:

print(f"aList[0:4] = {aList[0:4]}")
aList[0:4] = [0, 1, 2, 3]

Do you see what that did? It gave us the first 4 elements of the list, right? So which method do you need to apply in this case?

sir thanks i passed this assignment and understand the logic thank you so much for guidence