机器学习 — 提供推荐

提供推荐

  1. 计算两个人的相似度
  2. 本来是推荐平均评分较高的作品,考虑到两个人的爱好相似程度,对评分根据相似度进行加权平均

计算相似度:

  1. 欧几里得距离
  2. pearson相关度
critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
 'The Night Listener': 3.0},
'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 3.5},
'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
 'The Night Listener': 4.5, 'Superman Returns': 4.0,
 'You, Me and Dupree': 2.5},
'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 2.0},
'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

计算相关度

pearson相关系数计算公式(参考)
机器学习 — 提供推荐_第1张图片

from math import sqrt

# 欧几里得距离评价
def sim_distance(prefs, person1, person2):
    si = {}
    for item in prefs[person1]:
        if item in prefs[person2]:
            si[item] = 1
            
    if len(si) == 0:
        return 0
    
    sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2)
                         for item in prefs[person1] if item in prefs[person2]])

    return 1 / (1 + sqrt(sum_of_squares))

# 皮尔逊相关度评价
def sim_pearson(prefs, person1, person2):
    # 得到两者评价过的相同商品
    si = {}
    for item in prefs[person1]:
        if item in  prefs[person2]:
            si[item] = 1
   
    n = len(si)
    # 如果两个用户之间没有相似之处则返回1
    if n == 0:
        return 1
    
    # 对各自的所有偏好求和
    sum1 = sum([prefs[person1][item] for item in si])
    sum2 = sum([prefs[person2][item] for item in si])
    
    # 求各自的平方和
    sum1_square = sum([pow(prefs[person1][item], 2) for item in si])
    sum2_square = sum([pow(prefs[person2][item], 2) for item in si])
    
    # 求各自的乘积的平方
    sum_square = sum([prefs[person1][item] * prefs[person2][item] for item in si])
    
    # 计算pearson相关系数
    den = sqrt((sum1_square - pow(sum1, 2) / n) * (sum2_square - pow(sum2, 2) / n))
    if den == 0:
        return 0

    return (sum_square - (sum1 * sum2/n)) / den
    
print sim_distance(critics, 'Lisa Rose', 'Gene Seymour')
0.294298055086
print sim_pearson(critics, 'Lisa Rose', 'Gene Seymour')
0.396059017191

评论者打分

def topMatches(prefs, person, n = 5, simlarity = sim_pearson):
    scores = [(simlarity(prefs, person, other), other) for other in prefs if other != person]
    
    # 对列表进行排序,评价高者排在前面
    scores.sort()
    scores.reverse()
    # 取指定个数的(不需要判断n的大小,因为python中的元组可以接受正、负不在范围内的index)
    return scores[0:n]

寻找和“Toby”有相似偏好的人,取前3个

topMatches(critics, 'Toby', n = 3)
[(0.9912407071619299, 'Lisa Rose'),
 (0.9244734516419049, 'Mick LaSalle'),
 (0.8934051474415647, 'Claudia Puig')]
# 利用其他所有人的加权平均给用户推荐
def get_recommendations(prefs, person, similarity=sim_pearson):
    # 其他用户对某个电影的评分加权之后的总和
    totals = {}
    # 其他用户的相似度之和
    sim_sums = {}
    for other in prefs:
        # 不和自己比较
        if other == person:
            continue
        
        # 求出相似度
        sim = similarity(prefs, person, other)
        # 忽略相似度小于等于情况0的
        if sim <= 0:
            continue
        
        # 获取other所有的评价过的电影评分的加权值
        for item in prefs[other]:
            # 只推荐用户没看过的电影
            if item not in prefs[person] or prefs[person][item] == 0:
                #print item
                # 设置默认值
                totals.setdefault(item, 0)
                # 求出该电影的加权之后的分数之和
                totals[item] += prefs[other][item] * sim
                # 求出各个用户的相似度之和
                sim_sums.setdefault(item, 0)
                sim_sums[item] += sim
        

    # 对于加权之后的分数之和取平均值
    rankings = [(total / sim_sums[item], item) for item, total in totals.items()]

    # 返回经过排序之后的列表
    rankings.sort()
    rankings.reverse()
    return rankings
    

给出Toby的电影推荐列表

print get_recommendations(critics, 'Toby')
print get_recommendations(critics, 'Toby', similarity=sim_distance)
[(3.3477895267131013, 'The Night Listener'), (2.8325499182641614, 'Lady in the Water'), (2.5309807037655645, 'Just My Luck')]
[(3.457128694491423, 'The Night Listener'), (2.778584003814924, 'Lady in the Water'), (2.4224820423619167, 'Just My Luck')]

你可能感兴趣的:(机器学习 — 提供推荐)