Stay Hungry,Stay Foolish!

推荐算法(基于用户和基于物品)

推荐算法

https://yq.aliyun.com/articles/539247

基于用户的协同过滤算法

首先用一个词就能很好的解释什么叫做基于用户的协同过滤算法:【臭味相投】。虽然是贬义词,但也说明了,具有类似特征的人群,他们喜欢的东西很多也是一样的。因此,在推荐系统中,假设要为A用户推荐物品,可以通过寻找他的“邻居”——与A具有相似兴趣的用户把那些用户喜欢的,而A用户却不曾听说的东西推荐给A

 

基于物品的协同过滤算法

假设某天你购买了机器学习书籍,那么淘宝会给你推荐python书籍。因为机器经过判断得出这两者相似度很高,你既然会喜欢机器学习那么理应喜欢python。

基于物品的协同过滤算法就是给用户推荐那些和他们之前喜欢的物品相似的物品

不过, ItemCF算法并不利用物品的内容属性计算物品之间的相似度,它主要通过分析用户的行为记录计算物品之间的相似度。该算法认为,物品A和物品B具有很大的相似度是因为喜欢物品A的用户大都也喜欢物品B

 

https://www.jianshu.com/p/e56665c54df8

 基于领域的协同过滤算法主要有两种,一种是基于物品的,一种是基于用户的。所谓基于物品,就是用户喜欢了X商品,我们给他推荐与X商品相似的商品。所谓基于用户,就是用户A和用户B相似,用户A买了X、Y,用户B买了X、Y、Z,我们就给用户A推荐商品Z。

 

使用基于物品的协同过滤,需要维护一个物品相似度矩阵;使用基于用户的协同过滤,需要维护一个用户相似度矩阵。可以设想,如果物品之间的相似度经常变化,那么物品相似度的矩阵则需要经常更新。如果物品经常增加,那么物品相似度的矩阵也会增长的非常快。新闻网站就同时具有这两个特点,所以基于物品的协同过滤并不适用于新闻的推荐。

相似性计算方法:

https://blog.csdn.net/zz_dd_yy/article/details/51924661

根据皮尔逊相关系数的值参考以下标准,可以大概评估出两者的相似程度:

  • 0.8-1.0 极强相关
  • 0.6-0.8 强相关
  • 0.4-0.6 中等程度相关
  • 0.2-0.4 弱相关
  • 0.0-0.2 极弱相关或无相关

 

DEMO

https://github.com/fanqingsong/EBooksRecommander

包括两部分:

1、 抓取用户读书数据

2、 进行推荐

 

推荐代码:

# -*- 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()

 

posted @ 2019-08-01 01:00  lightsong  阅读(4302)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel