I have an array of (n_sample x 2) and I want to cluster them using KDTree in sklearn.neighbors.KDTree.
I have this sample piece of code:
from sklearn.neighbors import KDTree
import numpy as np
np.random.seed(0)
X = np.random.random((10, 2))
tree = KDTree(X, leaf_size=2)
Now I want to extract the points in the leaves of the tree so that each leaf can be a cluster. Points that are in the same leaf belong to the same cluster.
In the above example because the maximum leaf_size is 2, we'll have about 10 / 2 = 5 clusters.
What I desire is that given a point in X (e.g. X[0]) the tree can give me the index of the leaf of the tree that the points belongs to.