-
-
Notifications
You must be signed in to change notification settings - Fork 26k
calibration_loss calculator added #10971
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
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d71c057
caliberation_los calculator added
aishgrt1 ce6ef9e
Update classification.py
aishgrt1 63c6f95
Update classification.py
aishgrt1 fcc7cf4
Update classification.py
aishgrt1 c9c4b64
Update classification.py
aishgrt1 1617026
Update classification.py
aishgrt1 df89090
added calibration loss
aishgrt1 d545bba
added calibration_loss
aishgrt1 54c4941
Update __init__.py
aishgrt1 3ddb56c
Update classification.py
aishgrt1 fea917e
Update classification.py
aishgrt1 138801f
Update classification.py
aishgrt1 170375c
Update classification.py
aishgrt1 fc2da9e
Update classification.py
aishgrt1 09e8613
Update classification.py
aishgrt1 afda36e
Update classification.py
aishgrt1 908779d
Update classification.py
aishgrt1 9005a00
Update classification.py
aishgrt1 500ca14
Update classification.py
aishgrt1 30779c7
Update classification.py
aishgrt1 0ab5bc0
calibration_loss test added
aishgrt1 aa9bd3b
Update test_classification.py
aishgrt1 f863d3a
Update test_classification.py
aishgrt1 519c546
Update test_classification.py
aishgrt1 dc267e2
Update test_classification.py
aishgrt1 9de655f
Update classification.py
aishgrt1 04090b6
Update classification.py
aishgrt1 1484806
Update classification.py
aishgrt1 624a197
Update classification.py
aishgrt1 c02e66b
Update classification.py
aishgrt1 fe9e2d5
Update classification.py
aishgrt1 f3196d9
Update test_classification.py
aishgrt1 fb2bf49
Update test_classification.py
aishgrt1 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
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 |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
from ..utils.validation import _num_samples | ||
from ..utils.sparsefuncs import count_nonzero | ||
from ..exceptions import UndefinedMetricWarning | ||
from __future__ import division | ||
|
||
|
||
def _check_targets(y_true, y_pred): | ||
|
@@ -1993,3 +1994,64 @@ def brier_score_loss(y_true, y_prob, sample_weight=None, pos_label=None): | |
y_true = np.array(y_true == pos_label, int) | ||
y_true = _check_binary_probabilistic_predictions(y_true, y_prob) | ||
return np.average((y_true - y_prob) ** 2, weights=sample_weight) | ||
|
||
|
||
def calibration_loss(y_true, y_prob, bin_size=2): | ||
|
||
"""Compute Calibration score by bins. | ||
The calibration loss is defined as the measure to access the quality of | ||
aishgrt1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
learning methods and learned models. A calibration measure based on | ||
overlaping binning is CAL (Caruana and Niculescu-Mizil, 2004). | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "read more in the User Guide" |
||
Parameters | ||
---------- | ||
y_true : array, shape (n_samples,) | ||
True targets. | ||
|
||
y_prob : array, shape (n_samples,) | ||
Probabilities of the positive class. | ||
|
||
bin_size : int | ||
Size of the bin (samples) analysed in one iteration | ||
|
||
Returns | ||
------- | ||
score : float | ||
Calibration loss | ||
|
||
Examples | ||
-------- | ||
>>> import numpy as np | ||
>>> from sklearn.metrics import calibration_loss | ||
>>> y_true = np.array([0, 1, 1, 0]) | ||
>>> y_true_categorical = np.array(["spam", "ham", "ham", "spam"]) | ||
>>> y_prob = np.array([0.1, 0.9, 0.8, 0.3]) | ||
>>> calibration_loss(y_true, y_prob, bin_size=1) | ||
0.175 | ||
>>> calibration_loss(y_true, y_prob, bin_size=2) | ||
0.5333333333333333 | ||
""" | ||
pos_loss = 0.0 | ||
neg_loss = 0.0 | ||
|
||
for bin_start in range(0, len(y_true) - bin_size + 1): | ||
|
||
bin_end = bin_start + bin_size | ||
actual_per_pos_class = (y_true[bin_start:bin_end] | ||
.sum()) / bin_size | ||
bin_error_pos = abs(y_prob[bin_start:bin_end] | ||
- actual_per_pos_class).sum() | ||
pos_loss += bin_error_pos | ||
|
||
actual_per_neg_class = (bin_size - y_true[bin_start:bin_end] | ||
.sum()) / bin_size | ||
bin_error_neg = abs((1-y_prob[bin_start:bin_end]) | ||
- actual_per_neg_class).sum() | ||
neg_loss += bin_error_neg | ||
|
||
pos_loss /= (len(y_true) - bin_size + 1) | ||
neg_loss /= (len(y_true) - bin_size + 1) | ||
loss = (0.5) * (pos_loss + neg_loss) | ||
|
||
return loss | ||
|
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
from sklearn.utils.mocking import MockDataFrame | ||
|
||
from sklearn.metrics import accuracy_score | ||
from sklearn.metrics import calibration_loss | ||
from sklearn.metrics import average_precision_score | ||
from sklearn.metrics import classification_report | ||
from sklearn.metrics import cohen_kappa_score | ||
|
@@ -1635,3 +1636,36 @@ def test_brier_score_loss(): | |
# calculate even if only single class in y_true (#6980) | ||
assert_almost_equal(brier_score_loss([0], [0.5]), 0.25) | ||
assert_almost_equal(brier_score_loss([1], [0.5]), 0.25) | ||
|
||
|
||
def test_calibration_loss(): | ||
# Check calibration_loss function | ||
y_true = np.array([0, 1, 1, 0, 1, 1]) | ||
y_pred = np.array([0.1, 0.8, 0.9, 0.3, 1.0, 0.95]) | ||
calibration_loss_val = calibration_loss(y_true, y_pred, bin_size=2) | ||
assert_almost_equal(calibration_loss_val, 0.46999, decimal=4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this example from? Either cite a reference, or show how you calculated it |
||
|
||
def test_balanced_accuracy_score_unseen(): | ||
assert_warns_message(UserWarning, 'y_pred contains classes not in y_true', | ||
balanced_accuracy_score, [0, 0, 0], [0, 0, 1]) | ||
|
||
|
||
@pytest.mark.parametrize('y_true,y_pred', | ||
[ | ||
(['a', 'b', 'a', 'b'], ['a', 'a', 'a', 'b']), | ||
(['a', 'b', 'c', 'b'], ['a', 'a', 'a', 'b']), | ||
(['a', 'a', 'a', 'b'], ['a', 'b', 'c', 'b']), | ||
]) | ||
def test_balanced_accuracy_score(y_true, y_pred): | ||
macro_recall = recall_score(y_true, y_pred, average='macro', | ||
labels=np.unique(y_true)) | ||
with ignore_warnings(): | ||
# Warnings are tested in test_balanced_accuracy_score_unseen | ||
balanced = balanced_accuracy_score(y_true, y_pred) | ||
assert balanced == pytest.approx(macro_recall) | ||
adjusted = balanced_accuracy_score(y_true, y_pred, adjusted=True) | ||
chance = balanced_accuracy_score(y_true, np.full_like(y_true, y_true[0])) | ||
assert adjusted == (balanced - chance) / (1 - chance) | ||
def test_balanced_accuracy_score_unseen(): | ||
assert_warns_message(UserWarning, 'y_pred contains classes not in y_true', | ||
balanced_accuracy_score, [0, 0, 0], [0, 0, 1]) |
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.
Uh oh!
There was an error while loading. Please reload this page.