C1_W3_Assignment problem with simularity outputs for get_country function

Hi guys, my get_country function is returning wrong values for similarity (which is calculated by cosine_similarity).

The cosine similarity is very straightforward and appearently correctly implemented, but the values are still wrong. I think it could be something about the data loaded (but it is already onthe notebook).

Any idea on other sources of issues?

1 Like

It is the # UNQ_C3 GRADED FUNCTION: get_country

Expected Output: (Approximately)

(‘Egypt’, 0.7626821)

My output: (‘Egypt’, 0.58848363)

1 Like

I was able to get the expected answer at least up to the last decimal place:

('Egypt', 0.7626822)

I assume you’ve been through the instructions carefully and compared them to your code. The most common mistake on this function is to directly reference the global variable word_embeddings within the body of the function, instead of referencing it through the parameter. But that actually passes the test case in the notebook and only fails when you submit to the grader.

There are a couple of key pieces here: did you test to make sure that the word is not in the “group” that you defined early on? But if you accidentally included everything, you end up with higher numbers, not lower as in your case. So that’s probably not it …

The only other thing I can think of is maybe you didn’t compute the difference vector correctly. Here’s the hint in the comments:

# Remember: King - Man + Woman = None

If you already know that your cosine_similarity function passes the tests, then the rest of this should be pretty straightforward.

2 Likes

Perfect!
I was initially counting the vector as merely the difference between the embeddings of country1 and city1, and directly applying the city2 embeddings in the cosine_similarity calculation. Intuitively, this approach seemed viable, but I think I made some mistakes while manipulating the embeddings.

1 Like

That’s great that you were able to solve it just based on my general theories. Nice work!

1 Like