从向量列表中查找最近的向量|Python [英] Find Closest Vector from a List of Vectors | Python

查看:71
本文介绍了从向量列表中查找最近的向量|Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给出的话,请列出代表不同组的10个向量A的列表.然后,您有一个向量v1,v2,...,vn的时间序列,每个向量也是一个向量.我想知道是否有一种方法可以在A中为每个v1,v2,...,vn找到最接近"的向量,如果您定义一些距离度量?

If you are given say a list of 10 vectors, called A that represent different groups. Then you have a time series of vectors v1,v2,...,vn, each being a vector as well. I was wondering if there was a way to find the "closest" vector in A for each v1,v2,...,vn if you define some distance metric?

除了循环浏览和比较所有条目之外,还有其他快速的方法吗?

Is there a quick way to do this besides for looping through and just comparing all entries?

不,我不是在问如何做k均值或类似的事情.

No I am not asking how to do k-means or something like that.

推荐答案

您可以使用

You can use the spatial KDtree in scipy. It uses a fast tree algorithm to identify close by points for vectors of arbitrary dimension.

编辑:很抱歉,如果您正在寻找任意距离指标,例如结构可能仍然是一个选择.

Edit: sorry, if you are looking for arbitrary distance metrics, a Tree like structure might still be an option.

这里是一个例子:

>>> from scipy import spatial
>>> A = [[0,1,2,3,4], [4,3,2,1,0], [2,5,3,7,1], [1,0,1,0,1]]
>>> tree = spatial.KDTree(A)

这将使用A中的所有点设置KDTree,从而使您可以在其中进行快速空间搜索.这样的查询采用一个向量,并为其返回最接近的A:

This sets up the KDTree with all the points in A, allowing you to perform fast spatial searches within it. Such a query takes a vector and returns the closest neighbor in A for it:

>>> tree.query([0.5,0.5,0.5,0.5,0.5])
(1.1180339887498949, 3)

第一个返回值是最接近的邻居的距离,第二个返回值是它在A中的位置,这样您就可以像这样获得它:

The first return value is the distance of the closest neighbor and the second its position in A, such that you can obtain it for example like this:

>>> A[ tree.query([0.5,0.5,0.5,0.5,0.5])[1] ]
[1, 0, 1, 0, 1]

这篇关于从向量列表中查找最近的向量|Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆