8000 [MRG+1] Add sample_weight parameter to cohen_kappa_score by vpoughon · Pull Request #7569 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG+1] Add sample_weight parameter to cohen_kappa_score #7569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Enhancements
<https://github.com/scikit-learn/scikit-learn/pull/4939>`_) by `Andrea
Esuli`_.

- Add ``sample_weight`` parameter to :func:`metrics.cohen_kappa_score` by
Victor Poughon.

Bug fixes
.........

Expand Down
8 changes: 6 additions & 2 deletions sklearn/metrics/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def confusion_matrix(y_true, y_pred, labels=None, sample_weight=None):
return CM


def cohen_kappa_score(y1, y2, labels=None, weights=None):
def cohen_kappa_score(y1, y2, labels=None, weights=None, sample_weight=None):
"""Cohen's kappa: a statistic that measures inter-annotator agreement.

This function computes Cohen's kappa [1]_, a score that expresses the level
Expand Down Expand Up @@ -311,6 +311,9 @@ class labels [2]_.
List of weighting type to calculate the score. None means no weighted;
"linear" means linear weighted; "quadratic" means quadratic weighted.

sample_weight : array-like of shape = [n_samples], optional
Sample weights.

Returns
-------
kappa : float
Expand All @@ -328,7 +331,8 @@ class labels [2]_.
.. [3] `Wikipedia entry for the Cohen's kappa.
<https://en.wikipedia.org/wiki/Cohen%27s_kappa>`_
"""
confusion = confusion_matrix(y1, y2, labels=labels)
confusion = confusion_matrix(y1, y2, labels=labels,
sample_weight=sample_weight)
n_classes = confusion.shape[0]
sum0 = np.sum(confusion, axis=0)
sum1 = np.sum(confusion, axis=1)
Expand Down
1 change: 0 additions & 1 deletion sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@

# No Sample weight support
METRICS_WITHOUT_SAMPLE_WEIGHT = [
"cohen_kappa_score",
"confusion_matrix", # Left this one here because the tests in this file do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I would expect you need to add it in some other places now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, no. It's already in CLASSIFICATION_METRICS, which is in ALL_METRICS, which means it will now be tested by test_sample_weight_invariance().

# not work for confusion_matrix, as its output is a
# matrix instead of a number. Testing of
Expand Down
0