Last active
September 29, 2017 13:40
-
-
Save otknoy/c0adcf8860edf0d28dd2 to your computer and use it in GitHub Desktop.
Python + numpy で cos 類似度の計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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