Advertisement
bidesh23

KdTree

Dec 7th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.neighbors import KDTree
  3. rng = np.random.RandomState(0)
  4. Y = rng.random_sample((5, 3))
  5. # ~ blob_num = np.loadtxt("blobNumber.txt")
  6.  
  7. global X
  8.  
  9. file1 = open("blobNumber.txt","r")
  10. blob_num_1 = file1.read()
  11. word = blob_num_1.split()
  12. blob_num = int(word[0])
  13. print blob_num
  14. file1.close()
  15.  
  16. X = np.zeros((blob_num,3),dtype=float)
  17. data = np.loadtxt("blobCoordinates.txt")
  18. x = data[:,0]
  19. y = data[:,1]
  20. z = data[:,2]
  21.  
  22. for i in range(np.size(x)):
  23.     X[i][0] = x[i]
  24.     X[i][1] = y[i]
  25.     X[i][2] = z[i]
  26.  
  27. print X[[10]]
  28. tree = KDTree(X, leaf_size=2)
  29.  
  30. file1 = open("neighbourList.txt","w")#write mode
  31.  
  32. for i in range(np.size(x)):
  33.     dist, ind = tree.query(X[[i]], k=2)                
  34.     print(ind)  # indices of 3 closest neighbors
  35.     file1.write(ind)
  36.  
  37. file1.close()
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement