#!/usr/bin/env python3# -*- coding: utf-8 -*-"""
Created on Wed Jun 20 21:02:58 2018
@author: luogan
"""#coding=UTF-8from numpy import *
from numpy import linalg as la
defloadExData():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]]
defloadExData2():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]]
defecludSim(inA,inB):return1.0/(1.0 + la.norm(inA - inB))
defpearsSim(inA,inB):if len(inA) < 3 : return1.0return0.5+0.5*corrcoef(inA, inB, rowvar = 0)[0][1]
defcosSim(inA,inB):
num = float(inA.T*inB)
denom = la.norm(inA)*la.norm(inB)
return0.5+0.5*(num/denom)
defstandEst(dataMat, user, simMeas, item):
n = shape(dataMat)[1]
simTotal = 0.0; ratSimTotal = 0.0for 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 = 0else: 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: return0else: return ratSimTotal/simTotal
defsvdEst(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]) #arrange Sig4 into a diagonal matrix
xformedItems = dataMat.T * U[:,:4] * Sig4.I #create transformed items
Sig = mat(eye(n)*Sigma) #arrange Sig4 into a diagonal matrix#print Sig#print U * Sig * VT #back up source mat#print xformedItems #item feature begin compute item similer#print "user feature:"#xformedUsers = dataMat * VT[:,:4] * Sig4#print xformedUsers#print xformedUsers * xformedItems.T#print dataMatfor j in range(n):
userRating = dataMat[user,j]
if userRating == 0or 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: return0else: return ratSimTotal/simTotal
defrecommend(dataMat, user, N=3, simMeas=cosSim, estMethod=standEst):#print 'type', dataMat[:,:4] #the number user line or colprint (nonzero(dataMat[user,:].A==0)) # to array
unratedItems=nonzero(dataMat[user,:].A==0)[1]
print (unratedItems)
#unratedItems = nonzero(dataMat[user,:].A==0)[1]#find unrated items 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]
defprintMat(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 ('')
defimgCompress(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):#construct diagonal matrix from vector
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)
#myMat = mat(loadExData)
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]))
[ 012356810]
the0and4 similarity is: 0.487100the0and7 similarity is: 0.996341the0and9 similarity is: 0.490280the1and4 similarity is: 0.485583the1and7 similarity is: 0.995886the1and9 similarity is: 0.490272the2and4 similarity is: 0.485739the2and7 similarity is: 0.995963the2and9 similarity is: 0.490180the3and4 similarity is: 0.450495the3and7 similarity is: 0.482175the3and9 similarity is: 0.522379the5and4 similarity is: 0.506795the5and7 similarity is: 0.494716the5and9 similarity is: 0.496130the6and4 similarity is: 0.434401the6and7 similarity is: 0.479543the6and9 similarity is: 0.583833the8and4 similarity is: 0.490037the8and7 similarity is: 0.997067the8and9 similarity is: 0.490078the10and4 similarity is: 0.512896the10and7 similarity is: 0.524970the10and9 similarity is: 0.493617