Python Slope One

Source: Internet
Author: User
"" "#slope one algorithm is based on" the same thing "between the" scoring difference "of the linear algorithm, predicting the user to the item scoring personalized algorithm #适用商品的更新不频繁, a relatively stable number of items are significantly smaller than the number of users of the scene, depending on the user's behavior log and object preferences related content Advantages: 1 algorithm is simple, 2 can find the user's potential interests #缺点: dependent user behavior, there are cold startup problems and sparsity problems "" "#计算物品之间评分差的平均值, enter the following #items items-user Rating data Items #users user-item rating data users # Averages storage average Classification dictionary type data def buildaveragediffs (items, users, averages): #遍历每条物品-User Rating data for itemId in items: #遍 
            Calendar each other article-user rating data for Otheritemid in items: #物品间的评分偏差 average = 0.0 #两件物品均评过分的用户数 Userratingpaircount = 0 #若为不同的物品项 if itemId!= otheritemid: #遍历每条用户-Object Tasting data for userId in users: #每条数据为用户对物品的评分 userratings = Users[user Id] #当前物品项在用户的评分数据中, and the user also has a rating for other items if itemId in Userratings and Otheritemid in Userr Atings: #两件物品均评过分的用户数加1 userratingpaircount + 1 #
           Scoring deviations for each current item score-Other Items score sum             Average + = (Userratings[otheritemid]-userratings[itemid]) #物品间的评分偏差等于评分偏差和/Two items are rated excessive number of users averages[(itemid,otheritemid)] = Average/userratingpaircount #推荐评分, enter the following #users user-item rating data users #items items-user Rating data #averages rating deviations between items #targetUserId recommended users Targetuserid #targetItemId recommended items Targetitemid #return Predictive rating def
    Suggestedrating (Users, items, averages, Targetuserid, Targetitemid): #预测评分分母 runningratingcount = 0 #预测评分分子 Weightedratingtotal = 0.0 #遍历需要推荐的用户targetUserId对有过评分的物品 for i in Users[targetuserid]: #保存当前物品与要推荐物品targ Etitemid, common scoring user number Ratingcount = Userswhoratedboth (Users, I, Targetitemid) #分子 Weightedratingtotal + = (Users[targetuserid][i]-averages[(Targetitemid, i)]) * Ratingcount #分母 Runningratingcount + = Ratingcou NT #返回预测评分 return weightedratingtotal/runningratingcount #物品itemId1与itemId2共同有多少用户评分 def userswhoratedboth ( Users, ItemId1, itemId2): Count =0 #用户-Goods rating data users for userId in users: #用户对物品itemId1与itemId2都有过评分则计数值加1 if itemId1 in Users[userid

] and ItemId2 in Users[userid]: #加1 count = 1 #返回itemId1与itemId2共同评分用户数 return count 

    if __name__ = = ' __main__ ': items = {' A ': {1:5, 2:3}, ' B ': {1:3, 2:4, 3:2}, ' C ': {1:2, 3:5}}
    Users = {1: {' A ': 5, ' B ': 3, ' C ': 2}, 2: {' A ': 3, ' B ': 4}, 3: {' B ': 2, ' C ': 5}}} #物品间评分偏差初始化
    averages = {} #计算物品之间评分差的平均值 buildaveragediffs (items, users, averages) print averages #输出: Total items, number of users Print ({' ItemCount ': Len (items), ' UserCount ': Len (Users)}) print ("Guess that user A'll rate Item 3=" + STR (Sugg




 Estedrating (Users,items, averages, 3, ' A '))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.