基于利益匹配的人

问题描述 投票:1回答:2

通过填写一份轮廓指示个性,生活方式,兴趣等提供的数据基础上的相容性得分匹配用户:以下问题

每个属性是标签(例如属性平静人格),其或者是真(1)或假(0)。假设我们希望找到两个用户的兼容性。

Extract from panda DataFrame for personality

用户2从用户3减去,差异平方和的差之和被置于相对于最大可能偏差(为一个类别等个性属性的数量)。倒数是那么的相似性得分。这同样适用于所有类别完成(例如生活方式)

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

每个用户在数据帧的用户有比较:

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+)的数量增加了运行。任何建议,以加快这,使代码更容易吗?

python data-science matching recommender-systems
2个回答
0
投票

希望我有问题陈述正确的是:

我有数据帧X二进制指针变量。 (0,1)对于X的每一行(其代表不同的用户)的我想其他用户/行中找到最类似的用户/行。

我将使用sklearn,from here的NearestNeighbors类:

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.neighbors import NearestNeighbors
X = np.array([[0,0,0,0,1],
              [0,0,0,0,1],
              [1,1,1,0,0],
              [1,0,0,1,1]])

综观X,我们可以看到,IDX = 1,IDX = 2是最similiar。他们完全匹配。他们应与彼此为“最similiar。”

# two nbrs since first match is self match
nbrs = NearestNeighbors(n_neighbors=2, metric='dice').fit(X)
distances, indices = nbrs.kneighbors(X) 
print(indices) 

#remember first val in this array per line is self match
[[0 1]
[0 1]
[2 3]
[3 1]]

要将您的加权分数,我不是超级肯定。我的第一个想法是把你的二进制数据的排列,乘“这是多么重要,以我”就用在最邻近搜索不同的指标,例如"euclidean"或什么的。它种需要大约明确什么是包含在那些其他dataframes更多信息。

因此,可以说用户1和2(通过其索引位置)表示,第三列是超级重要的(一个“10” 0-10),而第三列在这里填写这样:

X = np.array([[0,0,0,0,1],
             [0,0,1,0,1],
             [1,1,1,0,0],
             [1,0,0,1,1]])
# notice they match now on that 3rd col, but disagree elsewhere

#ugly hack for replacing two vals
np.put(X[1], [2], [10]) # grab second row, third col, place [10]
np.put(X[2], [2], [10])

print(X)

[[ 0  0  0  0  1]
[ 0  0 10  0  1]
[ 1  1 10  0  0]
[ 1  0  0  1  1]]

现在,他们都认为这个问题是超级重要。现在尝试邻居用不同的指标计算:

nbrs = NearestNeighbors(n_neighbors=2, metric='euclidean').fit(X)

d, i = nbrs.kneighbors(X)
print(d)
print(i)

[[0.         1.41421356]
 [0.         1.73205081]
 [0.         1.73205081]
 [0.         1.41421356]]
[[0 3]
 [1 2]
 [2 1]
 [3 0]]

[1,2][2,1]指示第二行和第三行现在是最接近在一起以彼此。 (记住在阵列i第一val是自我匹配)

这里有精美的细节,我掩饰这可能使最近的邻居不合适的,但你可以阅读有关them in other various places


0
投票

@Dylan我与NearestNeighbours唯一的问题是,它会呈现不同的结果我采取的方法。一个例子:

from sklearn.neighbors import NearestNeighbors
import numpy as np

X = np.array([[0,0,0,0,1],
             [0,0,1,1,0]])

nbrs = NearestNeighbors(n_neighbors=2, metric = 'euclidean').fit(X)
distances, indices = nbrs.kneighbors(X)
print(distances)
print(1/ (1+distances)) # returns a similarity score between 0 and 1

个相似性得分是在0.366,而它应该是40%,因为它们的绝对偏差是3超过5个变量的 - > 60%的

© www.soinside.com 2019 - 2024. All rights reserved.