Skip to content

Instantly share code, notes, and snippets.

Working from home

Shengjia Yan yanshengjia

Working from home
Block or report user

Report or block yanshengjia

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
yanshengjia /
Created Dec 26, 2017
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
You can’t perform that action at this time.