My solution is to install the kdtree package. The package can be found in https://github.com/stefankoegl/kdtree. It is pretty straightforward to use. Let me share my example code of N nearest neighbor search algorithm. All points here are in a 3D dimension with N = 2. The average distance to its nearest neighbors is printed out for each point.
import kdtree import math import numpy data = ((1, 0, 0),(2, 0, 0), (2, 0, 0), (3, 0, 0)); # Set up kd tree myTree = kdtree.create(dimensions=3) for i in range(0, len(data)): tempData = data[i]; myTree.add((float(tempData[0]), float(tempData[1]), float(tempData[2]))); # Nearest neighbor search minDist = [] NN = 2; # Number of neighbors to search for i in range(0, len(data)): searchResult = myTree.search_knn(data[i], NN+1); # Find two closest neighbors including itself sumDist = 0; for p in range(0, NN): sumDist = sumDist + math.sqrt(searchResult[p+1][1]); avgDist = sumDist/NN; print avgDist
No comments:
Post a Comment