-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG+2] Added sample weight support to confusion matrix. #4001
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
DanielSidhion
wants to merge
4
commits into
scikit-learn:master
from
DanielSidhion:cmatrix_sample_weight
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
# Noel Dawe <[email protected]> | ||
# Jatin Shah <[email protected]> | ||
# Saurabh Jha <[email protected]> | ||
# Bernardo Stein <[email protected]> | ||
# License: BSD 3 clause | ||
|
||
from __future__ import division | ||
|
@@ -178,7 +179,7 @@ def accuracy_score(y_true, y_pred, normalize=True, sample_weight=None): | |
return _weighted_sum(score, sample_weight, normalize) | ||
|
||
|
||
def confusion_matrix(y_true, y_pred, labels=None): | ||
def confusion_matrix(y_true, y_pred, labels=None, sample_weight=None): | ||
"""Compute confusion matrix to evaluate the accuracy of a classification | ||
|
||
By definition a confusion matrix :math:`C` is such that :math:`C_{i, j}` | ||
|
@@ -201,6 +202,8 @@ def confusion_matrix(y_true, y_pred, labels=None): | |
If none is given, those that appear at least once | ||
in ``y_true`` or ``y_pred`` are used in sorted order. | ||
|
||
sample_weight : array-like of shape = [n_samples], optional | ||
Sample weights. | ||
|
||
Returns | ||
------- | ||
|
@@ -239,6 +242,13 @@ def confusion_matrix(y_true, y_pred, labels=None): | |
else: | ||
labels = np.asarray(labels) | ||
|
||
if sample_weight is None: | ||
sample_weight = np.ones(y_true.shape[0], dtype=np.int) | ||
else: | ||
sample_weight = np.asarray(sample_weight) | ||
|
||
check_consistent_length(sample_weight, y_true, y_pred) | ||
|
||
n_labels = labels.size | ||
label_to_ind = dict((y, x) for x, y in enumerate(labels)) | ||
# convert yt, yp into index | ||
|
@@ -249,8 +259,10 @@ def confusion_matrix(y_true, y_pred, labels=None): | |
ind = np.logical_and(y_pred < n_labels, y_true < n_labels) | ||
y_pred = y_pred[ind] | ||
y_true = y_true[ind] | ||
# also eliminate weights of eliminated items | ||
sample_weight = sample_weight[ind] | ||
|
||
CM = coo_matrix((np.ones(y_true.shape[0], dtype=np.int), (y_true, y_pred)), | ||
CM = coo_matrix((sample_weight, (y_true, y_pred)), | ||
shape=(n_labels, n_labels) | ||
).toarray() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: can you add a check here, to see if
sample_weight
is the same size asy_true
andy_pred
usingcheck_consistent_length
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MechCoder sure, seems like a good addition. I added the check after the highlighted code just to make sure that in the future, if some part of the previous code changes, the check still catches any future problems.
It would be good to have some tests against this behavior, but I'm a little short on time in the following weeks, so it's probably better to merge this and later I'll add another PR to improve the tests. What do you think?