"""
Created on Wed Jun 20 21:02:58 2018
@author: luogan
"""
from numpy import *
from numpy import linalg as la
def loadExData ():
return [[0 , 0 , 0 , 2 , 2 ],
[0 , 0 , 0 , 3 , 3 ],
[0 , 0 , 0 , 1 , 1 ],
[1 , 1 , 1 , 0 , 0 ],
[2 , 2 , 2 , 0 , 0 ],
[5 , 5 , 5 , 0 , 0 ],
[1 , 1 , 1 , 0 , 0 ]]
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 , 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 ecludSim (inA,inB ):
return 1.0 /(1.0 + la.norm(inA - inB))
def pearsSim (inA,inB ):
if len (inA) < 3 : return 1.0
return 0.5 +0.5 *corrcoef(inA, inB, rowvar = 0 )[0 ][1 ]
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, simMeas, 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 : similarity = 0
else : similarity = simMeas(dataMat[overLap,item], \
dataMat[overLap,j])
print ('the %d and %d similarity is: %f' % (item, j, similarity))
simTotal += similarity
ratSimTotal += similarity * userRating
if simTotal == 0 : return 0
else : return ratSimTotal/simTotal
def svdEst (dataMat, user, simMeas, item ):
n = shape(dataMat)[1 ]
simTotal = 0.0 ; ratSimTotal = 0.0
U,Sigma,VT = la.svd(dataMat)
Sig4 = mat(eye(4 )*Sigma[:4 ])
xformedItems = dataMat.T * U[:,:4 ] * Sig4.I
Sig = mat(eye(n)*Sigma)
for j in range (n):
userRating = dataMat[user,j]
if userRating == 0 or j==item: continue
similarity = simMeas(xformedItems[item,:].T,\
xformedItems[j,:].T)
print ('the %d and %d similarity is: %f' % (item, j, similarity))
simTotal += similarity
ratSimTotal += similarity * userRating
if simTotal == 0 : return 0
else : return ratSimTotal/simTotal
def recommend (dataMat, user, N=3 , simMeas=cosSim, estMethod=standEst ):
print (nonzero(dataMat[user,:].A==0 ))
unratedItems=nonzero(dataMat[user,:].A==0 )[1 ]
print (unratedItems)
if len (unratedItems) == 0 : return 'you rated everything'
itemScores = []
for item in unratedItems:
estimatedScore = estMethod(dataMat, user, simMeas, item)
itemScores.append((item, estimatedScore))
return sorted (itemScores, key=lambda jj: jj[1 ], reverse=True )[:N]
def printMat (inMat, thresh=0.8 ):
for i in range (32 ):
for k in range (32 ):
if float (inMat[i,k]) > thresh:
print (1 ),
else : print ( 0 ),
print ('' )
def imgCompress (numSV=3 , thresh=0.8 ):
myl = []
for line in open ('0_5.txt' ).readlines():
newRow = []
for i in range (32 ):
newRow.append(int (line[i]))
myl.append(newRow)
myMat = mat(myl)
print ("****original matrix******" )
printMat(myMat, thresh)
U,Sigma,VT = la.svd(myMat)
SigRecon = mat(zeros((numSV, numSV)))
for k in range (numSV):
SigRecon[k,k] = Sigma[k]
reconMat = U[:,:numSV]*SigRecon*VT[:numSV,:]
print ("****reconstructed matrix using %d singular values******" % numSV)
printMat(reconMat, thresh)
if __name__ == '__main__' :
print ("begin" )
myData=loadExData2()
myMat=mat(myData)
recommend(myMat, 2 , 3 , cosSim, svdEst)
begin
(array([0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]), array([ 0 , 1 , 2 , 3 , 5 , 6 , 8 , 10 ]))
[ 0 1 2 3 5 6 8 10 ]
the 0 and 4 similarity is: 0 .487100
the 0 and 7 similarity is: 0 .996341
the 0 and 9 similarity is: 0 .490280
the 1 and 4 similarity is: 0 .485583
the 1 and 7 similarity is: 0 .995886
the 1 and 9 similarity is: 0 .490272
the 2 and 4 similarity is: 0 .485739
the 2 and 7 similarity is: 0 .995963
the 2 and 9 similarity is: 0 .490180
the 3 and 4 similarity is: 0 .450495
the 3 and 7 similarity is: 0 .482175
the 3 and 9 similarity is: 0 .522379
the 5 and 4 similarity is: 0 .506795
the 5 and 7 similarity is: 0 .494716
the 5 and 9 similarity is: 0 .496130
the 6 and 4 similarity is: 0 .434401
the 6 and 7 similarity is: 0 .479543
the 6 and 9 similarity is: 0 .583833
the 8 and 4 similarity is: 0 .490037
the 8 and 7 similarity is: 0 .997067
the 8 and 9 similarity is: 0 .490078
the 10 and 4 similarity is: 0 .512896
the 10 and 7 similarity is: 0 .524970
the 10 and 9 similarity is: 0 .493617
原文链接
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)