https://yq.aliyun.com/articles/539247python
首先用一个词就能很好的解释什么叫作基于用户的协同过滤算法:【臭味相投】。虽然是贬义词,但也说明了,具备相似特征的人群,他们喜欢的东西不少也是同样的。所以,在推荐系统中,假设要为A用户推荐物品,能够经过寻找他的“邻居”——与A具备类似兴趣的用户。把那些用户喜欢的,而A用户却未曾据说的东西推荐给A。git
假设某天你购买了机器学习书籍,那么淘宝会给你推荐python书籍。由于机器通过判断得出这二者类似度很高,你既然会喜欢机器学习那么理应喜欢python。github
基于物品的协同过滤算法就是给用户推荐那些和他们以前喜欢的物品类似的物品。算法
不过, ItemCF算法并不利用物品的内容属性计算物品之间的类似度,它主要经过分析用户的行为记录计算物品之间的类似度。该算法认为,物品A和物品B具备很大的类似度是由于喜欢物品A的用户大都也喜欢物品B。json
https://www.jianshu.com/p/e56665c54df8机器学习
基于领域的协同过滤算法主要有两种,一种是基于物品的,一种是基于用户的。所谓基于物品,就是用户喜欢了X商品,咱们给他推荐与X商品类似的商品。所谓基于用户,就是用户A和用户B类似,用户A买了X、Y,用户B买了X、Y、Z,咱们就给用户A推荐商品Z。scrapy
使用基于物品的协同过滤,须要维护一个物品类似度矩阵;使用基于用户的协同过滤,须要维护一个用户类似度矩阵。能够设想,若是物品之间的类似度常常变化,那么物品类似度的矩阵则须要常常更新。若是物品常常增长,那么物品类似度的矩阵也会增加的很是快。新闻网站就同时具备这两个特色,因此基于物品的协同过滤并不适用于新闻的推荐。
类似性计算方法:学习
https://blog.csdn.net/zz_dd_yy/article/details/51924661网站
根据皮尔逊相关系数的值参考如下标准,能够大概评估出二者的类似程度:spa
- 0.8-1.0 极强相关
- 0.6-0.8 强相关
- 0.4-0.6 中等程度相关
- 0.2-0.4 弱相关
- 0.0-0.2 极弱相关或无相关
https://github.com/fanqingsong/EBooksRecommander
包括两部分:
一、 抓取用户读书数据
二、 进行推荐
推荐代码:
# -*- coding: utf-8 -*- from __future__ import division from math import sqrt from dataloader import loadJsonObjectToDict import pickle import os import json import io PENALTY_RATIO = 9 def sim_tanimoto(prefs, personA, personB): print "enter sim_tanimoto" keys_a = set(prefs[personA]) keys_b = set(prefs[personB]) intersection = keys_a & keys_b unionDict = dict(prefs[personA], **prefs[personB]) return len(intersection)/len(unionDict) def sim_euclid(prefs, personA, personB): print "enter sim_euclid" si = {} #Dict for shared item for item in prefs[personA]: if item in prefs[personB]: si[item] = 1 #Zero shared item -> not similar at all if len(si) == 0: return 0 sum_of_squares = sum([pow(prefs[personA][item] - prefs[personB][item], 2) for item in si]) r = 1/(1+sqrt(sum_of_squares)) return r def sim_pearson(prefs, personA, personB): print "enter sim_pearson" si = {} #Dict for shared item for item in prefs[personA]: if item in prefs[personB]: si[item] = 1 n = len(si) if n == 0: return 0 #sum sumA = sum([prefs[personA][item] for item in si]) sumB = sum([prefs[personB][item] for item in si]) #sum sqrt sumASqrt = sum([pow(prefs[personA][item], 2) for item in si]) sumBSqrt = sum([pow(prefs[personB][item], 2) for item in si]) #power of sum pSum = sum(prefs[personA][it] * prefs[personB][it] for it in si) #pearson Formula 4 num = pSum - (sumA*sumB/n) den = sqrt((sumASqrt - pow(sumA, 2)/n) * (sumBSqrt - pow(sumB, 2)/n)) if den == 0: return 0 r = num/den return r def sim_combine(prefs, personA, personB): print "enter sim_combine" return (sim_euclid(prefs, personA, personB) + sim_tanimoto(prefs, personA, personB) * PENALTY_RATIO)/(PENALTY_RATIO + 1) def topMatches(prefs, person, n=5, similarity = sim_pearson): print "enter topMatches" #scores = [(sim_pearson(prefs, person, other) * sim_euclid(prefs, person, other), other) for other in prefs if other != person] scores = [(similarity(prefs, person, other), other) for other in prefs if other != person] scores.sort() scores.reverse() return scores[0:n] def getRecommandations(prefs, person,similarity = sim_pearson): print "enter getRecommandations" totals = {} simSums = {} for other in prefs: if other == person : continue sim = similarity(prefs, person, other) 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 rankings = [(total/simSums[item], item) for item, total in totals.items()] rankings.sort() rankings.reverse() return rankings def transformPrefs(prefs): print "enter transformPrefs" result = {} for person in prefs: for item in prefs[person]: result.setdefault(item,{}) result[item][person] = prefs[person][item] return result def calculationSimilarItem(prefs, simFunction, dumpedfilePath, n=10): print "enter calculationSimilarItem" result = {} if os.path.exists(dumpedfilePath): print('find preprocessed data, loading directly...') with io.open(dumpedfilePath, 'rb') as f: result = pickle.load(f) return result itemPrefs = transformPrefs(prefs) for item in itemPrefs: scores = topMatches(itemPrefs, item, n=n, similarity=simFunction) result[item] = scores with io.open(dumpedfilePath, 'wb') as f: pickle.dump(result,f) return result def getRecommandedItems(itemMatch, userRating): print "enter getRecommandedItems" # print json.dumps(itemMatch, encoding="utf-8", ensure_ascii=False) # print "----------------------------------------------------------------------" # print json.dumps(userRating, encoding="utf-8", ensure_ascii=False) scores = {} totalSim = {} for (item, rating) in userRating.items(): # print item.encode("UTF-8") for (similarity, itemSim) in itemMatch[item]: if itemSim in userRating or similarity <= 0: continue scores.setdefault(itemSim,0) scores[itemSim] += similarity*rating totalSim.setdefault(itemSim,0) totalSim[itemSim] += similarity rankings =[(score/totalSim[item], item) for item,score in scores.items()] rankings.sort() rankings.reverse() return rankings def readUserPrefs(userRatingPath): print "enter readUserPrefs" userRating = {} if os.path.exists(userRatingPath): f = io.open(userRatingPath, 'r', encoding="utf-8") for line in f: txtSeg = line.split() userRating[txtSeg[0]] = float(txtSeg[1]) return userRating #TestCode def ItemBasedReco(): #Load scrapy data into {User -> Book -> Note} Dict loadedData = loadJsonObjectToDict("../data/YSData.json") # Read User prefs userRatingPath = "./UserPrefs.txt" userRating = readUserPrefs(userRatingPath) print("------------------ Item Based: Sim Euclid --------------------") #Using Euclid for Calculating Similarity #Calculate Top10 Matche book for each book with similarity point li = calculationSimilarItem(loadedData, sim_euclid, "../data/CalculatedItemSim" +"Euclid" + ".pkl") #Get the Recommandations re = getRecommandedItems(li, userRating) #Print recommandation for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) print("------------------ Item Based: Sim Tanimoto --------------------") #Using Euclid for Calculating Similarity #Calculate Top10 Matche book for each book with similarity point li = calculationSimilarItem(loadedData, sim_tanimoto, "../data/CalculatedItemSim" +"Tanimoto" + ".pkl") #Get the Recommandations re = getRecommandedItems(li, userRating) #Print recommandation for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) print("------------------ Item Based: Sim Pearson --------------------") #Using Euclid for Calculating Similarity #Calculate Top10 Matche book for each book with similarity point li = calculationSimilarItem(loadedData, sim_pearson,"../data/CalculatedItemSim" +"Pearson" + ".pkl") #Get the Recommandations re = getRecommandedItems(li, userRating) #Print recommandation for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) print("------------------ Item Based: Sim Tanimoto * 10 + Sim Euclid --------------------") #Using Euclid for Calculating Similarity #Calculate Top10 Matche book for each book with similarity point li = calculationSimilarItem(loadedData,sim_combine, "../data/CalculatedItemSim" +"Combine" + ".pkl") #Get the Recommandations re = getRecommandedItems(li, userRating) #Print recommandation for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) def UserBasedReco(): #Load scrapy data into {User -> Book -> Note} Dict loadedData = loadJsonObjectToDict("../data/YSData.json") # Read User prefs userRatingPath = "./UserPrefs.txt" userRating = readUserPrefs(userRatingPath) loadedData['Me'] = userRating re = getRecommandations(loadedData,'Me',sim_euclid) print("------------------ User Based: Sim Euclid --------------------") for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) re = getRecommandations(loadedData,'Me',sim_pearson) print("------------------ User Based: Sim Pearson --------------------") for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) re = getRecommandations(loadedData,'Me',sim_tanimoto) print("------------------ User Based: Sim Tanimoto --------------------") for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) re = getRecommandations(loadedData,'Me',sim_combine) print("------------------ User Based: Sim Tanimoto * 10 + Sim Euclid --------------------") for tl in re[0:15]: print (str(tl[0]) + ":" + tl[1]) if __name__ == '__main__': UserBasedReco() ItemBasedReco()