Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View yanshengjia's full-sized avatar
🏠
Working from home

Shengjia Yan yanshengjia

🏠
Working from home
View GitHub Profile
@yanshengjia
yanshengjia / cossim.py
Created December 26, 2017 18:31
Compute cosine similarity between two vectors
import math
def cosine_similarity(v1, v2):
"compute cosine similarity of v1 to v2: (v1 dot v2)/{||v1||*||v2||)"
sumxx, sumxy, sumyy = 0.0, 0.0, 0.0
for i in range(len(v1)):
x = v1[i]
y = v2[i]
sumxx += x * x
sumyy += y * y
sumxy += x * y