From a2108ea9c85b8e2a7065fb37273cf047a81e619c Mon Sep 17 00:00:00 2001 From: GaelVaroquaux Date: Sun, 19 Feb 2012 17:18:49 +0100 Subject: [PATCH] ENH: optim in ward_tree Almost a factor of 2 in the unusual setting I am in, for which the following lines take most of the time: while parent[l] != l: l = parent[l] --- sklearn/cluster/hierarchical.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sklearn/cluster/hierarchical.py b/sklearn/cluster/hierarchical.py index c57637a87a5f8..8bcccd3acd293 100644 --- a/sklearn/cluster/hierarchical.py +++ b/sklearn/cluster/hierarchical.py @@ -143,16 +143,18 @@ def ward_tree(X, connectivity=None, n_components=None, copy=True): used_node[i] = used_node[j] = False # update the moments - for p in xrange(2): - moments[p][k] = moments[p][i] + moments[p][j] + moments[0][k] = moments[0][i] + moments[0][j] + moments[1][k] = moments[1][i] + moments[1][j] # update the structure matrix A and the inertia matrix coord_col = [] visited[:] = False visited[k] = True for l in set(A[i]).union(A[j]): - while parent[l] != l: - l = parent[l] + parent_l = parent[l] + while parent_l != l: + l = parent_l + parent_l = parent[l] if not visited[l]: visited[l] = True coord_col.append(l)