defItemSim(self,train=None):train=trainorself.traindata#n=0Item_count=dict()#浏览过每种物品的人数#ItemSimcount=dict()#既浏览过i,又浏览过j的某人的时间加权#forusers,itemsi...
def ItemSim(self,train = None):
train = train or self.traindata
#n=0
Item_count = dict() #浏览过每种物品的人数#
ItemSimcount = dict() #既浏览过i,又浏览过j的某人的时间加权#
for users,items in train.items():
#n+=1
for i,tui in items.items():
Item_count.setdefault(i,0)
Item_count[i] += 1
for j,tuj in items.items():
if i == j:
continue
ItemSimcount.setdefault(users,{})
ItemSimcount[users].setdefault(i,{})
ItemSimcount[users][i].setdefault(j,0)
ItemSimcount[users][i][j] += 1
self.ItemSimlist = dict()
for user,rec in ItemSimcount.items():
self.ItemSimlist.setdefault(user,{})
for i ,related_items in rec.items():
self.ItemSimlist[user].setdefault(i,{})
for j, cij in related_items.items(): #cij:计算出的分子,存放喜欢i和j的某用户的时间加权#
self.ItemSimlist[user][i].setdefault(j,0)
self.ItemSimlist[user][i][j] = cij / math.sqrt(Item_count[i] * Item_count[j] * 1.0)
class中有这段代码,运行到self.ItemSimlist[user][i].setdefault(j,0)时会抱Memory Error的错,怎么破,求大神。
展开