Skip to content

Instantly share code, notes, and snippets.

@otknoy
Last active September 29, 2017 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otknoy/c0adcf8860edf0d28dd2 to your computer and use it in GitHub Desktop.
Save otknoy/c0adcf8860edf0d28dd2 to your computer and use it in GitHub Desktop.
Python + numpy で cos 類似度の計算
#!/usr/bin/env python
import numpy as np
def cos_sim(v1, v2):
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
if __name__ == '__main__':
x = np.array([1, 1, 1, 1, 1])
y = np.array([1, 0, 1, 0, 1])
z = np.array([0, 1, 0, 0, 0])
print cos_sim(x, y)
print cos_sim(y, z)
print cos_sim(z, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment