我是机器学习的新手。我正在根据他们的兴趣等级(1 =低,10 =高)将SetA中的人与SetB中的人进行匹配。我的真实数据集具有40个功能(后来我也希望对某些功能以及不太常见的兴趣设置更高的权重-我相信this会帮助我?)
示例数据集:
>>> dfA = pd.DataFrame(np.array([[1, 1, 1], [4, 4, 4], [8, 8, 8]]),
columns=['interest1', 'interest2', 'interest3'],
index=['personA1','personA2','personA3'])
>>> dfB = pd.DataFrame(np.array([[4, 4, 3], [2, 2, 1], [1, 2, 2]]),
columns=['interest1', 'interest2', 'interest3'],
index=['personB1','personB2','personB3'])
print(dfA, "\n", dfB)
>>> interest1 interest2 interest3
personA1 1 1 1
personA2 4 4 4
personA3 8 8 8
interest1 interest2 interest3
personB1 4 4 3
personB2 2 2 1
personB3 1 2 2
我为此使用sklearn的最近邻居算法:
knn = NearestNeighbors(n_neighbors = 2).fit(dfA)
distances, indicies = knn.kneighbors(dfB)
>>> print(distances, "\n \n", indicies)
>>>[[1. 4.69041576]
[1.41421356 4.12310563]
[1.41421356 4.12310563]]
[[1 0]
[0 1]
[0 1]]
我不明白输出?我知道一个类似的问题explanation,但是我不知道如何将其应用于这种情况,因为有2个不同的数据集。
最终,我想要一个最终的数据帧来进行类似的匹配:
SetA SetB
personA1 personB2
personA2 personB1
personA3 personB3
您获得的结果是SetB中给定人员的最近邻居从SetA中的人员中选择。换句话说,第一个元素distances[0]
告诉您personB1与SetA中它的两个最近邻居的距离。 indicies[0]
告诉您这两个人的索引。
在此示例中:indicies[0] = [1, 0]
表示在SetA1中personB1最近的邻居是SetA [1] = personA2和SetA [0] = personA1。distances[0] = [1. 4.69041576]
告诉我们personB1和personA2之间的距离为1,personB1和personA1之间的距离为4.69041576(如果您手动计算欧几里得距离,则可以轻松地进行检查)。
几句话:
从对问题的描述中,您似乎只对SetB中一个人与SetA中一个人的最近邻居感兴趣(而不对最近的2个邻居感兴趣)。如果是这种情况,我建议在knn参数中将n = 2更改为n = 1。
请注意索引:在数据集中,标签从1开始(personA1,personA2,...),但在knn中,索引始终从0开始。当事情变得更加复杂时,这可能导致混乱,因为SetA [0] = personA1,请注意这一点。