Skip to content

Instantly share code, notes, and snippets.

@marcelcaraciolo
Created August 21, 2012 19:10
cosine
def cosine(dot_product, rating_norm_squared, rating2_norm_squared):
'''
The cosine between two vectors A, B
dotProduct(A, B) / (norm(A) * norm(B))
'''
numerator = dot_product
denominator = rating_norm_squared * rating2_norm_squared
return (numerator / (float(denominator))) if denominator else 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment