title: Sorting an array by looking at another array in python (deep learning purpose)
body: <p>I am actually doing deep learning on white spots images. Thanks to my model I predict a list of points with [x, y] coordinates. I have also a list of the target points with [x', y'] coordinates.
  My goal is to compute the euclidean distance between the predicted and the target coordinates.
  The length of the list of my predictions is smaller than the list of the targets.</p>
  <p>Predictions</p>
  <pre><code>[[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]]
  </code></pre>
  <p>Targets</p>
  <pre><code>[[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]]
  </code></pre>
  <p>What I want is to sort the arrays of Targets by having their predictions at the same position in the target list of coordinates and hence, having the non-predicted coordinates at the end of the target list.</p>
  <p>I have already tried to compute the euclidean distance for each element of the targets by looking at the predictions and taking the lower distance under a threshold to sort the array. The problem I have is that when you have 2 predicted points close to each other and 2 target points related to that prediction, and one of the predicted points is closer to both of the targets than the other prediction, my code does not work.</p>
  <pre><code># 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 &lt; 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
  </code></pre>
  <p><em>a_sorted is the list of predictions and b_sorted is the list of targets.</em></p>
  <p>Here I know that my code only returned a vector with the targets that have a prediction but it is the first step.</p>
  <p>The code work in general but here I gave an example where it is not working. The point at [116, 52] is the predictions of the target at [117, 51] but my output is:</p>
  <pre><code>[[11, 25], [13, 82], [27, 42], [32, 107], [37, 72], [38, 120], [40, 29], [55, 89], [62, 12], [81, 120], [83, 67], [86, 108], [94, 23], [107, 56], [107, 39], [111, 89], [115, 36], [118, 56], [116, 120], [92, 1], [56, 124], [89, 124], [61, 122], [117, 51]]
  </code></pre>
  <p>Thank you for your help.</p>