Skip to content

Instantly share code, notes, and snippets.

@dela3499
Created November 6, 2015 00:31
Show Gist options
  • Save dela3499/7856cf4927bec14d0438 to your computer and use it in GitHub Desktop.
Save dela3499/7856cf4927bec14d0438 to your computer and use it in GitHub Desktop.
Cosine distance between two random vectors is normally distributed around 0.75
import numpy as np
mag = np.linalg.norm
r = np.random.random
def cosdist(x,y):
return sum(x * y) / (mag(x) * mag(y))
def repeat(f,n):
return [f() for _ in range(n)]
d = repeat(lambda: cosdist(r(100),r(100)),100000)
plt.hist(d,bins = 100); # cosine distance centered on 0.75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment