numpy计算两二维数组距离

利用numpy可以很方便的计算两个二维数组之间的距离。二维数组之间的距离定义为:X的维度为(a,c),Y的维度为(b,c),Z为X到Y的距离数组,维度为(a,b)。且Z[0,0]是X[0]到Y[0]的距离。Z(m,n)为X[m]到Y[n]的距离。如下图所示。

这里写图片描述

代码如下:

#computer the distance between text point x and train point x_train
import numpy as np
X = np.random.random((3,2))
X_train = np.random.random((5,2))
print('X:')
print(X)
print('X_train:')
print(X_train)

dist = np.zeros((X.shape[0],X_train.shape[0]))
print('--------------------')
#way 1:use two loops
for i in range(X.shape[0]):
    for j in range(X_train.shape[0]):
        dist[i,j] = np.sum((X[i,:]-X_train[j,:])**2)
print('way 1 result:')
print(dist)

#way 2:use one loops
for i in range(X.shape[0]):
    dist[i,:] = np.sum((X_train-X[i,:])**2,axis=1)
print('--------------------')
print('way 2 result:')
print(dist)

#way 3:use no loops
dist = np.reshape(np.sum(X**2,axis=1),(X.shape[0],1))+ np.sum(X_train**2,axis=1)-2*X.dot(X_train.T)
print('--------------------')
print('way 3 result:')
print(dist)

结果:

X:
[[ 0.2892627   0.46569586]
 [ 0.75739842  0.33398985]
 [ 0.95221813  0.11192751]]
X_train:
[[ 0.10710814  0.27200357]
 [ 0.09082801  0.68378859]
 [ 0.62707459  0.81271073]
 [ 0.29597022  0.56699045]
 [ 0.42406439  0.93302285]]
--------------------
way 1 result:
[[ 0.07069699  0.08694076  0.23453619  0.01030558  0.23656601]
 [ 0.42671974  0.56667526  0.24615799  0.26720526  0.46995212]
 [ 0.73983525  1.06901804  0.59681545  0.6377436   0.95314395]]
--------------------
way 2 result:
[[ 0.07069699  0.08694076  0.23453619  0.01030558  0.23656601]
 [ 0.42671974  0.56667526  0.24615799  0.26720526  0.46995212]
 [ 0.73983525  1.06901804  0.59681545  0.6377436   0.95314395]]
--------------------
way 3 result:
[[ 0.07069699  0.08694076  0.23453619  0.01030558  0.23656601]
 [ 0.42671974  0.56667526  0.24615799  0.26720526  0.46995212]
 [ 0.73983525  1.06901804  0.59681545  0.6377436   0.95314395]]
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值