python中余弦算法的伪代码

时间:2019-03-17 19:19:46

标签: python python-2.7

我想为此算法编写伪代码,有人可以帮忙吗?

def get_cosine(vec1, vec2):
 intersection = set(vec1.keys()) & set(vec2.keys())
 numerator = sum([vec1[x] * vec2[x] for x in intersection])

 sum1 = sum([vec1[x]**2 for x in vec1.keys()])
 sum2 = sum([vec2[x]**2 for x in vec2.keys()])
 denominator = math.sqrt(sum1) * math.sqrt(sum2)

 if not denominator:
    return 0.0
 else:
    return float(numerator) / denominator

0 个答案:

没有答案