我有两个类型的字典:的Python:计算两个类型的字典的余弦相似度更快
d1 = {1234: 4, 125: 7, ...}
d2 = {1234: 8, 1288: 5, ...}
http://stardict.sourceforge.net/Dictionaries.php下载的长度为10至40000。变化要计算我使用此功能的余弦相似性:
from scipy.linalg import norm
def simple_cosine_sim(a, b):
if len(b) < len(a):
a, b = b, a
res = 0
for key, a_value in a.iteritems():
res += a_value * b.get(key, 0)
if res == 0:
return 0
try:
res = res/norm(a.values())/norm(b.values())
except ZeroDivisionError:
res = 0
return res
可以更快地计算相似度吗?
UPD:使用Cython +重写代码+速度提高15%。感谢@Davidmh
from scipy.linalg import norm
def fast_cosine_sim(a, b):
if len(b) < len(a):
a, b = b, a
cdef long up, key
cdef int a_value, b_value
up = 0
for key, a_value in a.iteritems():
b_value = b.get(key, 0)
up += a_value * b_value
if up == 0:
return 0
return up/norm(a.values())/norm(b.values())
我已经评论了你用Cython代码,增加了一种替代方法。我希望这有帮助。 – Davidmh