@@ -832,7 +832,9 @@ class labels [2]_.
832
832
labels : array-like of shape (n_classes,), default=None
833
833
List of labels to index the matrix. This may be used to select a
834
834
subset of labels. If `None`, all labels that appear at least once in
835
- ``y1`` or ``y2`` are used.
835
+ ``y1`` or ``y2`` are used. Note that at least one label in `labels` must be
836
+ present in `y1`, even though this function is otherwise agnostic to the order
837
+ of `y1` and `y2`.
836
838
837
839
weights : {'linear', 'quadratic'}, default=None
838
840
Weighting type to calculate the score. `None` means not weighted;
@@ -866,7 +868,18 @@ class labels [2]_.
866
868
>>> cohen_kappa_score(y1, y2)
867
869
0.6875
868
870
"""
869
- confusion = confusion_matrix (y1 , y2 , labels = labels , sample_weight = sample_weight )
871
+ try :
872
+ confusion = confusion_matrix (y1 , y2 , labels = labels , sample_weight = sample_weight )
873
+ except ValueError as e :
874
+ if "At least one label specified must be in y_true" in str (e ):
875
+ msg = (
876
+ "At least one label in `labels` must be present in `y1` (even though "
877
+ "`cohen_kappa_score` is otherwise agnostic to the order of `y1` and "
878
+ "`y2`)."
879
+ )
880
+ raise ValueError (msg ) from e
881
+ raise
882
+
870
883
n_classes = confusion .shape [0 ]
871
884
sum0 = np .sum (confusion , axis = 0 )
872
885
sum1 = np .sum (confusion , axis = 1 )
0 commit comments