Skip to content

Commit

Permalink
ENH: optim in ward_tree
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
GaelVaroquaux committed Feb 19, 2012
1 parent 331eaf3 commit a2108ea
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions sklearn/cluster/hierarchical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a2108ea

Please sign in to comment.