print("cosine_similarity(france - paris, rome - italy) = ",cosine_similarity(france - paris, rome - italy))
# END SKIP FOR GRADING
# PUBLIC TESTS
def cosine_similarity_test(target):
a = np.random.uniform(-10, 10, 10)
b = np.random.uniform(-10, 10, 10)
c = np.random.uniform(-1, 1, 23)
assert np.isclose(cosine_similarity(a, a), 1), "cosine_similarity(a, a) must be 1"
assert np.isclose(cosine_similarity((c >= 0) * 1, (c < 0) * 1), 0), "cosine_similarity(a, not(a)) must be 0"
assert np.isclose(cosine_similarity(a, -a), -1), "cosine_similarity(a, -a) must be -1"
assert np.isclose(cosine_similarity(a, b), cosine_similarity(a * 2, b * 4)), "cosine_similarity must be scale-independent. You must divide by the product of the norms of each input"
print("\033[92mAll test passed!")
cosine_similarity_test(cosine_similarity)
cosine_similarity(father, mother) = None
cosine_similarity(ball, crocodile) = None
cosine_similarity(france - paris, rome - italy) = None
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-4b2d02f84f9c> in <module>
27 print("\033[92mAll test passed!")
28
---> 29 cosine_similarity_test(cosine_similarity)
<ipython-input-31-4b2d02f84f9c> in cosine_similarity_test(target)
21
22 assert np.isclose(cosine_similarity(a, a), 1), "cosine_similarity(a, a) must be 1"
---> 23 assert np.isclose(cosine_similarity((c >= 0) * 1, (c < 0) * 1), 0), "cosine_similarity(a, not(a)) must be 0"
24 assert np.isclose(cosine_similarity(a, -a), -1), "cosine_similarity(a, -a) must be -1"
25 assert np.isclose(cosine_similarity(a, b), cosine_similarity(a * 2, b * 4)), "cosine_similarity must be scale-independent. You must divide by the product of the norms of each input"
<__array_function__ internals> in isclose(*args, **kwargs)
/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
2255 y = array(y, dtype=dt, copy=False, subok=True)
2256
-> 2257 xfin = isfinite(x)
2258 yfin = isfinite(y)
2259 if all(xfin) and all(yfin):
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
In particular I would like to draw your attention to this line
23 assert np.isclose(cosine_similarity((c >= 0) * 1, (c < 0) * 1), 0), "cosine_similarity(a, not(a)) must be 0"
There is no particular reason that the cosine error needs to be non-zero. Two vectors can be perpendicular, in other words, have a dot product of zero. That is completely possible. It’s unfortunate for optimization purposes because it provides poor feedback. A direction in which to progress probably needs to be chosen at random or something. But it is possible.
Letting you know because it is not letting me pass this unit test, and because this unit test lacks in a logical basis.
Steven