Changeset 10633:fb05a6f3a235 in orange for Orange/clustering/hierarchical.py
 Timestamp:
 03/23/12 20:20:57 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/clustering/hierarchical.py
r10581 r10633 368 368 369 369 from Orange.utils import progress_bar_milestones, deprecated_keywords 370 370 371 371 import sys 372 372 … … 408 408 matrix = Orange.misc.SymMatrix(len(data)) 409 409 for i in range(len(data)): 410 for j in range(i +1):410 for j in range(i + 1): 411 411 matrix[i, j] = distance(data[i], data[j]) 412 root = HierarchicalClustering(matrix, linkage=linkage, progress_callback=(lambda value, obj=None: progress_callback(value *100.0/(2 if order else 1))) if progress_callback else None)412 root = HierarchicalClustering(matrix, linkage=linkage, progress_callback=(lambda value, obj=None: progress_callback(value * 100.0 / (2 if order else 1))) if progress_callback else None) 413 413 if order: 414 order_leaves(root, matrix, progress_callback=(lambda value: progress_callback(50.0 + value /2)) if progress_callback else None)414 order_leaves(root, matrix, progress_callback=(lambda value: progress_callback(50.0 + value / 2)) if progress_callback else None) 415 415 return root 416 416 … … 447 447 for a2 in range(a1): 448 448 matrix[a1, a2] = (1.0  orange.PearsonCorrelation(a1, a2, data, 0).r) / 2.0 449 root = orange.HierarchicalClustering(matrix, linkage=linkage, progress_callback=(lambda value, obj=None: progress_callback(value *100.0/(2 if order else 1))) if progress_callback else None)449 root = orange.HierarchicalClustering(matrix, linkage=linkage, progress_callback=(lambda value, obj=None: progress_callback(value * 100.0 / (2 if order else 1))) if progress_callback else None) 450 450 if order: 451 order_leaves(root, matrix, progressCallback=(lambda value: progress_callback(50.0 + value /2)) if progress_callback else None)451 order_leaves(root, matrix, progressCallback=(lambda value: progress_callback(50.0 + value / 2)) if progress_callback else None) 452 452 return root 453 453 454 454 clustering_features = \ 455 455 deprecated_keywords({"progressCallback":"progress_callback"})(clustering_features) 456 457 456 457 458 458 def cluster_to_list(node, prune=None): 459 459 """ Return a list of clusters down from the node of hierarchical clustering. … … 470 470 if prune: 471 471 if len(node) <= prune: 472 return [] 472 return [] 473 473 if node.branches: 474 474 return [node] + cluster_to_list(node.left, prune) + cluster_to_list(node.right, prune) … … 528 528 529 529 """ 530 530 531 531 objects = getattr(tree.mapping, "objects", None) 532 532 tree.mapping.setattr("objects", range(len(tree))) … … 534 534 ordering = {} 535 535 visited_clusters = set() 536 536 537 537 # def opt_ordering_recursive(tree): 538 538 # if len(tree)==1: … … 588 588 # with recursion_limit(sys.getrecursionlimit() + len(tree)): 589 589 # opt_ordering_recursive(tree) 590 590 591 591 def opt_ordering_iterative(tree): 592 if len(tree) ==1:592 if len(tree) == 1: 593 593 for leaf in tree: 594 594 M[tree, leaf, leaf] = 0 … … 596 596 # _optOrdering(tree.left) 597 597 # _optOrdering(tree.right) 598 598 599 599 Vl = set(tree.left) 600 600 Vr = set(tree.right) … … 612 612 ordered_k = sorted(other(w, Vrl, Vrr), key=lambda k: M[tree_right, w, k]) 613 613 k0 = ordered_k[0] 614 cur_min = 1e30000 614 cur_min = 1e30000 615 615 cur_m = cur_k = None 616 616 for m in ordered_m: … … 639 639 # progressCallback(100.0 * len(visited_clusters) / len(tree.mapping)) 640 640 # visited_clusters.add(tree) 641 641 642 642 subtrees = postorder(tree) 643 643 milestones = progress_bar_milestones(len(subtrees), 1000) 644 644 645 645 for i, subtree in enumerate(subtrees): 646 646 opt_ordering_iterative(subtree) … … 661 661 # right.swap() 662 662 # order_recursive(right, k, w) 663 663 664 664 def order_iterative(tree, u, w): 665 665 """ Order the tree based on the computed optimal ordering. … … 672 672 opt_uw[left] = (u, m) 673 673 opt_uw[right] = (k, w) 674 674 675 675 if left.branches and m not in left.right: 676 676 left.swap() 677 677 678 678 if right.branches and k not in right.left: 679 679 right.swap() 680 680 681 681 u, w = min([(u, w) for u in tree.left for w in tree.right], key=lambda (u, w): M[tree, u, w]) 682 682 683 683 ## print "M(v) =", M[tree, u, w] 684 684 685 685 # with recursion_limit(sys.getrecursionlimit() + len(tree)): 686 686 # order_recursive(tree, u, w) 687 687 688 688 order_iterative(tree, u, w) 689 689 690 690 691 691 # def _check(tree, u, w): … … 722 722 """ 723 723 node_count = iter(range(len(tree))) 724 724 725 725 if progress_callback is not None: 726 726 def p(*args): … … 728 728 else: 729 729 p = None 730 730 731 731 Orange.core.HierarchicalClusterOrdering(tree, matrix, progress_callback=p) 732 732 … … 735 735 736 736 order_leaves = order_leaves_cpp 737 737 738 738 """ 739 739 Matplotlib dendrogram ploting. This is mostly untested, … … 774 774 775 775 def add_cell(self, row, col, *args, **kwargs): 776 xy = (0, 0)776 xy = (0, 0) 777 777 778 778 cell = TableCell(xy, *args, **kwargs) … … 798 798 cells = numpy.array([[self._cells.get((row, col), None) for col in range(max(keys[:, 1] + 1))] \ 799 799 for row in range(max(keys[:, 0] + 1))]) 800 800 801 801 widths = self._get_column_widths(renderer) 802 802 x = self.xy[0] + numpy.array([numpy.sum(widths[:i]) for i in range(len(widths))]) 803 803 y = self.xy[1]  numpy.arange(cells.shape[0])  0.5 804 804 805 805 for i in range(cells.shape[0]): 806 806 for j in range(cells.shape[1]): … … 816 816 def _get_column_widths(self, renderer): 817 817 keys = numpy.array(self._cells.keys()) 818 widths = numpy.zeros(len(keys)).reshape((numpy.max(keys[:, 0]+1), numpy.max(keys[:,1]+1)))818 widths = numpy.zeros(len(keys)).reshape((numpy.max(keys[:, 0] + 1), numpy.max(keys[:, 1] + 1))) 819 819 fontSize = self._calc_fontsize(renderer) 820 820 for (row, col), cell in self._cells.items(): … … 823 823 transform = self._axes.transData.inverted() 824 824 x1, _ = transform.transform_point((0, 0)) 825 x2, _ = transform.transform_point((w + w *TableCell.PAD + 10, 0))825 x2, _ = transform.transform_point((w + w * TableCell.PAD + 10, 0)) 826 826 w = abs(x1  x2) 827 827 widths[row, col] = w … … 832 832 _, y1 = transform.transform_point((0, 0)) 833 833 _, y2 = transform.transform_point((0, 1)) 834 return min(max(int(abs(y1  y2) *0.85) ,4), self.max_fontsize)834 return min(max(int(abs(y1  y2) * 0.85) , 4), self.max_fontsize) 835 835 836 836 def get_children(self): … … 867 867 points.append(center) 868 868 self.plt.plot([tree.height, tree.height], [points[0][1], points[1][1]], color="black") 869 return (tree.height, (points[0][1] + points[1][1]) /2.0)869 return (tree.height, (points[0][1] + points[1][1]) / 2.0) 870 870 else: 871 871 return (0.0, tree.first) 872 872 draw_tree(self.root) 873 873 874 874 def plotHeatMap(self): 875 875 import numpy.ma as ma 876 876 import numpy 877 877 dx, dy = self.root.height, 0 878 fx, fy = self.root.height /len(self.data.domain.attributes), 1.0878 fx, fy = self.root.height / len(self.data.domain.attributes), 1.0 879 879 data, c, w = self.data.toNumpyMA() 880 data = (data  ma.min(data)) /(ma.max(data)  ma.min(data))881 x = numpy.arange(data.shape[1] + 1) /float(numpy.max(data.shape))882 y = numpy.arange(data.shape[0] + 1) /float(numpy.max(data.shape))*len(self.root)880 data = (data  ma.min(data)) / (ma.max(data)  ma.min(data)) 881 x = numpy.arange(data.shape[1] + 1) / float(numpy.max(data.shape)) 882 y = numpy.arange(data.shape[0] + 1) / float(numpy.max(data.shape)) * len(self.root) 883 883 self.heatmap_width = numpy.max(x) 884 884 … … 892 892 if self.plot_attr_names: 893 893 names = [attr.name for attr in self.data.domain.attributes] 894 self.plt.xticks(numpy.arange(data.shape[1] + 1) /float(numpy.max(data.shape)), names)894 self.plt.xticks(numpy.arange(data.shape[1] + 1) / float(numpy.max(data.shape)), names) 895 895 self.plt.gca().xaxis.tick_top() 896 896 for label in self.plt.gca().xaxis.get_ticklabels(): … … 909 909 ## tick.label2On = True 910 910 ## text = TableTextLayout(xy=(self.meshXOffset+1, len(self.root)), tableText=[[label] for label in self.labels]) 911 text = TableTextLayout(xy=(self.meshXOffset *1.005, len(self.root)  1), tableText=[[label] for label in self.labels])911 text = TableTextLayout(xy=(self.meshXOffset * 1.005, len(self.root)  1), tableText=[[label] for label in self.labels]) 912 912 text.set_figure(self.plt.gcf()) 913 913 self.plt.gca().add_artist(text) … … 916 916 def plotLabels(self): 917 917 ## table = TablePlot(xy=(self.meshXOffset*1.005, len(self.root) 1), axes=self.plt.gca()) 918 table = TablePlot(xy=(0, len(self.root)  1), axes=self.plt.gca())918 table = TablePlot(xy=(0, len(self.root)  1), axes=self.plt.gca()) 919 919 table.set_figure(self.plt.gcf()) 920 for i, label in enumerate(self.labels):920 for i, label in enumerate(self.labels): 921 921 table.add_cell(i, 0, width=1, height=1, text=label, loc="left", edgecolor="w") 922 922 table.set_zorder(0) 923 923 self.plt.gca().add_artist(table) 924 924 self.plt.gca()._set_artist_props(table) 925 925 926 926 def plot(self, filename=None, show=False): 927 927 self.plt.rcParams.update(self.params) … … 930 930 space = 0.01 if self.space_width == None else self.space_width 931 931 border = self.border_width 932 width = 1.0  2 *border933 height = 1.0  2 *border934 textLineHeight = min(max(h /len(self.labels), 4), self.plt.rcParams.get("font.size", 12))935 maxTextLineWidthEstimate = textLineHeight *labelLen932 width = 1.0  2 * border 933 height = 1.0  2 * border 934 textLineHeight = min(max(h / len(self.labels), 4), self.plt.rcParams.get("font.size", 12)) 935 maxTextLineWidthEstimate = textLineHeight * labelLen 936 936 ## print maxTextLineWidthEstimate 937 textAxisWidthRatio = 2.0 *maxTextLineWidthEstimate/w937 textAxisWidthRatio = 2.0 * maxTextLineWidthEstimate / w 938 938 ## print textAxisWidthRatio 939 939 labelsAreaRatio = min(textAxisWidthRatio, 0.4) if self.label_width == None else self.label_width 940 940 x, y = len(self.data.domain.attributes), len(self.data) 941 941 942 heatmapAreaRatio = min(1.0 *y/h*x/w, 0.3) if self.heatmap_width == None else self.heatmap_width943 dendrogramAreaRatio = 1.0  labelsAreaRatio  heatmapAreaRatio  2 *space if self.dendrogram_width == None else self.dendrogram_width942 heatmapAreaRatio = min(1.0 * y / h * x / w, 0.3) if self.heatmap_width == None else self.heatmap_width 943 dendrogramAreaRatio = 1.0  labelsAreaRatio  heatmapAreaRatio  2 * space if self.dendrogram_width == None else self.dendrogram_width 944 944 945 945 self.fig = self.plt.figure() 946 self.labels_offset = self.root.height /20.0947 dendrogramAxes = self.plt.axes([border, border, width *dendrogramAreaRatio, height])946 self.labels_offset = self.root.height / 20.0 947 dendrogramAxes = self.plt.axes([border, border, width * dendrogramAreaRatio, height]) 948 948 dendrogramAxes.xaxis.grid(True) 949 949 import matplotlib.ticker as ticker … … 953 953 dendrogramAxes.invert_xaxis() 954 954 self.plotDendrogram() 955 heatmapAxes = self.plt.axes([border + width *dendrogramAreaRatio + space, border, width*heatmapAreaRatio, height], sharey=dendrogramAxes)955 heatmapAxes = self.plt.axes([border + width * dendrogramAreaRatio + space, border, width * heatmapAreaRatio, height], sharey=dendrogramAxes) 956 956 957 957 heatmapAxes.xaxis.set_major_locator(ticker.NullLocator()) … … 959 959 heatmapAxes.yaxis.set_major_locator(ticker.NullLocator()) 960 960 heatmapAxes.yaxis.set_minor_locator(ticker.NullLocator()) 961 961 962 962 self.plotHeatMap() 963 labelsAxes = self.plt.axes([border + width *(dendrogramAreaRatio + heatmapAreaRatio + 2*space), border, width*labelsAreaRatio, height], sharey=dendrogramAxes)963 labelsAxes = self.plt.axes([border + width * (dendrogramAreaRatio + heatmapAreaRatio + 2 * space), border, width * labelsAreaRatio, height], sharey=dendrogramAxes) 964 964 self.plotLabels() 965 965 labelsAxes.set_axis_off() … … 974 974 if show: 975 975 self.plt.show() 976 977 976 977 978 978 """ 979 979 Dendrogram ploting using Orange.utils.render … … 994 994 995 995 """ 996 def __init__(self, tree, attr_tree = None, labels=None, data=None, width=None, height=None, tree_height=None, heatmap_width=None, text_width=None,996 def __init__(self, tree, attr_tree=None, labels=None, data=None, width=None, height=None, tree_height=None, heatmap_width=None, text_width=None, 997 997 spacing=2, cluster_colors={}, color_palette=ColorPalette([(255, 0, 0), (0, 255, 0)]), maxv=None, minv=None, gamma=None, renderer=EPSRenderer, **kwargs): 998 998 self.tree = tree … … 1006 1006 labels = [""] * len(tree) 1007 1007 self.labels = labels 1008 1008 1009 1009 # self.attr_labels = [str(attr.name) for attr in data.domain.attributes] if not attr_labels and data else attr_labels or [] 1010 1010 self.data = data … … 1025 1025 self.set_matrix_color_schema(color_palette, minv, maxv, gamma) 1026 1026 self.renderer = renderer 1027 1027 1028 1028 def set_matrix_color_schema(self, color_palette, minv, maxv, gamma=None): 1029 1029 """ Set the matrix color scheme. … … 1036 1036 self.maxv = maxv 1037 1037 self.gamma = gamma 1038 1038 1039 1039 def color_shema(self): 1040 vals = [float(val) for ex in self.data for val in ex if not val.isSpecial() and val.variable.varType ==orange.VarTypes.Continuous] or [0]1041 avg = sum(vals) /len(vals)1042 1040 vals = [float(val) for ex in self.data for val in ex if not val.isSpecial() and val.variable.varType == orange.VarTypes.Continuous] or [0] 1041 avg = sum(vals) / len(vals) 1042 1043 1043 maxVal = self.maxv if self.maxv else max(vals) 1044 1044 minVal = self.minv if self.minv else min(vals) 1045 1045 1046 1046 def _colorSchema(val): 1047 1047 if val.isSpecial(): 1048 1048 return self.color_palette(None) 1049 elif val.variable.varType ==orange.VarTypes.Continuous:1049 elif val.variable.varType == orange.VarTypes.Continuous: 1050 1050 r, g, b = self.color_palette((float(val)  minVal) / abs(maxVal  minVal), gamma=self.gamma) 1051 elif val.variable.varType ==orange.VarTypes.Discrete:1052 r = g = b = int(255.0 *float(val)/len(val.variable.values))1051 elif val.variable.varType == orange.VarTypes.Discrete: 1052 r = g = b = int(255.0 * float(val) / len(val.variable.values)) 1053 1053 return (r, g, b) 1054 1054 return _colorSchema 1055 1055 1056 1056 def layout(self): 1057 1057 height_final = False … … 1061 1061 height, height_final = self.height, True 1062 1062 heatmap_height = height  (tree_height + self.spacing if self.attr_tree else 0)  2 * self.horizontal_margin 1063 font_size = 1063 font_size = heatmap_height / len(self.labels) #self.font_size or (height  (tree_height + self.spacing if self.attr_tree else 0)  2 * self.horizontal_margin) / len(self.labels) 1064 1064 else: 1065 1065 font_size = self.font_size 1066 1066 heatmap_height = font_size * len(self.labels) 1067 1067 height = heatmap_height + (tree_height + self.spacing if self.attr_tree else 0) + 2 * self.horizontal_margin 1068 1068 1069 1069 text_width = self.text_width or max([len(label) for label in self.labels] + [0]) * font_size #max([self.renderer.string_size_hint(label) for label in self.labels]) 1070 1070 1071 1071 if self.width: 1072 1072 width = self.width … … 1075 1075 heatmap_width = len(self.data.domain.attributes) * heatmap_height / len(self.data) if self.data else 0 1076 1076 width = 2 * self.vertical_margin + tree_height + (heatmap_width + self.spacing if self.data else 0) + self.spacing + text_width 1077 1077 1078 1078 return width, height, tree_height, heatmap_width, heatmap_height, text_width, font_size 1079 1079 1080 1080 def plot(self, filename="graph.eps", **kwargs): 1081 1081 width, height, tree_height, heatmap_width, heatmap_height, text_width, font_size = self.layout() … … 1083 1083 1084 1084 heatmap_cell_width = 0.0 if not self.data else heatmap_width / len(self.data.domain.attributes) 1085 1085 1086 1086 self.renderer = self.renderer(width, height) 1087 1087 1088 1088 def draw_tree(cluster, root, treeheight, treewidth, color): 1089 1089 height = treeheight * cluster.height / root.height … … 1093 1093 center = draw_tree(branch, root, treeheight, treewidth, self.cluster_colors.get(branch, color)) 1094 1094 centers.append(center) 1095 self.renderer.draw_line(center[0], center[1], center[0], height, stroke_color =self.cluster_colors.get(branch, color))1096 1097 self.renderer.draw_line(centers[0][0], height, centers[1][0], height, stroke_color =self.cluster_colors.get(cluster, color))1095 self.renderer.draw_line(center[0], center[1], center[0], height, stroke_color=self.cluster_colors.get(branch, color)) 1096 1097 self.renderer.draw_line(centers[0][0], height, centers[1][0], height, stroke_color=self.cluster_colors.get(cluster, color)) 1098 1098 return (centers[0][0] + centers[1][0]) / 2.0, height 1099 1099 else: … … 1103 1103 self.renderer.rotate(90) 1104 1104 # print self.renderer.transform() 1105 draw_tree(self.tree, self.tree, tree_height, heatmap_height, self.cluster_colors.get(self.tree, (0, 0,0)))1105 draw_tree(self.tree, self.tree, tree_height, heatmap_height, self.cluster_colors.get(self.tree, (0, 0, 0))) 1106 1106 self.renderer.restore_render_state() 1107 1107 if self.attr_tree: … … 1110 1110 self.renderer.scale(1.0, 1.0) 1111 1111 # print self.renderer.transform() 1112 draw_tree(self.attr_tree, self.attr_tree, tree_height, heatmap_width, self.cluster_colors.get(self.attr_tree, (0, 0,0)))1112 draw_tree(self.attr_tree, self.attr_tree, tree_height, heatmap_width, self.cluster_colors.get(self.attr_tree, (0, 0, 0))) 1113 1113 self.renderer.restore_render_state() 1114 1114 1115 1115 self.renderer.save_render_state() 1116 1116 self.renderer.translate(self.vertical_margin + tree_height + self.spacing, self.horizontal_margin + (tree_height + self.spacing if self.attr_tree else 0)) … … 1123 1123 r, g, b = colorSchema(ex[jj]) 1124 1124 self.renderer.draw_rect(j * heatmap_cell_width, i * heatmap_cell_height, heatmap_cell_width, heatmap_cell_height, fill_color=(r, g, b), stroke_color=(255, 255, 255)) 1125 1125 1126 1126 self.renderer.translate(heatmap_width + self.spacing, heatmap_cell_height) 1127 1127 # print self.renderer.transform() … … 1132 1132 self.renderer.restore_render_state() 1133 1133 self.renderer.save(filename, **kwargs) 1134 1135 1134 1135 1136 1136 def dendrogram_draw(file, cluster, attr_cluster=None, labels=None, data=None, 1137 1137 width=None, height=None, tree_height=None, 1138 heatmap_width=None, text_width=None, 1138 heatmap_width=None, text_width=None, spacing=2, 1139 1139 cluster_colors={}, 1140 1140 color_palette=ColorPalette([(255, 0, 0), (0, 255, 0)]), … … 1193 1193 name, ext = os.path.splitext(file) 1194 1194 format = ext.lower().lstrip(".") or format 1195 1195 1196 1196 if format is None: 1197 1197 format = "png" 1198 1198 1199 1199 renderer = {"eps":EPSRenderer, "svg":SVGRenderer, "png":PILRenderer}.get(format, "png") 1200 1200 1201 1201 d = DendrogramPlot(cluster, attr_cluster, labels, data, width, height, 1202 1202 tree_height, heatmap_width, text_width, spacing, … … 1207 1207 else: 1208 1208 d.plot(file) 1209 1209 1210 1210 def postorder(cluster): 1211 1211 """ Return a post order list of clusters. … … 1222 1222 while stack: 1223 1223 cluster = stack.pop(0) 1224 1224 1225 1225 if cluster.branches: 1226 1226 if cluster in visited: … … 1233 1233 visited.add(cluster) 1234 1234 return order 1235 1236 1235 1236 1237 1237 def preorder(cluster): 1238 1238 """ Return a pre order list of clusters. … … 1252 1252 stack = cluster.branches + stack 1253 1253 return order 1254 1255 1254 1255 1256 1256 def dendrogram_layout(cluster, expand_leaves=False): 1257 1257 """ Return a layout of the cluster dendrogram on a 2D plane. The return … … 1292 1292 cluster_geometry[cluster] = (start, center, end) 1293 1293 result.append((cluster, (start, center, end))) 1294 1294 1295 1295 return result 1296 1296 1297 1297 def clone(cluster): 1298 1298 """ Clone a cluster, including it's subclusters. … … 1313 1313 node_clone.mapping = mapping 1314 1314 clones[node] = node_clone 1315 1315 1316 1316 return clones[cluster] 1317 1317 1318 1318 def pruned(cluster, level=None, height=None, condition=None): 1319 1319 """ Return a new pruned clustering instance. … … 1342 1342 prune(cluster, level, height, condition) 1343 1343 return cluster 1344 1345 1344 1345 1346 1346 def prune(cluster, level=None, height=None, condition=None): 1347 1347 """ Prune the clustering instance ``cluster`` in place. … … 1365 1365 if not any(arg is not None for arg in [level, height, condition]): 1366 1366 raise ValueError("At least one pruning argument must be supplied") 1367 1367 1368 1368 level_check = height_check = condition_check = lambda cl: False 1369 1369 cluster_depth = cluster_depths(cluster) 1370 1370 1371 1371 if level is not None: 1372 1372 level_check = lambda cl: cluster_depth[cl] >= level 1373 1373 1374 1374 if height is not None: 1375 1375 height_check = lambda cl: cl.height <= height … … 1377 1377 if condition is not None: 1378 1378 condition_check = condition 1379 1379 1380 1380 pruned_set = set() 1381 1381 1382 1382 def check_all(cl): 1383 1383 return any([check(cl) for check in [level_check, height_check, 1384 1384 condition_check]]) 1385 1385 1386 1386 for cluster in preorder(cluster): 1387 1387 if cluster not in pruned_set: … … 1391 1391 else: 1392 1392 pass 1393 1394 1393 1394 1395 1395 def cluster_depths(cluster): 1396 1396 """ Return a dictionary mapping :class:`HierarchicalCluster` instances to … … 1434 1434 iter_count = matrix.dim * (matrix.dim  1) / 2 1435 1435 milestones = progress_bar_milestones(iter_count, 100) 1436 1436 1437 1437 for count, ((i, a1), (j, a2)) in enumerate(_pairs(enumerate(attributes))): 1438 1438 matrix[i, j] = (1.0  orange.PearsonCorrelation(a1, a2, data, 0).r) / 2.0 1439 1439 if progress_callback and count in milestones: 1440 1440 progress_callback(100.0 * count / iter_count) 1441 1441 1442 1442 return matrix 1443 1443 1444 1444 1445 def _pairs(seq, same =False):1445 def _pairs(seq, same=False): 1446 1446 """ Return all pairs from elements of `seq`. 1447 1447 """ … … 1451 1451 for j in range(i + same, len(seq)): 1452 1452 yield seq[i], seq[j] 1453 1454 1453 1454 1455 1455 def joining_cluster(cluster, item1, item2): 1456 1456 """ Return the cluster where `item1` and `item2` are first joined … … 1474 1474 else: 1475 1475 return cluster 1476 1476 1477 1477 1478 1478 def cophenetic_distances(cluster): … … 1488 1488 """ 1489 1489 1490 mapping = cluster.mapping 1490 mapping = cluster.mapping 1491 1491 matrix = Orange.misc.SymMatrix(len(mapping)) 1492 1492 for cluster in postorder(cluster): … … 1496 1496 for idx2 in mapping[branch2.first: branch2.last]: 1497 1497 matrix[idx1, idx2] = cluster.height 1498 1498 1499 1499 else: 1500 1500 for ind1, ind2 in _pairs(mapping[cluster.first: cluster.last]): 1501 1501 matrix[ind1, ind2] = cluster.height 1502 1502 1503 1503 return matrix 1504 1504 1505 1505 1506 1506 def cophenetic_correlation(cluster, matrix): 1507 1507 """ Return the `cophenetic correlation coefficient … … 1525 1525 original = numpy.ravel(original) 1526 1526 return numpy.corrcoef(cophenetic, original)[0, 1] 1527 1528 1527 1528
Note: See TracChangeset
for help on using the changeset viewer.