-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathhierarchical-example.py
77 lines (60 loc) · 1.76 KB
/
hierarchical-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Orange
m = [[],
[ 3],
[ 2, 4],
[17, 5, 4],
[ 2, 8, 3, 8],
[ 7, 5, 10, 11, 2],
[ 8, 4, 1, 5, 11, 13],
[ 4, 7, 12, 8, 10, 1, 5],
[13, 9, 14, 15, 7, 8, 4, 6],
[12, 10, 11, 15, 2, 5, 7, 3, 1]]
matrix = Orange.misc.SymMatrix(m)
root = Orange.clustering.hierarchical.HierarchicalClustering(matrix,
linkage=Orange.clustering.hierarchical.AVERAGE)
def print_clustering(cluster):
if cluster.branches:
return "(%s %s)" % (print_clustering(cluster.left), print_clustering(cluster.right))
else:
return str(cluster[0])
print print_clustering(root)
print root.height
root.mapping.objects = ["Ann", "Bob", "Curt", "Danny", "Eve",
"Fred", "Greg", "Hue", "Ivy", "Jon"]
print print_clustering(root)
for el in root.left:
print el
print print_clustering(root)
root.left.swap()
print print_clustering(root)
root.permute([1, 0])
print print_clustering(root)
def prune(cluster, h):
if cluster.branches:
if cluster.height < h:
cluster.branches = None
else:
for branch in cluster.branches:
prune(branch, h)
def print_clustering2(cluster):
if cluster.branches:
return "(%s %s)" % (print_clustering2(cluster.left), print_clustering2(cluster.right))
else:
return str(tuple(cluster))
prune(root, 5)
print print_clustering2(root)
def list_of_clusters0(cluster, alist):
if not cluster.branches:
alist.append(list(cluster))
else:
for branch in cluster.branches:
list_of_clusters0(branch, alist)
def list_of_clusters(root):
l = []
list_of_clusters0(root, l)
return l
print list_of_clusters(root)
root.mapping.objects = None
print list_of_clusters(root)
print root.left.last
print root.right.first