def rearrange(X, optimal = True, method = "average"): metric_kwargs = {} Y = squareform(X, force="tovector") Z = [(int(l), int(r), max(0., d), int(n)) for (l, r, d, n) in linkage(Y, method=method, metric=None)] leaves = list(leaves_list(Z)) N = len(leaves) root = len(Z)+N-1 assert len(X) == N # bar-joseph optimal ordering if optimal: import barjoseph leaves = barjoseph.optimal(root, **{ "S": lambda i, j: exp(-X[i][j]), "left": lambda i: None if i < N else Z[i-N][0], "right": lambda i: None if i < N else Z[i-N][1], "is_leaf": lambda i: i < N, "is_empty": lambda v: v is None, }) assert list(sorted(leaves)) == list(range(N)) return leaves
[0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0, 0]]) y = pdist(data, metric=metric) Z = linkage(y, method=method, metric=metric) dendrogram(Z) Z = [(int(l), int(r), max(0., s), int(n)) for (l, r, s, n) in Z] # cleaning leaves = list(leaves_list(Z)) count = len(leaves) root = len(Z) + count - 1 X = squareform(y) assert len(X) == count from utils import memoise # bar-joseph optimal ordering ################################################ from barjoseph import optimal leaves = optimal( root, **{ "S": lambda i, j: X[i][j], "left": lambda i: None if i < count else Z[i - count][0], "right": lambda i: None if i < count else Z[i - count][1], "is_leaf": lambda i: i < count, "is_empty": lambda v: v is None, }) print leaves
Z = [(int(l), int(r), max(0., d), int(n)) for (l, r, d, n) in linkage(Y, method=method, metric=None)] leaves = list(leaves_list(Z)) N = len(leaves) root = len(Z)+N-1 assert len(X) == N # bar-joseph optimal ordering if optimal: import barjoseph leaves = barjoseph.optimal(root, **{ "S": lambda i, j: exp(-X[i][j]), "left": lambda i: None if i < N else Z[i-N][0], "right": lambda i: None if i < N else Z[i-N][1], "is_leaf": lambda i: i < N, "is_empty": lambda v: v is None, }) assert list(sorted(leaves)) == list(range(N)) # output for leaf in leaves: print(format(names[leaf], "%ss" % max(len(name) for name in names)), *vectors[leaf], sep=sep)
y = pdist(data, metric=metric) Z = linkage(y, method=method, metric=metric) dendrogram(Z) Z = [(int(l), int(r), max(0., s), int(n)) for (l, r, s, n) in Z] # cleaning leaves = list(leaves_list(Z)) count = len(leaves) root = len(Z)+count-1 X = squareform(y) assert len(X) == count from utils import memoise # bar-joseph optimal ordering ################################################ from barjoseph import optimal leaves = optimal(root, **{ "S": lambda i, j: X[i][j], "left": lambda i: None if i < count else Z[i-count][0], "right": lambda i: None if i < count else Z[i-count][1], "is_leaf": lambda i: i < count, "is_empty": lambda v: v is None, }) print leaves