协同过滤中皮尔逊相关系数python实现代码

协同过滤中皮尔逊相关系数python实现代码_第1张图片

评分矩阵如下表所示
协同过滤中皮尔逊相关系数python实现代码_第2张图片

from  math  import  sqrt
 
def  multipl(a,b):
	sumofab = 0.0
	for  i  in  range ( len (a)):
	 temp = a[i] * b[i]
	 sumofab += temp
	return  sumofab
 
def  corrcoef(x,y):
	n = len (x)
	#求和
	sum1 = sum (x)
	sum2 = sum (y)
	#求乘积之和
	sumofxy = multipl(x,y)
	#求平方和
	sumofx2  =  sum ([ pow (i, 2 )  for  i  in  x])
	sumofy2  =  sum ([ pow (j, 2 )  for  j  in  y])
	num = sumofxy - ( float (sum1) * float (sum2) / n)
	#计算皮尔逊相关系数
	den = sqrt((sumofx2 - float (sum1 ** 2 ) / n) * (sumofy2 - float (sum2 ** 2 ) / n))
	return  num / den

a = [3.3, 6.5, 2.8, 3.4, 5.5]
b = [3.5, 5.8, 3.1, 3.6, 5.1]
c = [5.6, 3.3, 4.5, 5.2, 3.2]
d = [5.4, 2.8, 4.1, 4.9, 2.8]
e = [5.2, 3.1, 4.7, 5.3, 3.1]
 
print(corrcoef(a,b))
print(corrcoef(a,c))
print(corrcoef(a,d))
print(corrcoef(a,e))
print(corrcoef(b,c))
print(corrcoef(b,d))
print(corrcoef(b,e))
print(corrcoef(c,d))
print(corrcoef(c,e))
print(corrcoef(d,e))

结果为:

             相关系数
    
    用户A&B   0.9997735176536731
    用户A&C  -0.8477583108627492
    用户A&D  -0.8418164002035229
    用户A&E  -0.9152367092225393
    用户B&C  -0.8417411591004959
    用户B&D  -0.835319954072308
    用户B&E  -0.9099753379926111
    用户C&D   0.9989872824995625
    用户C&E   0.9762719892992101
    用户D&E   0.9697821610909036

你可能感兴趣的:(python)