Skip to content

Commit fdba091

Browse files
committed
upload similarity.py
1 parent 1dc5d4a commit fdba091

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

utils/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- data_util.py用于加载MNIST数据集
44
- test.py 使用KNN算法测试MNIST数据集
5+
- similarity.py 用于计算相似度的方法
56

67
## 依赖
78

utils/similarity.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sun Feb 28 19:38:58 2016
4+
similarity
5+
@author: liudiwei
6+
"""
7+
8+
import numpy as np
9+
10+
#欧几里得距离,使用1/(1+distance)归一化
11+
def eulisSim(matA, matB):
12+
return 1.0/(1.0 + np.linalg.norm(matA - matB))
13+
14+
#pearson相关系数,使用0.5+0.5*corrcof()将数值归一化
15+
def pearsSim(matA, matB):
16+
if len(matA) < 3:
17+
return 1.0
18+
return 0.5 + 0.5 * np.corrcoef(matA, matB, rowvar = 0)[0][1]
19+
20+
#余弦相似度,使用0.5+0.5*cos(theta)归一化
21+
def cosSim(matA, matB):
22+
val1 = float(matA * matB.T)
23+
val2 = np.linalg.norm(matA) * np.linalg.norm(matB)
24+
cos_theta = val1/val2
25+
return 0.5 + 0.5 * cos_theta
26+
27+
if __name__=="__main__":
28+
matA = np.mat([1,2,3,4,5])
29+
matB = np.mat([2,3,4,5,6])
30+
edist = eulisSim(matA, matB) #output: 0.3090169943749474
31+
pdist = pearsSim(matA, matB) #output: 1.0
32+
cdist = cosSim(matA, matB) #output: 0.99746833816309111

0 commit comments

Comments
 (0)