# source:orange/Orange/testing/regression/tests_20/reference_hclust.py@9952:986e9cd806f4

Revision 9952:986e9cd806f4, 1.9 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Tests moved and renamed from orange 20.

Line
1import orange, time
2
3def repTime(msg):
4    #print "%s: %s" % (time.asctime(), msg)
5    pass
6
7def callback(f, o):
8    print int(round(100*f)),
9
11data = orange.ExampleTable("iris")
12
13repTime("Computing distances")
14matrix = orange.SymMatrix(len(data))
15matrix.setattr("objects", data)
16distance = orange.ExamplesDistanceConstructor_Euclidean(data)
17for i1, ex1 in enumerate(data):
18    for i2 in range(i1+1, len(data)):
19        matrix[i1, i2] = distance(ex1, data[i2])
20
22clustering = orange.HierarchicalClustering()
24clustering.overwriteMatrix = 1
25root = clustering(matrix)
26
27repTime("Done.")
28
29def prune(cluster, togo):
30    if cluster.branches:
31        if togo<0:
32            cluster.branches = None
33        else:
34            for branch in cluster.branches:
35                prune(branch, togo-cluster.height)
36
37def listOfClusters0(cluster, alist):
38    if not cluster.branches:
39        alist.append(list(cluster))
40    else:
41        for branch in cluster.branches:
42            listOfClusters0(branch, alist)
43
44def listOfClusters(root):
45    l = []
46    listOfClusters0(root, l)
47    return l
48
49prune(root, 1.4)
50
51for n, cluster in enumerate(listOfClusters(root)):
52    print "\n\n*** Cluster %i ***\n" % n
53    for ex in cluster:
54        print ex
55
56for cluster in listOfClusters(root):
57    dist = orange.getClassDistribution(cluster)
58    for e, d in enumerate(dist):
59        print "%s: %3.0f   " % (data.domain.classVar.values[e], d),
60    print
61
62def listOfClustersT0(cluster, alist):
63    if not cluster.branches:
64        alist.append(orange.ExampleTable(cluster))
65    else:
66        for branch in cluster.branches:
67            listOfClustersT0(branch, alist)
68
69def listOfClustersT(root):
70    l = []
71    listOfClustersT0(root, l)
72    return l
73
74for t in listOfClustersT(root):
75    print type(t), "of length", len(t)
Note: See TracBrowser for help on using the repository browser.