我有一个数据集,它有n行(观察值)和p列(特征):
import numpy as np
from scipy.spatial.distance import pdist, squareform
p = 3
n = 5
xOld = np.random.rand(n * p).reshape([n, p])
我想得到一个nxn
矩阵中这些点之间的距离,这个矩阵真的有n x (n-1)/2
唯一的值
sq_dists = pdist(xOld, 'sqeuclidean')
D_n = squareform(sq_dists)
现在假设我得到了N
额外的观察结果,并且想更新D_n
。一种非常低效的方法是:
N = 3
xNew = np.random.rand(N * p).reshape([N, p])
sq_dists = pdist(np.row_stack([xOld, xNew]), 'sqeuclidean')
D_n_N = squareform(sq_dists)
但是,考虑到n~10000和n~100,这将是多余的。我的目标是更有效地使用D_n
。为了做到这一点,我将D\n\n划分如下。我已经有了D_n
,可以计算B [N x N]
。然而,我想知道是否有一个好的方法来计算a(或转置),而不需要一堆for循环,并最终构造D_n_N
D_n (n x n) A [n x N]
A.T [N x n] B [N x N]
提前谢谢。你知道吗
很有趣的问题!在解决这个问题的过程中,我学到了一些新的东西。你知道吗
涉及的步骤:
首先,我们在这里介绍新的临时秘书处。所以,我们需要使用
cdist
来得到新旧pts之间的平方欧氏距离。在新的输出中,这些将被容纳在两个块中,一个位于旧距离的正下方,另一个位于旧距离的右侧。我们还需要计算新pts中的
pdist
,并将其square-formed
块放在新对角线区域的尾部。示意性地放置新的
D_n_N
如下所示:总而言之,实施过程将遵循以下思路——
运行时测试
接近-
计时-
只需使用cdist:
最后:
相关问题 更多 >
编程相关推荐