def __init__(self, records): Cluster.__init__(self, records) vote = dict() count = dict() for r in records: if r.test != 0: continue basic.AddToDict(vote, r.item, r.vote) basic.AddToDict(count, r.item, 1) k = 0 for item, v in vote.items(): ave = v / (count[item] * 1.0) c = int(ave * 2) self.group[item] = c
def UserSimilarity(records): item_users = dict() ave_vote = dict() activity = dict() for r in records: if r.test != 0: continue basic.AddToMat(item_users, r.item, r.user, r.vote) basic.AddToDict(ave_vote, r.user, r.vote) basic.AddToDict(activity, r.user, 1) ave_vote = {x:y/activity[x] for x,y in ave_vote.items()} nu = dict() W = dict() for i,ri in item_users.items(): for u,rui in ri.items(): basic.AddToDict(nu, u, (rui - ave_vote[u])*(rui - ave_vote[u])) for v,rvi in ri.items(): if u == v: continue basic.AddToMat(W, u, v, (rui - ave_vote[u])*(rvi - ave_vote[v])) for u in W: W[u]= {x:y/math.sqrt(nu[x]*nu[u]) for x,y in W[u].items()} return W,ave_vote
def __init__(self, records): Cluster.__init__(self, records) popularity = dict() for r in records: if r.test != 0: continue basic.AddToDict(popularity, r.item, 1) k = 0 for item, n in sorted(popularity.items(), key=itemgetter(1), reverse=False): c = int((k * 5) / (1.0 * len(popularity))) self.group[item] = c k += 1
def __init__(self, records): Cluster.__init__(self, records) activity = dict() for r in records: if r.test != 0: continue basic.AddToDict(activity, r.user, 1) k = 0 for user, n in sorted(activity.items(), key=itemgetter(1), reverse=False): c = int((k * 5) / (1.0 * len(activity))) self.group[user] = c k += 1