我有 2 个二维数组,其中列向量是特征向量。一个数组大小为 F x A,另一个数组大小为 F x B,其中 A << B。例如,对于 A = 2 和 F = 3(B 可以是任何值):
arr1 = np.array( [[1, 4],
[2, 5],
[3, 6]] )
arr2 = np.array( [[1, 4, 7, 10, ..],
[2, 5, 8, 11, ..],
[3, 6, 9, 12, ..]] )
我想为arr1
每个arr2
可能的arr2
. 列向量是相互独立的,所以我相信我应该计算每个列向量之间的距离arr1
和从i
到i + A
从的列向量集合之间的距离,arr2
并取这些距离的总和(虽然不确定)。
numpy 是否提供了一种有效的方法来执行此操作,还是我必须从第二个数组中获取切片,并使用另一个循环计算切片中每个列向量arr1
与切片中相应列向量之间的距离?
为清楚起见,使用上述数组的示例:
>>> magical_distance_func(arr1, arr2[:,:2])
[0, 10.3923..]
>>> # First, distance between arr2[:,:2] and arr1, which equals 0.
>>> # Second, distance between arr2[:,1:3] and arr1, which equals
>>> diff = arr1 - np.array( [[4,7],[5,8],[6,9]] )
>>> diff
[[-3, -3], [-3, -3], [-3, -3]]
>>> # this happens to consist only of -3's. Norm of each column vector is:
>>> norm1 = np.linalg.norm([:,0])
>>> norm2 = np.linalg.norm([:,1])
>>> # would be extremely good if this worked for an arbitrary number of norms
>>> totaldist = norm1 + norm2
>>> totaldist
10.3923...
当然,转置数组也很好,如果这意味着可以在这里使用 cdist。