《集体智慧编程》 读书笔记 第二章

作为个人记录之用,主要是将代码及其注释贴出来。

from math import sqrt
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}}

#欧几里德距离
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]])

 #威尔逊相关度  绘制一条尽可能靠近地图上所有的坐标点 称为最佳拟合线


def simPerson(prefs, p1, p2):
#得到双方都评价过的物品列表
    si = {}
    for item in prefs[p1]:
        if item in prefs[p2]: si[item] = 1
    n = len(si)

    if n == 0:
        return -1

    sum1 = sum([prefs[p1][it] for it in si])
    sum2 = sum([prefs[p2][it] for it in si])

    #求平方和
    sum1sq = sum([pow(prefs[p1][it], 2) for it in si])
    sum2sq = sum([pow(prefs[p2][it], 2) for it in si])

    #求乘积之和
    pSum = sum([prefs[p1][it]*prefs[p2][it] for it in si])

    #计算皮尔逊评价值
    num = pSum - (sum1*sum2/n)
    den = sqrt((sum1sq-pow(sum1, 2)/n)*(sum2sq-pow(sum2, 2)/n))
    if den == 0:
        return 0
    r = num/den
    return r


def topMatches(prefs, person, n=5, similarity=simPerson):
    scores = []
    # scores=[(similarity(prefs,person,other),other)
    #         for other in prefs if other != person]
    for other in prefs:
        if other != person:
            scores.append((similarity(prefs, person, other), other))
    scores.sort()
    scores.reverse()
    print(scores[0:n])
    return scores[0:n]

topMatches(critics, 'Toby', n=6)


def get_recommendation(prefs, person, similarity=simPerson):
    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


def transformPrefs(prefs):
    result = {}
    for person in prefs:
        for item in prefs[person]:
            result.setdefault(item, {})
            #字典中如果有item没有这个key,就插入这个key并赋值,并返回result的值(默认为None)
            #如果有这个key则返回相应的value
            #作用在于将所有的电影名添加
            result[item][person] = prefs[person][item]
    return result

import pydelicious

print(pydelicious.get_popular(tag='programming'))

def calculateSimlarItems(prefs, n = 10):
    result = {}
    #以物品为中心对偏好矩阵实施倒置处理
    itemPrefs = transformPrefs(prefs)
    c = 0
    for item in itemPrefs:
        c += 1
        if c % 100 == 0:
            d = c / len(itemPrefs)
            print(d)
        scores = topMatches(itemPrefs, item, n=n, similarity=sim_distance)
        result[item] = scores
    return result

def getRcommendeditems(prefs, itemMatch, user):
    userRatings = prefs[user]
    scores = {}
    totalSim = {}
    for(item, rating) in userRatings.items(): #循环遍历由当前用户评分的物品
        for (similarity, item2) in itemMatch[item]:#循环遍历与当前物品相近的物品
            if item2 in userRatings:
                continue
            scores.setdefault(item2, 0)
            scores[item2] += similarity * rating #相似度*当前物品的评分 对某部电影有一个评分,找到相似的并求出相似度,推算出评价分

            totalSim.setdefault(item2, 0)
            totalSim[item2] += similarity

    rankings = [(scores / totalSim[item], item) for item, score in scores.items()] #此时的item为相似的物品,score为加权分

    rankings.sort()
    rankings.reverse()
    return rankings
原文地址:https://www.cnblogs.com/caidongzhou/p/5921005.html