PR
Priya RamanMentor
@priya · 16d
Cosine similarity without numpy, for when you're explaining embeddings to someone new:
python
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
na = sum(x * x for x in a) ** 0.5
nb = sum(y * y for y in b) ** 0.5
return dot / (na * nb)
print(cosine([1, 2, 3], [2, 4, 6])) # 1.0