Skip to content

Commit f7e2b74

Browse files
flaviomorellirth
authored andcommitted
DOC Added example to ConvergenceWarning (scikit-learn#15216)
1 parent 3c13450 commit f7e2b74

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

sklearn/exceptions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ class ChangedBehaviorWarning(UserWarning):
4848
class ConvergenceWarning(UserWarning):
4949
"""Custom warning to capture convergence problems
5050
51+
Examples
52+
--------
53+
54+
>>> import numpy as np
55+
>>> import warnings
56+
>>> from sklearn.cluster import KMeans
57+
>>> from sklearn.exceptions import ConvergenceWarning
58+
>>> warnings.simplefilter("always", ConvergenceWarning)
59+
>>> X = np.asarray([[0, 0],
60+
... [0, 1],
61+
... [1, 0],
62+
... [1, 0]]) # last point is duplicated
63+
>>> with warnings.catch_warnings(record=True) as w:
64+
... km = KMeans(n_clusters=4).fit(X)
65+
... print(w[-1].message)
66+
Number of distinct clusters (3) found smaller than n_clusters (4).
67+
Possibly due to duplicate points in X.
68+
5169
.. versionchanged:: 0.18
5270
Moved from sklearn.utils.
5371
"""

0 commit comments

Comments
 (0)