既然你还没有得到任何答案,我想我至少会贡献一些想法。我使用了一个 python kd 树模块来快速搜索最近的邻居点:
http
://code.google.com/p/python-kdtree/downloads/detail?name=
kdtree.py 只要它们是任意的点长度相同的尺寸。
我不确定你想如何应用“重要性”的权重,但这里只是关于如何使用 kdtree 模块至少让最接近给定人集的每个点的“人”的头脑风暴:
import numpy
from kdtree import KDTree
from itertools import chain
class PersonPoint(object):
def __init__(self, person, point, factor):
self.person = person
self.point = point
self.factor = factor
def __repr__(self):
return '<%s: %s, %0.2f>' % (self.person,
['%0.2f' % p for p in self.point], self.factor)
def __iter__(self):
return self.point
def __len__(self):
return len(self.point)
def __getitem__(self, i):
return self.point[i]
people = {}
for name in ('bill', 'john', 'mary', 'jenny', 'phil', 'george'):
factors = numpy.random.rand(6)
points = numpy.random.rand(6, 3).tolist()
people[name] = [PersonPoint(name, p, f) for p,f in zip(points, factors)]
bill_points = people['bill']
others = list(chain(*[people[name] for name in people if name != 'bill']))
tree = KDTree.construct_from_data(others)
for point in bill_points:
# t=1 means only return the 1 closest.
# You could set it higher to return more.
print point, "=>", tree.query(point, t=1)[0]
结果:
<bill: ['0.22', '0.64', '0.14'], 0.07> =>
<phil: ['0.23', '0.54', '0.11'], 0.90>
<bill: ['0.31', '0.87', '0.16'], 0.88> =>
<phil: ['0.36', '0.80', '0.14'], 0.40>
<bill: ['0.34', '0.64', '0.25'], 0.65> =>
<jenny: ['0.29', '0.77', '0.28'], 0.40>
<bill: ['0.24', '0.90', '0.23'], 0.53> =>
<jenny: ['0.29', '0.77', '0.28'], 0.40>
<bill: ['0.50', '0.69', '0.06'], 0.68> =>
<phil: ['0.36', '0.80', '0.14'], 0.40>
<bill: ['0.13', '0.67', '0.93'], 0.54> =>
<jenny: ['0.05', '0.62', '0.94'], 0.84>
我认为根据结果,您可以查看最常匹配的“人”,然后考虑权重。或者,也许您可以将结果中的重要因素加起来,然后选择评分最高的因素。这样,如果 mary 只匹配一次但有 10 个因素,而 phil 有 3 个匹配但总共只有 5 个,那么 mary 可能更相关?
我知道您有一个更强大的功能来创建索引,但它需要遍历集合中的每个点。