Last active
October 17, 2016 16:31
-
-
Save fogonthedowns/3e4fdf7ced7cbe4ede5c402e2119122b to your computer and use it in GitHub Desktop.
return the leaf paths from scipy.cluster ClusterNode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| linkage_matrix = | |
| [[2, 3, 0.06571365, 2], [0, 10, 0.07951425, 2], [5, 6, 0.09405724, 2], [11, 13, 0.10182075, 3], [1, 12, 0.12900146, 3], [14, 15, 0.13498948, 5], [8, 9, 0.16806049, 2], [7, 16, 0.1887918, 4], [17, 19, 0.2236683, 9], [18, 20, 0.29471335, 11], [4, 21, 0.45878, 12]] | |
| from scipy.cluster import hierarchy | |
| a = hierarchy.to_tree(linkage_matrix) | |
| def parse_tree(tree, path): | |
| path = path | |
| if path ==[]: | |
| path.append(str(tree.get_id())) | |
| if tree.is_leaf() is False: | |
| left = tree.get_left() | |
| left_id = str(left.get_id()) | |
| if left.is_leaf() is False: | |
| path.append(left_id) | |
| parse_tree(left, path) | |
| path.pop() | |
| else: | |
| parse_tree(left, path) | |
| right = tree.get_right() | |
| right_id = str(right.get_id()) | |
| if right.is_leaf() is False: | |
| path.append(right_id) | |
| parse_tree(right, path) | |
| else: | |
| path.append(str(tree.get_id())) | |
| print(('.').join(path)) | |
| path.pop() | |
| parse_tree(a, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment