-
-
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
[MRG+2] Added sample weight support to confusion matrix. #4001
Conversation
Implementation for #3450. Any feedback is appreciated. |
|
||
cm = confusion_matrix(y_true, y_pred, sample_weight=weights) | ||
|
||
assert_array_almost_equal(cm, [[4.1, 0.8, 0.1], |
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.
I'd rather not have this hard-coded. More precisely, we can use:
assert_array_almost_equal(cm,
.1 * confusion_matrix(y_true[:25], y_pred[:25]) +
.2 * confusion_matrix(y_true[25:50], y_pred[25:50]) +
.3 * confusion_matrix(y_true[50:], y_pred[50:]))
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.
That's definitely better. I was influenced by the tests around it which had hard-coded values as well. Thank you for the feedback! Will push a commit shortly.
Are you still here? can you rebase? |
Hi! Just got the notification. Will rebase in 48h, as I'm currently unable to do that. |
Sure. ! |
Added a sample_weight parameter inside confusion_matrix which will be used to build the confusion matrix. If not present, it will default to np.ones() of size equal to the number of samples. Tests were created in test_classification.py instead of test_common.py.
Removed hard-coded result matrix for something better.
Should be ok now. If there's anything else you need, just let me know! |
# confusion_matrix with sample_weight is in | ||
# test_classification.py | ||
"hamming_loss", | ||
"matthews_corrcoef_score", |
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.
you can remove the "hamming loss" and "matthews_corrocef_score". They were supported recently
Just that minor comment. Will merge after that |
Sorry about that! Please let me know of anything else needed. Thanks! |
sample_weight = np.ones(y_true.shape[0], dtype=np.int) | ||
else: | ||
sample_weight = np.asarray(sample_weight) | ||
|
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 as y_true
and y_pred
using check_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?
Merged as 01b5b7b after doing a cosmit, an error to check for inconsistent length and updating whatsnew. Sorry for the year long wait @DanielSidhion ! |
Added a sample_weight parameter inside confusion_matrix which
will be used to build the confusion matrix. If not present, it
will default to np.ones() of size equal to the number of samples.
Tests were created in test_classification.py instead of
test_common.py.