def closestNeighbor(query, embedding_array, normed=False, top_k=1):
'''Gets the index of the closest neighbor of embedding_array
to the query point. Distance metric is cosine.
SLOW. DO NOT USE THIS FOR RAPID COMPUTATION.
'''
embedding_array = numpy.array(embedding_array)
if not normed:
embedding_array = numpy.array([
(embedding_array[i] / numpy.linalg.norm(embedding_array[i]))
for i in range(embedding_array.shape[0])
])
## assuming embeddings are unit-normed by this point;
## norm(query) is a constant factor, so we can ignore it
dists = numpy.array([
numpy.dot(query, embedding_array[i])
for i in range(embedding_array.shape[0])
])
sorted_ixes = numpy.argsort(-1 * dists)
return sorted_ixes[:top_k]
评论列表
文章目录