Collective Smart Programming-Recommendation System (2), collective Smart Programming recommendation
Step 3: score reviewers
Now that we have a function for comparing two people, we can write a program below to rate each person based on the specified person and find the closest matching result. In this example, we are interested in finding Movie reviewers who are similar to ourselves, so that we know who should adopt their suggestions when selecting a video.
def topMatches(prefs,person,n=5,similarity=sim_pearson): scores=[(similarity(prefs,person,other),other) for other in prefs if other!=person] scores.sort() scores.reverse() return scores[0:n]
This function uses the list derivation to compare itself with every other user in the dictionary. Returns the first N items of the sorting result.
>>>reload(recommendations)>>> recommendations.topMatches(recommendations.critics,'Toby',n=3)[(0.9912407071619299, 'Lisa Rose'), (0.9244734516419049, 'Mick LaSalle'), (0.8934051474415647, 'Claudia Puig')]
Step 4: Recommended items
We use a weighted evaluation value to score the film. The score of the reviewer forms a ranking. To this end, we must obtain the evaluation results of all other reviewers to obtain the similarity and multiply them by the value given to each video.
def getRecommendations(prefs,person,similarity=sim_pearson): totals={} simSums={} for other in prefs: if other==person:continue #do not compare with self sim=similarity(prefs,person,other) #ignore the value <=0 if sim<=0 :continue for item in prefs[other]: if item not in prefs[person] or prefs[person][item]==0: totals.setdefault(item,0) totals[item]+=prefs[other][item]*sim simSums.setdefault(item,0) simSums[item]+=sim ranking=[(total/simSums[item],item)for item,total in totals.items() ] ranking.sort() ranking.reverse() return ranking
In this way, we can find ourselves that we should watch movies next.
Reference: collective Smart Programming