我有一个目标阵列A,它代表NCEP再分析数据中的等压压力水平。我也有一个压力,在这个压力下,云被观察为长时间序列,B。
我正在寻找的是一个k-最近邻查找返回那些最近邻居的索引,类似于matlab中的knnsearch
,可以在python中表示相同,例如:indices, distance = knnsearch(A, B, n)
其中indices
是n
中每个值最接近的A
索引在B
,和distance
是B
的值与A
中最接近的值有多远,A
和B
可以有不同的长度(这是我在大多数解决方案中找到的瓶颈,到目前为止,我必须循环B
中的每个值以返回我的indices
和distance
)
import numpy as np
A = np.array([1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 10]) # this is a fixed 17-by-1 array
B = np.array([923, 584.2, 605.3, 153.2]) # this can be any n-by-1 array
n = 2
我想从indices, distance = knnsearch(A, B, n)
返回的是:
indices = [[1, 2],[4, 5] etc...]
其中923
中的A
与第一个A[1]=925
匹配,然后A[2]=850
中的584.2
和A
与第一个A[4]=600
匹配,然后A[5]=500
distance = [[72, 77],[15.8, 84.2] etc...]
其中72
表示B
中查询值与A
中最接近的值之间的距离,例如: distance[0, 0] == np.abs(B[0] - A[1])
我能想出的唯一解决方案是:
import numpy as np
def knnsearch(A, B, n):
indices = np.zeros((len(B), n))
distances = np.zeros((len(B), n))
for i in range(len(B)):
a = A
for N in range(n):
dif = np.abs(a - B[i])
ind = np.argmin(dif)
indices[i, N] = ind + N
distances[i, N] = dif[ind + N]
# remove this neighbour from from future consideration
np.delete(a, ind)
return indices, distances
array_A = np.array([1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 10])
array_B = np.array([923, 584.2, 605.3, 153.2])
neighbours = 2
indices, distances = knnsearch(array_A, array_B, neighbours)
print(indices)
print(distances)
收益:
[[ 1. 2.]
[ 4. 5.]
[ 4. 3.]
[10. 11.]]
[[ 2. 73. ]
[ 15.8 84.2]
[ 5.3 94.7]
[ 3.2 53.2]]
必须有一种方法来删除for循环,因为我需要性能,如果我的A和B数组包含数千个元素与许多最近的邻居...
请帮忙!谢谢 :)
第二个循环可以很容易地进行矢量化。最直接的方法是使用np.argsort
并选择对应于n个最小dif
值的索引。但是,对于大型数组,由于只应对n个值进行排序,因此最好使用np.argpartition
。
因此,代码看起来像这样:
def vector_knnsearch(A, B, n):
indices = np.empty((len(B), n))
distances = np.empty((len(B), n))
for i,b in enumerate(B):
dif = np.abs(A - b)
min_ind = np.argpartition(dif,n)[:n] # Returns the indexes of the 3 smallest
# numbers but not necessarily sorted
ind = min_ind[np.argsort(dif[min_ind])] # sort output of argpartition just in case
indices[i, :] = ind
distances[i, :] = dif[ind]
return indices, distances
正如在评论中所说,第一个循环也可以使用meshgrid删除,但是,额外使用内存和计算时间来构造meshgrid使得这种方法对于我尝试的维度来说更慢(对于大型数组,这可能会变得更糟并最终在内存错误)。此外,代码的可读性降低。总的来说,这可能会使这种方法更少pythonic。
def mesh_knnsearch(A, B, n):
m = len(B)
rng = np.arange(m).reshape((m,1))
Amesh, Bmesh = np.meshgrid(A,B)
dif = np.abs(Amesh-Bmesh)
min_ind = np.argpartition(dif,n,axis=1)[:,:n]
ind = min_ind[rng,np.argsort(dif[rng,min_ind],axis=1)]
return ind, dif[rng,ind]
并不重要的是将这个rng
定义为2d数组,以便重新调整a[rng[0],ind[0]]
,a[rng[1],ind[1]]
等,并保持阵列的尺寸,因为选择a[:,ind]
,它可以检索a[:,ind[0]]
,a[:,ind[1]]
等。