numpy python:向量化距离函数以计算尺寸为(m,3)的2个矩阵的成对距离 [英] numpy python: vectorize distance function to calculate pairwise distance of 2 matrix with a dimension of (m, 3)

查看:159
本文介绍了numpy python:向量化距离函数以计算尺寸为(m,3)的2个矩阵的成对距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个numpy数组A和B.A的形状为(m,3),B的形状为(n,3).

I have two numpy arrays A and B. Shape of A is (m,3) and shape of B is (n, 3).

这些矩阵如下:

A
#output
array([[  9.227,  -4.698, -95.607],
   [ 10.294,  -4.659, -94.606],
   [ 11.184,  -5.906, -94.675],
   ...,
   [ 19.538, -91.572, -45.361],
   [ 20.001, -92.655, -45.009],
   [ 19.271, -92.726, -45.79 ]])

因此,它包含每行3D点的坐标x,y,z. B遵循相同的格式.

So it contains for each row the coordinates x,y,z of a 3D point. B follows the same format.

我有这个功能(np是numpy):

I have this function (np is numpy):

def compute_dist(point1, point2):
    squared = (point1-point2)**2
    return (np.sqrt(np.sum(squares)))

我想使用矢量化函数计算A和B之间的成对距离.

I want to compute a pairwise distance between A and B by using a vectorized function.

我尝试:

 v = np.vectorize(compute_dist)
 v(A, B)
 #output
 matrix([[37.442, 42.693, 72.705],
    [37.442, 42.693, 72.705],
    [37.442, 42.693, 72.705],
    ...,
    [37.442, 42.693, 72.705],
    [37.442, 42.693, 72.705],
    [37.442, 42.693, 72.705]])

即使我阅读了文档,我也不知道如何使用向量化.如何计算包含A和B之间成对距离的矩阵?我知道有 scipy.distance.cdist ,但我想自己通过 np.vectorize 来完成.

I don't understand how to use vectorize even if I read the doc. How can I compute a matrix which contains pairwise distance between A and B? I know there is scipy.distance.cdist but I want to do it myself with np.vectorize.

我不在乎输出的格式(列表,数组,矩阵...).最后,我只想找到最小的距离.

I don't care about the format of the output (list, array, matrix ...). At the end I just want to find the minimal distance.

推荐答案

您可以使用np.newaxis扩展两个数组AB的尺寸以启用广播,然后做你的计算.

You can use np.newaxis to expand the dimensions of your two arrays A and B to enable broadcasting and then do your calculations.

成对距离意味着A (m, 3)中的每个点都应与B (n, 3)中的每个点进行比较.这导致距离的(m, n)矩阵. 使用numpy的人可以使用广播来达到想要的结果. 通过使用A=A[:, np.newaxis, :]B=B[np.newaxis, :, :] 形状分别是A (m, 1, 3)B(1, n, 3). 如果您随后自动执行类似C = A-B numpy的计算 广播.这意味着您将获得B的所有n列的A的所有m行的副本 以及A的所有m行的B的所有n列的副本.

Pairwise distance means every point in A (m, 3) should be compared to every point in B (n, 3). This results in a (m, n) matrix of distances. With numpy one can use broadcasting to achieve the wanted result. By using A=A[:, np.newaxis, :] and B=B[np.newaxis, :, :] the resulting shapes are A (m, 1, 3) and B(1, n, 3) respectivley. If you then perform a calculation like C = A-B numpy automatically broadcasts. This means you get a copy of all m rows of A for all n columns of B and a copy of all n columns of B for all m rows of A.

  A (m, 1, 3)
- B (1, n, 3)
--------------
= C (m, n, 3)

要获取距离矩阵,可以使用numpy.linalg.norm():

To get the distance matrix you can than use numpy.linalg.norm():

import numpy as np
m = 10
n = 12
A = np.random.random((m, 3))
B = np.random.random((n, 3))

# Add newaxis on seconbd axis of A and on first axis on B
# shape: (m, n, 3) = (m, 1, 3) - (1, n, 3)
C = A[:, np.newaxis, :] - B[np.newaxis, :, :]

C = np.linalg.norm(C, axis=-1)
# shape: (m, n)

这篇关于numpy python:向量化距离函数以计算尺寸为(m,3)的2个矩阵的成对距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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