1 changed files with 107 additions and 0 deletions
@ -0,0 +1,107 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
""" |
||||
|
Created on Tue Jul 14 08:25:02 2020 |
||||
|
|
||||
|
@author: zhao |
||||
|
""" |
||||
|
|
||||
|
from numpy import * |
||||
|
from numpy import linalg as la |
||||
|
|
||||
|
# 载入数据 (用户-评分矩阵) |
||||
|
# 行为用户, 列为用户评分/周浏览情况, 表示用户对某个政策类型的评分或浏览情况 |
||||
|
def loadExData2(): |
||||
|
return[[0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5], |
||||
|
[0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 3], |
||||
|
[0, 0, 0, 0, 4, 0, 0, 1, 0, 4, 0], |
||||
|
[3, 3, 4, 0, 0, 0, 0, 2, 2, 0, 0], |
||||
|
[5, 4, 5, 0, 0, 0, 0, 5, 5, 0, 0], |
||||
|
[0, 0, 0, 0, 5, 0, 1, 0, 0, 5, 0], |
||||
|
[4, 3, 4, 0, 0, 0, 0, 5, 5, 0, 1], |
||||
|
[0, 0, 0, 4, 0, 4, 0, 0, 0,0 0, 4], |
||||
|
[0, 0, 0, 2, 0, 2, 5, 0, 0, 1, 2], |
||||
|
[0, 0, 0, 0, 5, 0, 0, 0, 0, 4, 0], |
||||
|
[1, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0]] |
||||
|
|
||||
|
# 计算两个评分的欧氏距离 |
||||
|
def esclidSim(inA,inB): |
||||
|
if len(inA)<3: |
||||
|
return 1.0 |
||||
|
return 1.0/(1.0+la.norm(inA-inB)) |
||||
|
# 计算两个评分的 皮尔逊相关系数 (Pearson Correlation) |
||||
|
def pearsSim(inA,inB): |
||||
|
if len(inA)<3: |
||||
|
return 1.0 |
||||
|
return 0.5+0.5*corrcoef(inA.inB)[0][1] |
||||
|
# 计算两个评分的余弦相似度 (Cosine similarity) |
||||
|
def cosSim(inA,inB): |
||||
|
num = float(inA.T*inB) |
||||
|
denom = la.norm(inA)*la.norm(inB) |
||||
|
return 0.5+0.5*(num/denom) |
||||
|
|
||||
|
# 基于评分的相似度推荐 |
||||
|
def standEst(dataMat,user,sinMeas,item): |
||||
|
n = shape(dataMat)[1] |
||||
|
simTotal = 0.0;ratsimTotal = 0.0 |
||||
|
for j in range(n): |
||||
|
userRating = dataMat[user,j] |
||||
|
if userRating ==0: |
||||
|
continue |
||||
|
overLap = nonzero(logical_and(dataMat[:,item].A>0,dataMat[:,j].A>0))[0] |
||||
|
if len(overLap)==0: |
||||
|
simility =0 |
||||
|
else: |
||||
|
simility = sinMeas(dataMat[overLap,item],dataMat[overLap,j]) |
||||
|
print("the %d and %d similit is :%f"%(item,j,simility)) |
||||
|
simTotal +=simility |
||||
|
ratsimTotal+=simility*userRating |
||||
|
if simTotal == 0: |
||||
|
return 0 |
||||
|
else: |
||||
|
return ratsimTotal/simTotal |
||||
|
|
||||
|
def recommand(dataMat,user,n=3,simMeans=cosSim,estMethod =standEst): |
||||
|
unratedItems = nonzero(dataMat[user,:].A==0)[1] |
||||
|
if len(unratedItems)==0: |
||||
|
return "you rated everything" |
||||
|
itemScores =[] |
||||
|
for item in unratedItems: |
||||
|
estimatscore = estMethod(dataMat,user,simMeans,item) |
||||
|
itemScores.append((item,estimatscore)) |
||||
|
return sorted(itemScores,key = lambda jj:jj[1],reverse=True)[:n] |
||||
|
|
||||
|
myMat = mat(loadExData2()) |
||||
|
print(recommand(myMat, 1)) |
||||
|
x=[[1,2],[3,4]] |
||||
|
x=mat(x) |
||||
|
y=array(x) |
||||
|
print(x*x) |
||||
|
print(y*y) |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
Loading…
Reference in new issue