Numpy 窗口上的距离矩阵(通过访问周围的帧)
我是scipy的新手,最近了解了Numpy 窗口上的距离矩阵(通过访问周围的帧),numpy,scipy,distance,Numpy,Scipy,Distance,我是scipy的新手,最近了解了cdist,我们可以通过自己的自定义函数来计算距离。但是,cdist仅计算单个点上的距离(因此,自定义函数必须计算两点之间的距离)。这适用于大多数情况,但在我的例子中,距离是计算一组点之间的距离。(您可以将其视为滑动窗口,例如大小为3的窗口)。举例说明: def f(*a, **kw): print(a, kw) return 0 # Consider it a set of points: [p0, p1, p2, ...] a = numpy
cdist
,我们可以通过自己的自定义函数来计算距离。但是,cdist
仅计算单个点上的距离(因此,自定义函数必须计算两点之间的距离)。这适用于大多数情况,但在我的例子中,距离是计算一组点之间的距离。(您可以将其视为滑动窗口,例如大小为3的窗口)。举例说明:
def f(*a, **kw):
print(a, kw)
return 0
# Consider it a set of points: [p0, p1, p2, ...]
a = numpy.array([[1,2,3],[10,11,12],[20,21,22],[30,31,32]])
# Consider it a set of points: [q0, q1, q2, ...]
b = numpy.array([[4,5,6],[14,15,16],[24,25,26],[34,35,36],[44,45,46]])
现在,如果我简单地执行scipy.space.distance.cdist(a,b,f)
,每次调用f
,每个数组中只有一个条目被传递到f
。因此,函数调用(和传递的值)类似于:
1st call: p0, q0
2nd call: p0, q1
3rd call: p0, q2
4th call: p0, q3
5th call: p0, q4 <--
6th call: p1, q0
7th call: p1, q1
...and so on...
第一次呼叫:p0,q0
第二次呼叫:p0,q1
第三次呼叫:p0,q2
第四次呼叫:p0,q3
第五次调用:p0,q4由于您想要计算所有距离,并且您正在使用一个自定义函数,因此使用cdist
除了方便之外,可能没有什么好处
首先,你需要打开你的观点窗口,我建议使用作为你的大步
:
import numpy as np
from numpy.lib.stride_tricks import as_strided
p = np.random.rand(4, 3)
q = np.random.rand(6, 3)
win = 3
win_p = as_strided(p, shape=(p.shape[0]-win+1, win)+p.shape[1:],
strides=p.strides[:1]*2+p.strides[1:])
win_q = as_strided(q, shape=(q.shape[0]-win+1, win)+q.shape[1:],
strides=q.strides[:1]*2+q.strides[1:])
现在:
>>> p
array([[ 0.8962062 , 0.62471689, 0.1754708 ],
[ 0.69104294, 0.66960955, 0.97012228],
[ 0.32023353, 0.08146304, 0.7663883 ],
[ 0.06276952, 0.1679428 , 0.9419452 ]])
>>> win_p
array([[[ 0.8962062 , 0.62471689, 0.1754708 ],
[ 0.69104294, 0.66960955, 0.97012228],
[ 0.32023353, 0.08146304, 0.7663883 ]],
[[ 0.69104294, 0.66960955, 0.97012228],
[ 0.32023353, 0.08146304, 0.7663883 ],
[ 0.06276952, 0.1679428 , 0.9419452 ]]])
从这里,您可以简单地执行显而易见的操作:
ps, qs = len(win_p), len(win_q)
dist = np.empty((ps, qs))
for j, pp in enumerate(win_p) :
for k, qq in enumerate(win_q) :
dist[j, k] = f(pp, qq)
正如我所说,如果您定义自己的距离函数,我认为这不会比cdist
慢
根据距离函数的外观,您可以尝试对事物进行矢量化。这可能需要创建一个中间数组shape(ps,win,3,qs,win,3)
,因此很可能需要大量内存。但是,请随意发布您对测量距离的想法,我很乐意看一看。因为您想要计算所有距离,并且您使用的是自定义函数,所以使用cdist
除了方便之外,可能没有什么好处
首先,你需要打开你的观点窗口,我建议使用作为你的大步
:
import numpy as np
from numpy.lib.stride_tricks import as_strided
p = np.random.rand(4, 3)
q = np.random.rand(6, 3)
win = 3
win_p = as_strided(p, shape=(p.shape[0]-win+1, win)+p.shape[1:],
strides=p.strides[:1]*2+p.strides[1:])
win_q = as_strided(q, shape=(q.shape[0]-win+1, win)+q.shape[1:],
strides=q.strides[:1]*2+q.strides[1:])
现在:
>>> p
array([[ 0.8962062 , 0.62471689, 0.1754708 ],
[ 0.69104294, 0.66960955, 0.97012228],
[ 0.32023353, 0.08146304, 0.7663883 ],
[ 0.06276952, 0.1679428 , 0.9419452 ]])
>>> win_p
array([[[ 0.8962062 , 0.62471689, 0.1754708 ],
[ 0.69104294, 0.66960955, 0.97012228],
[ 0.32023353, 0.08146304, 0.7663883 ]],
[[ 0.69104294, 0.66960955, 0.97012228],
[ 0.32023353, 0.08146304, 0.7663883 ],
[ 0.06276952, 0.1679428 , 0.9419452 ]]])
从这里,您可以简单地执行显而易见的操作:
ps, qs = len(win_p), len(win_q)
dist = np.empty((ps, qs))
for j, pp in enumerate(win_p) :
for k, qq in enumerate(win_q) :
dist[j, k] = f(pp, qq)
正如我所说,如果您定义自己的距离函数,我认为这不会比cdist
慢
根据距离函数的外观,您可以尝试对事物进行矢量化。这可能需要创建一个中间数组shape(ps,win,3,qs,win,3)
,因此很可能需要大量内存。但是,请随意发布您对测量距离的想法,我很乐意看一看。谢谢@Jamie,as___
功能看起来很有趣!我想我在浏览文档时错过了它,但我会再次搜索它:)谢谢@Jamie,as__crossed
功能看起来很有趣!我想我在浏览文档时错过了它,但我会再次搜索它:)