Skip to content

Instantly share code, notes, and snippets.

@otknoy
Last active December 1, 2016 08:32
Show Gist options
  • Save otknoy/1927a29f642593861afc to your computer and use it in GitHub Desktop.
Save otknoy/1927a29f642593861afc to your computer and use it in GitHub Desktop.
Python + scipy で cos 類似度の計算
#!/usr/bin/env python
import numpy as np
import scipy.spatial.distance
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 1 - scipy.spatial.distance.cosine(x, y)
print 1 - scipy.spatial.distance.cosine(y, z)
print 1 - scipy.spatial.distance.cosine(z, x)
@otknoy
Copy link
Author

otknoy commented Sep 2, 2014

距離だから、1 から引いてやれば類似度として使える。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment