# 基于物品的过滤

```def transformPrefs(prefs):
result={}
for person in prefs:
for item in prefs[person]:
result.setdefault(item,{})

# Flip item and person
result[item][person]=prefs[person][item]
return result

def calculateSimilarItems(prefs,n=5):
# Create a dictionary of items showing which other items they
# are most similar to.
result={}
# Invert the preference matrix to be item-centric
itemPrefs=transformPrefs(prefs)
c=0
for item in itemPrefs:
# Status updates for large datasets
c+=1
if c%100==0: print "%d / %d" % (c,len(itemPrefs))
# Find the most similar items to this one
scores=topMatches(itemPrefs,item,n=n,similarity=sim_distance)
result[item]=scores
return result```

import r
itemsim=r.calculateSimilarItems(r.critics)
itemsim
{‘Lady in the Water’: [(0.4494897427831781, ‘You, Me and Dupree’), (0.38742588672279304, ‘The Night Listener’), (0.3483314773547883, ‘Snakes on a Plane’), (0.3483314773547883, ‘Just My Luck’), (0.2402530733520421, ‘Superman Returns’)], ‘Snakes on a Plane’: [(0.3483314773547883, ‘Lady in the Water’)……

```def getRecommendedItems(prefs,itemMatch,user):
userRatings=prefs[user]
scores={}
totalSim={}
# Loop over items rated by this user
for (item,rating) in userRatings.items( ):

# Loop over items similar to this one
for (similarity,item2) in itemMatch[item]:

# Ignore if this user has already rated this item
if item2 in userRatings: continue
# Weighted sum of rating times similarity
scores.setdefault(item2,0)
scores[item2]+=similarity*rating
# Sum of all the similarities
totalSim.setdefault(item2,0)
totalSim[item2]+=similarity

# Divide each total score by total weighting to get an average
rankings=[(score/totalSim[item],item) for item,score in scores.items( )]

# Return the rankings from highest to lowest
rankings.sort( )
rankings.reverse( )
return rankings```

r.getRecommendedItems(r.critics,itemsim,’Toby’)
[(3.1667425234070894, ‘The Night Listener’), (2.936629402844435, ‘Just My Luck’), (2.868767392626467, ‘Lady in the Water’)]