>>>import numpy
>>>vec1=[[1,1,1],[2,2,2]]
>>>vec2=[[2,2,2],[1,1,1]]
>>>vec1=numpy.array(vec1)
>>>vec2=numpy.array(vec2)
>>>vec1
array([[1, 1, 1],
[2, 2, 2]])
>>>vec2
array([[2, 2, 2],
[1, 1, 1]])
>>>dist = numpy.sqrt(numpy.sum(numpy.square(vec1 - vec2)))
>>>dist
2.4494897427831779
>>>numpy.linalg.norm(vec1-vec2)
2.4494897427831779
余弦相似度:
>>>vec1
array([[1, 1, 1],
[2, 2, 2]])
>>>vec2
array([[2, 2, 2],
[1, 1, 1]])
>>>num=float(numpy.sum(vec1*vec2))
>>>num
12.0
>>>denom=numpy.linalg.norm(vec1)*numpy.linalg.norm(vec2)
>>>cos=num/denom