Calculation deviation:
The card () represents the number of elements that the collection contains.
Weighted slope one algorithm
# coding:utf-8__author__ = ' similarface ' import codecs, OS, sysfrom math import sqrt ' ' This data: {"user": {"Band": Score}} "' Users2 = {" A My ": {" Taylor Swift ": 4," Psy ": 3," Whitney Houston ": 4}," Ben ": {" Taylor Swift ": 5," Psy ": 2}," Clara ": {"PSY": 3.5, "Whitney Houston": 4}, "Daisy": {"Taylor Swift": 5, "Whitney Houston": 3}}class recommender:def __init__ (self, data, k=1, metric= ' Pearson ', n=5): self.k = k SELF.N = n self.username2id = {} Self.userid2name = {} Self.productid2name = {} Self.metric = metric if self.metric = = ' Pearson ': Self.fn = Self.pearson if Type (data). __name__ = = ' Dict ': self.data = data #频率值 Both the B and a are evaluated Number of users self.frequencies={} #样本A对样本B的偏差值 self.deviations={} def computerdeviation (self): "' Calculate the deviation between samples: return: ' #{' User: {"Band 1": Rating 1, "Band 2": Rating 2, "band n": Rating n}} = "ratings={": Score} for Ratings in Self.data.valUEs (): # "band n": Score N for (item,rating) in Ratings.items (): #频率值 2 Number of users who have scored simultaneously #setdefault if the key is in the dictionary, the value corresponding to the key is returned. If the key is not in the dictionary, insert the key into the dictionary, with {} as the value of the key, and return {} self.frequencies.setdefault (item, {}) #偏差值 Self.deviations.setdefault (item, {}) for (item2,rating2) in Ratings.items (): If Item !=item2:self.frequencies[item].setdefault (item2,0) self.deviations[item].se Tdefault (item2,0.0) self.frequencies[item][item2]+=1 self.deviations[item][i Tem2]+=rating-rating2 for (item,ratings) in Self.deviations.items (): For ITEM2 in ratings: #dev (I,j) ratings[item2]/=self.frequencies[item][item2] def convertproductid2name (self, id): ' ' Given product number return product name ' if ID in Self.productid2name:return self.productid2name[ID] Else:return ID def slopeonerecommendations (self,userratings): "Traverse all samples of user U reviews: u[i ] The deviation matrix traversing user u: dev[j,i] SUM ((Dev[j,i]+u[i]) *c[j,i]) ==?c[j,i]=frequencies[j][i]:p Aram use Rratings:: Return: ' recommendations={} frequencies={} for (useritem,userrating) in U Serratings.items (): for (diffitem,diffratting) in Self.deviations.items (): If Diffitem not in US Erratings and Useritem in Self.deviations[diffitem]: Freq=self.frequencies[diffitem][useritem] Recommendations.setdefault (diffitem,0.0) Frequencies.setdefault (diffitem,0) recommendations[diffitem]+= (diffratting[useritem]+userrating) *freq frequencies[diffitem]+=freq Recommendations=[(Self.convertproductid2name (k), V/frequencies[k]) for K, V in Recommendations.items ()] Recommend Ations.sort (Key=lambda ARtisttuple:artisttuple[1],reverse=true) return recommendationsif __name__ = = ' __main__ ': R=recommender (USERS2) R.computerdeviation () g=users2[' Ben '] result=r.slopeonerecommendations (g) Print (Result)
[(' Whitney Houston ', 3.375)]
Slope one of data mining