1 parent 21d49cc commit 6ccc8b3Copy full SHA for 6ccc8b3
1 file changed
src/python/14.SVD/svdRec.py
@@ -0,0 +1,30 @@
1
+# coding=utf-8
2
+def loadExData():
3
+ return[[1,1,1,0,0],
4
+ [2,2,2,0,0],
5
+ [1,1,1,0,0],
6
+ [5,5,5,0,0],
7
+ [1,1,0,2,2],
8
+ [0,0,0,3,3],
9
+ [0,0,0,1,1]]
10
+
11
+from numpy import *
12
+from numpy import linalg as la
13
+# 欧氏距离相似度,假定inA和inB 都是列向量
14
+# 计算向量的第二范式,相当于计算了欧氏距离
15
+def ecludSim(inA,inB):
16
+ return 1.0/(1.0 + la.norm(inA - inB))
17
18
+# pearsSim()函数会检查是否存在3个或更多的点。
19
+# corrcoef直接计算皮尔逊相关系数
20
+def pearsSim(inA,inB):
21
+ # 如果不存在,该函数返回1.0,此时两个向量完全相关。
22
+ if len(inA)< 3 :return 1.0
23
+ return 0.5 + 0.5*corrcoef(inA,inB,rowvar = 0)[0][1]
24
25
+# 计算余弦相似度
26
+def cosSim(inA,inB):
27
+ num = float(inA.T*inB)
28
+ denom = la.norm(inA)*la.norm(inB)
29
+ return 0.5 +0.5*(num/denom)
30
0 commit comments