def test_construct_from_graph(self): """ Check viability and correctness of LSTs constructed from a similarity graph. """ tree = dcl.construct_tree_from_graph(self.knn_graph, self.density, prune_threshold=self.gamma) self._check_tree_viability(tree) self._check_tree_correctness(tree)
## Construct a similarity graph and density estimate by hand. For this demo we # use the k-nearest neighbors similarity graph and density estimator that # DeBaCl uses by default, but this is the place to plug in custom similarity # graphs and density estimators. In particular, this functionality means the # user is not limited to tabular data or pre-set distance functions. If the # user can make a similarity graph and estimate a density (or pseudo-density), # then a level set tree can be estimated with DeBaCl. k = int(0.05 * n) knn_graph, radii = dcl.utils.knn_graph(X, k, method='kd-tree') density = dcl.utils.knn_density(radii, n, p, k) ## Build the level set tree. gamma = int(0.1 * n) tree = dcl.construct_tree_from_graph(knn_graph, density, prune_threshold=gamma, verbose=True) print tree ## Save the tree to disk and load it back (just for demo purposes). tree.save('crater_tree.lst') tree2 = dcl.load_tree('crater_tree.lst') print tree2 ## Get leaf clusters and leaf node indices. leaves = tree.get_leaf_nodes() labels = tree.get_clusters(method='leaf') ## Plot the tree, coloring the nodes used to cluster. fig = tree.plot(form='mass', color_nodes=leaves, colormap=plt.cm.Spectral)[0]
## Construct a similarity graph and density estimate by hand. For this demo we # use the k-nearest neighbors similarity graph and density estimator that # DeBaCl uses by default, but this is the place to plug in custom similarity # graphs and density estimators. In particular, this functionality means the # user is not limited to tabular data or pre-set distance functions. If the # user can make a similarity graph and estimate a density (or pseudo-density), # then a level set tree can be estimated with DeBaCl. k = int(0.05 * n) knn_graph, radii = dcl.utils.knn_graph(X, k, method='kd-tree') density = dcl.utils.knn_density(radii, n, p, k) ## Build the level set tree. gamma = int(0.1 * n) tree = dcl.construct_tree_from_graph(knn_graph, density, prune_threshold=gamma, verbose=True) print tree ## Save the tree to disk and load it back (just for demo purposes). tree.save('crater_tree.lst') tree2 = dcl.load_tree('crater_tree.lst') print tree2 ## Get leaf clusters and leaf node indices. leaves = tree.get_leaf_nodes() labels = tree.get_clusters(method='leaf')