I am attempting to run a nearest neighbor sort in python. I have a dataFrame full of points, example:
x y
1 10 10.0
2 26 11.0
3 27 20.0
4 36 19.0
...
up to 1000 points. I am trying to sort these points by shortest distance to any unused point in the dataFrame. The code I am currently using to do this sort is shown below.
for j in range(0, len(data)-2):
minDist = 1000000
k = j+1
for i in range(k, len(data)-1):
#dist1 = distance.euclidean(j, i+1)
dist2 = distance.euclidean(j, i)
if(dist2<minDist):
minDist = dist2
print(minDist)
minI = data.iloc[i]
b, c = data.iloc[j+1].copy(), data.iloc[i].copy()
data.iloc[j+1],data.iloc[i] = c, b
However, when I run this code, my output data file only moves one data point, and it's not the correct data point, as shown here:
x y
1 10.0 10.0
2 624.0 436.0
3 26.0 11.0
4 27.0 20.0
I believe it is some problem with the nested for loops, however I am not sure. Are there any errors with my for loops? Or is it just a problem with how I'm approaching the problem in Python?