删除二维 numpy 数组中彼此特定距离的点

我有以下二维数组:


MyData = array([x = [ 82, 210, 203, 234, 135,  92, 176, 146, 246,  35, 257, 227, 258,

    132,  31, 160, 269,  24, 248, 274, 281, 279,  71,  21, 188, 163,

    243],

   y = [ 15,  16,  18,  18,  19,  21,  23,  29,  35,  47,  50,  53,  60,

     64,  67,  69,  77,  88,  89,  91, 105, 115, 138, 175, 178, 205,

    207]], dtype=int64)

我想删除彼此之间处于特定欧几里德距离的所有 x 和 y 对。


例如,此处 (210,16) 和 (203,18) 的距离小于 10,均应删除。


然而,在这样做之前,我首先需要所有距离(这很容易),然后我必须删除它们。


所以,我创建了这个距离矩阵:


distance = np.zeros((27,27))

for i in range (0 , 27):

    for j in range (0 , 27):

        dist= np.linalg.norm(MyData[:,i] - MyData[:,j])

        distance[i,j] = dist

然后使用以下条件,我找到了我的索引:


indx = (np.where((distance >  0) & (distance <= 10)))[0]

indy = (np.where((distance > 0) & (distance <= 10)))[1]

现在,我不确定如何使用从 indx 和 indy 获得的索引来过滤“MyData”。


喵喔喔
浏览 116回答 1
1回答

Helenr

numpy的解决方案首先准备数据import numpy as npx = np.array([[ 82, 210, 203, 234, 135,&nbsp; 92, 176, 146, 246,&nbsp; 35, 257, 227, 258,&nbsp; &nbsp; 132,&nbsp; 31, 160, 269,&nbsp; 24, 248, 274, 281, 279,&nbsp; 71,&nbsp; 21, 188, 163,&nbsp; &nbsp; 243],&nbsp; &nbsp;[ 15,&nbsp; 16,&nbsp; 18,&nbsp; 18,&nbsp; 19,&nbsp; 21,&nbsp; 23,&nbsp; 29,&nbsp; 35,&nbsp; 47,&nbsp; 50,&nbsp; 53,&nbsp; 60,&nbsp; &nbsp; &nbsp;64,&nbsp; 67,&nbsp; 69,&nbsp; 77,&nbsp; 88,&nbsp; 89,&nbsp; 91, 105, 115, 138, 175, 178, 205,&nbsp; &nbsp; 207]]).T计算所有点对的距离a,b = np.tril_indices(27, -1)diss = np.linalg.norm(x[b] - x[a], axis=1)找到距离小于阈值的点distance = 10near = x[np.unique(np.concatenate([b[diss < distance], a[diss < distance]]))]然后我们可以绘制点import matplotlib.pyplot as pltplt.scatter(x[:,0], x[:,1])plt.scatter(near[:,0], near[:,1]);删除点remove = np.delete(x,np.unique(np.concatenate([b[diss < distance], a[diss < distance]])), axis=0)plt.scatter(remove[:,0], remove[:,1]);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python