【发布时间】:2019-01-22 14:26:46
【问题描述】:
以下问题:通过填写表明个性、生活方式、兴趣等的个人资料提供的数据,根据兼容性得分匹配用户。
每个属性都是真 (1) 或假 (0) 的标签(例如性格冷静的属性)。假设我们想要找到两个用户的兼容性。
Extract from panda DataFrame for personality
从用户 3 中减去用户 2,对差异进行平方,然后将差异的总和与最大可能偏差(一个类别的属性数量等个性)相关联。倒数是相似度的分数。 所有类别(例如生活方式)都是如此
def similarityScore (pandaFrame, name1, name2):
profile1 = pandaToArray(pandaFrame, name1)#function changing DataFrane to array
profile2 = pandaToArray(pandaFrame, name2)
newArray = profile1 - profile2
differences = 0
for element in newArray:
element = (element)**2
differences += element
maxDifference = len(profile1)
similarity = 1 - (differences/maxDifference)
return similarity
每个用户都与 DataFrame 中的每个其他用户进行比较:
def scorecalc(fileName):
data = csvToPanda(fileName)
scorePanda = pd.DataFrame([], columns=userList, index=userList)
for user1 in userList:
firstUser = user1
for user2 in userList:
secondUser = user2
score = similarityScore(data, firstUser, secondUser)
scorePanda.iloc[[userList.index(firstUser)],[userList.index(secondUser)]] = score
return(scorePanda)
根据特定类别的相似性对用户的重要性,通过将相似性分数乘以偏好数据框来加权相似性分数:
def weightedScore (personality, lifestyle,preferences):
personality = personality.multiply(preferences['personality'])
lifestyle = lifestyle.multiply(preferences['lifestyle'])
weightscore = (personality + lifestyle)
return(weightscore)
结果将是从 0 到 1 的兼容性分数。
一切正常,但运行它需要相当长的时间,尤其是当比较的用户数量(100+)增加时。有什么建议可以加快速度,让代码更简单?
【问题讨论】:
-
您的所有数据是否都是二进制指标变量格式,就像您分享的小照片中显示的那样?
-
@dylan 是的
-
虽然您已经完成了很多出色的工作,但我可能会建议使用适用于二进制变量(例如 dice sorensen)的距离度量的基于最近邻的解决方案。 sklearn 有相当多的 c/cython 优化,可能会有所帮助。如果不是这样,也许是一个 numpy 解决方案?
标签: python data-science matching recommender-systems