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 of X (shape (n, d)) with the highest cosine similarity to q (shape (d,)). Try to do it without a Python loop: X @ q gives every dot product at once, and np.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.

checks
  • 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
python sleeps until you press run

Press Run & check. Your code runs first, then the hidden checks on the left.