@@ -54,17 +54,29 @@ def ward_tree(X, connectivity=None, n_components=None, copy=True,
5454 Make a copy of connectivity or work inplace. If connectivity
5555 is not of LIL type there will be a copy in any case.
5656
57+ n_clusters : int (optional)
58+ Stop early the construction of the tree at n_clusters. This is
59+ useful to decrease computation time if the number of clusters is
60+ not small compared to the number of samples. In this case, the
61+ complete tree is not computed, thus the 'children' output is of
62+ limited use, and the 'parents' output should rather be used.
63+ This option is valid only when specifying a connectivity matrix.
64+
5765 Returns
5866 -------
59- children : list of pairs. Lenght of n_nodes
60- list of the children of each nodes.
61- Leaves of the tree have empty list of children.
67+ children : 2D array, shape ( n_nodes, 2)
68+ list of the children of each nodes.
69+ Leaves of the tree have empty list of children.
6270
6371 n_components : sparse matrix.
6472 The number of connected components in the graph.
6573
6674 n_leaves : int
6775 The number of leaves in the tree
76+
77+ parents : 1D array, shape (n_nodes, ) or None
78+ The parent of each node. Only returned when a connectivity matrix
79+ is specified, elsewhere 'None' is returned.
6880 """
6981 X = np .asarray (X )
7082 n_samples , n_features = X .shape
@@ -73,9 +85,9 @@ def ward_tree(X, connectivity=None, n_components=None, copy=True,
7385
7486 if connectivity is None :
7587 if n_clusters is not None :
76- raise ValueError ('Early stopping is implemented only for '
88+ warnings . warn ('Early stopping is implemented only for '
7789 'structured Ward clustering (i.e. with '
78- 'explicit connectivity.' )
90+ 'explicit connectivity.' , stacklevel = 2 )
7991 out = hierarchy .ward (X )
8092 children_ = out [:, :2 ].astype (np .int )
8193 return children_ , 1 , n_samples , None
@@ -284,6 +296,15 @@ class Ward(BaseEstimator):
284296 The number of connected components in the graph defined by the \
285297 connectivity matrix. If not set, it is estimated.
286298
299+ compute_full_tree: bool or 'auto' (optional)
300+ Stop early the construction of the tree at n_clusters. This is
301+ useful to decrease computation time if the number of clusters is
302+ not small compared to the number of samples. This option is
303+ useful only when specifying a connectivity matrix. Note also that
304+ when varying the number of cluster and using caching, it may
305+ be advantageous to compute the full tree.
306+
307+
287308 Attributes
288309 ----------
289310 `children_` : array-like, shape = [n_nodes, 2]
@@ -393,6 +414,15 @@ class WardAgglomeration(AgglomerationTransform, Ward):
393414 The number of connected components in the graph defined by the
394415 connectivity matrix. If not set, it is estimated.
395416
417+ compute_full_tree: bool or 'auto' (optional)
418+ Stop early the construction of the tree at n_clusters. This is
419+ useful to decrease computation time if the number of clusters is
420+ not small compared to the number of samples. This option is
421+ useful only when specifying a connectivity matrix. Note also that
422+ when varying the number of cluster and using caching, it may
423+ be advantageous to compute the full tree.
424+
425+
396426 Attributes
397427 ----------
398428 `children_` : array-like, shape = [n_nodes, 2]
0 commit comments