多列值的余弦相似度

cosine similarity for multiple column values

id  size    numberOfPlants  balcony   available  publicTransport    
0   1191    3               0         1           1
1   3251    2               1         0           0
2   1641    1               1         1           0
3   2133    3               0         0           1

我有一个包含多个列值的数据集,其中每一行代表一个对象。我想计算所有行之间的余弦相似度,以便我考虑具有相同权重的所有因素、大小、numberOfPlants、阳台(布尔值)、可用(布尔值)和公共交通(布尔值)。如果我输入 id,我想取回看起来最相似的行的 id。我如何在这里计算余弦相似度?

这应该可以解决成对余弦相似性问题(这里我假设您已经确定了潜在的 NaN):

from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(df)
print(similarity)

示例 df:

   id  size  numberOfPlants  balcony  available  publicTransport
0   1     3               1        0          1                1
1  23     4               2        4          0                2
2   3     5               2        5          1                3
3   4     6               2       76          3                4
4   5     7              34        4          4                5
5   6     8               5        2          5                6
6   4     4               6        1          6                4

然后上面的代码returns

[[1.         0.45345772 0.77907297 0.11234465 0.53344315 0.92557018
  0.80683665]
 [0.45345772 1.         0.5838878  0.23664101 0.276951   0.60827093
  0.50306617]
 [0.77907297 0.5838878  1.         0.67443414 0.51221106 0.85759678
  0.71288699]
 [0.11234465 0.23664101 0.67443414 1.         0.16973016 0.25879044
  0.19243529]
 [0.53344315 0.276951   0.51221106 0.16973016 1.         0.63498516
  0.76022001]
 [0.92557018 0.60827093 0.85759678 0.25879044 0.63498516 1.
  0.93652389]
 [0.80683665 0.50306617 0.71288699 0.19243529 0.76022001 0.93652389
  1.        ]]

您可以 cosine_similarity 来自 sklearn。其中 target 是您要为其查找相似项的任何行。最好从比较中排除 id

from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

def get_similar_row(rows, target):
    """Return the index of the most similar row"""
    return np.argmax(cosine_similarity(rows, [target]))


get_similar_row([[1191, 3, 0, 1, 1], 
                 [3251, 2, 1, 0, 0], 
                 [1641, 1, 1, 1, 0]], [2133, 3, 0, 0, 1], top_n=2)

输出将是:1

如果您需要获取 top n 相似的行:

def get_top_n_similar_rows(rows, target, top_n=1):
    """Return the top n indices of the most similar rows"""
    return cosine_similarity(rows, [target]).reshape(1, -1)[0].argsort()[::-1][:top_n]