通过查看python中的另一个数组对数组进行排序(用于深入学习)
实际上,我正在深入学习白点图像。由于我的模型,我预测了一系列具有[x,y]坐标的点。我还有一个带有[x',y']坐标的目标点列表。 我的目标是计算预测坐标和目标坐标之间的欧氏距离。 我的预测列表的长度小于目标列表的长度 预言通过查看python中的另一个数组对数组进行排序(用于深入学习),python,arrays,sorting,deep-learning,Python,Arrays,Sorting,Deep Learning,实际上,我正在深入学习白点图像。由于我的模型,我预测了一系列具有[x,y]坐标的点。我还有一个带有[x',y']坐标的目标点列表。 我的目标是计算预测坐标和目标坐标之间的欧氏距离。 我的预测列表的长度小于目标列表的长度 预言 [[11, 25], [13, 82], [27, 42], [32, 107], [37, 72], [38, 120], [40, 29], [55, 89], [62, 12], [80, 20], [83, 67], [ 86, 108], [94, 23], [1
[[11, 25], [13, 82], [27, 42], [32, 107], [37, 72], [38, 120], [40, 29], [55, 89], [62, 12], [80, 20], [83, 67], [ 86, 108], [94, 23], [107, 56], [108, 38], [111, 89], [115, 36], [116, 52], [116, 121], [119, 57]]
目标
[[11, 25], [13, 82], [27, 42], [32, 107], [37, 72], [38, 120], [40, 29], [55, 89], [56, 124], [61, 122], [62, 12], [ 81, 120], [83, 67], [86, 108], [89, 124], [92, 1], [94, 23], [107, 56], [107, 39], [111, 89], [115, 36], [116, 120], [117, 51], [118, 56]]
我想对目标数组进行排序,方法是将它们的预测放在目标坐标列表中的同一位置,从而将非预测坐标放在目标列表的末尾
我已经试着通过查看预测并在阈值下取较低的距离来排序数组,来计算目标的每个元素的欧氏距离。我的问题是,如果有两个预测点彼此接近,并且有两个目标点与该预测相关,并且其中一个预测点比另一个预测点更接近这两个目标,那么我的代码就无法工作
# Align predictions targets in increasing order for the x parameter, the targets not predicted will be at the end of the target list
def align_by_dist(a_sorted, b_sorted):
for i in range(len(b_sorted)):
D = []
for j in range(len(a_sorted)):
dist = distance.euclidean(a_sorted[j], b_sorted[i])
if dist < 7: # Select a treshold for the euclidean distance
D.append(dist) # Append the euclidean distance which is lower than the treshold
if dist == min(D): # Check if it is the lower euclidean distance between the close micro-bubb
b_sorted[j], b_sorted[i] = b_sorted[i], b_sorted[j]
# If it is the lowest euclidean distance, put at the same level the target micro-bubble
# and its closest predicted micro-bubble
return a_sorted, b_sorted
谢谢您的帮助。我可能已经成功地修改了我的代码:
def align_by_dist(A, B):
for i in range(len(A)):
D = [] # This list will contain the index where the euclidean distance is lower than the threshold
for j in range(len(B)):
dist = distance.euclidean(A[i], B[j]) # Compute the euclidean distance between a target at index j and a prediction at index I
if dist <= 4: # Select a treshold for the euclidean distance
D.append(np.array([dist,j,i])) # Append the euclidean distance and the index of the target and prediction vector
if D: # If we find an euclidean distance lower than the threshold we can now sort for the index i the list of prediction
D_sorted = sorted(D,key=lambda elem:elem[0]) # We sort the array of distance lower than the threshold to get the minimum distance for the index I
value = D_sorted[0]
B[value[2].astype(np.int64)], B[value[1].astype(np.int64)] = B[value[1].astype(np.int64)], B[value[2].astype(np.int64)] # We updated the target list position
A[value[2].astype(np.int64)] = [1000000,1000000] # We set the value of the predictions very high to not have duplicates when sorting the targets coordinates
return B
def align_by_dist(A、B):
对于范围内的i(len(A)):
D=[]#此列表将包含欧几里德距离低于阈值的索引
对于范围内的j(len(B)):
dist=距离。欧几里德(A[i],B[j])#计算索引j处的目标和索引i处的预测之间的欧几里德距离
如果区
def align_by_dist(A, B):
for i in range(len(A)):
D = [] # This list will contain the index where the euclidean distance is lower than the threshold
for j in range(len(B)):
dist = distance.euclidean(A[i], B[j]) # Compute the euclidean distance between a target at index j and a prediction at index I
if dist <= 4: # Select a treshold for the euclidean distance
D.append(np.array([dist,j,i])) # Append the euclidean distance and the index of the target and prediction vector
if D: # If we find an euclidean distance lower than the threshold we can now sort for the index i the list of prediction
D_sorted = sorted(D,key=lambda elem:elem[0]) # We sort the array of distance lower than the threshold to get the minimum distance for the index I
value = D_sorted[0]
B[value[2].astype(np.int64)], B[value[1].astype(np.int64)] = B[value[1].astype(np.int64)], B[value[2].astype(np.int64)] # We updated the target list position
A[value[2].astype(np.int64)] = [1000000,1000000] # We set the value of the predictions very high to not have duplicates when sorting the targets coordinates
return B