source: orange/orange/doc/modules/hclust-test2.py @ 8042:ffcb93bc9028

Revision 8042:ffcb93bc9028, 1.5 KB checked in by markotoplak, 3 years ago (diff)

Hierarchical clustering: also catch RuntimeError when importing matplotlib (or the documentation could not be built on server).

Line 
1import orange
2from orngClustering import *
3
4# data = orange.ExampleTable("doc//datasets//brown-selected.tab")
5data = orange.ExampleTable("iris")
6# data = orange.ExampleTable("doc//datasets//zoo.tab")
7# data = orange.ExampleTable("doc//datasets//titanic.tab")
8# 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]]
9# matrix = orange.SymMatrix(m)
10dist = orange.ExamplesDistanceConstructor_Euclidean(data)
11matrix = orange.SymMatrix(len(data))
12# matrix.setattr('items', data)
13for i in range(len(data)):
14    for j in range(i+1):
15        matrix[i, j] = dist(data[i], data[j])
16root = orange.HierarchicalClustering(matrix, linkage=orange.HierarchicalClustering.Average)
17# root.mapping.objects = [str(ex.getclass()) for ex in data]
18d = DendrogramPlot(root, data=data, labels=[str(ex.getclass()) for ex in data], width=500, height=2000)
19d.set_matrix_color_schema([(0, 255, 0), (255, 0, 0)], 0.0, 1.0)
20# d.setClusterColors({root.left:(0,255,0), root.right:(0,0,255)})
21d.plot("graph.png")
22print "Sum:", sum([matrix[root.mapping[i], root.mapping[i+1]] for i in range(len(root.mapping)-1)])
23orderLeaves(root, matrix)
24print "Sum:", sum([matrix[root.mapping[i], root.mapping[i+1]] for i in range(len(root.mapping)-1)])
25d = DendrogramPlot(root, data=data, labels=[str(ex.getclass()) for ex in data], width=500, height=2000)
26d.set_matrix_color_schema([(0, 255, 0), (255, 0, 0)], 0.0, 1.0)
27d.plot("tmp.png")
Note: See TracBrowser for help on using the repository browser.