gpt4 book ai didi

python - 当我读《集体智慧》时,我坚持了一些代码

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:02 25 4
gpt4 key购买 nike

最近我读了 Toby Segaran 写的集体智慧。但我一直在理解书中的一些代码。

这是recommendations.py中的一些代码

下面的代码是从偏好字典中返回人员的最佳匹配项,并使用加权平均值获取人员的推荐 其他所有用户的排名

# Return the Pearson correlation coefficient for p1 and p2
def sim_person(prefs, p1, p2):
# Get the list of shared_items
si={}
for item in prefs[p1]:
if item in prefs[p2]:si[item]=1

# Find the number of elements
n=len(si)

# if they have no ratings in common, return 0
if n==0: return 0

# Add up all the preferences
sum1 = sum([prefs[p1][it] for it in si])
sum2 = sum([prefs[p2][it] for it in si])

# Sum up the squares
sum1Sq = sum([pow(prefs[p1][it],2) for it in si])
sum2Sq = sum([pow(prefs[p2][it],2) for it in si])

# Sum up the products
pSum = sum([prefs[p1][it]*prefs[p2][it] for it in si])

# Calculate Person score
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

# Returns the best matches for person from the prefs dictionary.
# Number of results and similarity function are optional params.
def topMatch(prefs, person, n=5, similarity=sim_person):
scores = [(similarity(prefs, person, other), other)
for other in prefs if other!=person]

# Sort the list so the highest scores appear at the top
scores.sort()
scores.reverse()
return scores[0:n]

# Gets recommendations for a person by using a weighted average
# of every other user's rankings
def getRecommendations(prefs, person, similarity=sim_person):
totals = {}
simSums = {}
for other in prefs:
# don't compare me to myself
if other == person: continue
sim = similarity(prefs, person, other)

# ignore scores of zero of lower
if sim<=0: continue
for item in prefs[other]:

# only score movies I haven't seen yet
if item not in prefs[person] or prefs[person][item]==0:
# Similarity * Score
totals.setdefault(item, 0)
totals[item]+=prefs[other][item]*sim
# Sum of similarities
simSums.setdefault(item, 0)
simSums[item]+=sim

# Create the normalized list
rankings = [(total/simSums[item], item) for item, total in totals.items()]

# Return the sorted list
rankings.sort()
rankings.reverse()
return rankings

我无法理解的第一个代码是:

scores = [(similarity(prefs, person, other), other) for other in prefs if other!=person]

这句话中的第二个other是参数的意思吗?我可以将此代码更改为:

scores = [(similarity(prefs, person, other) for other in prefs if other!=person] 

我无法理解的第二个代码是:

rankings = [(total/simSums[item], item) for item, total in totals.items()]

最佳答案

看起来您正在构建元组。比较:

coordinates = (10, 2)

some_score = (similarity(prefs, person, other), other)

您正在创建一个 2 元素元组。第一个元素是 similarity(prefs, person, other),第二个元素是 other

关于python - 当我读《集体智慧》时,我坚持了一些代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32929508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com