5 hidden checks
A tiny search engine: given a query vector, find the row of a dataset that points in the most similar direction. Implement two functions.
cosine_similarity(a, b): the cosine of the angle between two 1-D vectors, as a float.most_similar(X, q): the index of the row ofX(shape(n, d)) with the highest cosine similarity toq(shape(d,)). Try to do it without a Python loop:X @ qgives every dot product at once, andnp.linalg.norm(X, axis=1)gives every row's length.
Press Run & check (or Ctrl+Enter) to run your code and then the hidden checks. Run as often as you like.
- cosine_similarity of parallel vectors is 1not run
- cosine_similarity of perpendicular and opposite vectorsnot run
- cosine_similarity matches a known valuenot run
- most_similar picks the right rownot run
- most_similar works on the iris datanot run