-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from scikit-learn-contrib/426-move-some-metho…
…ds-of-mapieclassifier-to-a-utils-file ENH: move get_true_label_position to utils in classification
- Loading branch information
Showing
4 changed files
with
88 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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
26 changes: 26 additions & 0 deletions
26
mapie/conformity_scores/utils_classification_conformity_scores.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import numpy as np | ||
from mapie._typing import NDArray | ||
|
||
|
||
def get_true_label_position(y_pred_proba: NDArray, y: NDArray) -> NDArray: | ||
""" | ||
Return the sorted position of the true label in the | ||
prediction | ||
Parameters | ||
---------- | ||
y_pred_proba: NDArray of shape (n_samples, n_classes) | ||
Model prediction. | ||
y: NDArray of shape (n_samples) | ||
Labels. | ||
Returns | ||
------- | ||
NDArray of shape (n_samples, 1) | ||
Position of the true label in the prediction. | ||
""" | ||
index = np.argsort(np.fliplr(np.argsort(y_pred_proba, axis=1))) | ||
position = np.take_along_axis(index, y.reshape(-1, 1), axis=1) | ||
|
||
return position |
This file contains 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
54 changes: 54 additions & 0 deletions
54
mapie/tests/test_utils_classification_conformity_scores.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import List | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from mapie.conformity_scores.utils_classification_conformity_scores import ( | ||
get_true_label_position, | ||
) | ||
from mapie._typing import NDArray | ||
|
||
Y_TRUE_PROBA_PLACE = [ | ||
[ | ||
np.array([2, 0]), | ||
np.array([ | ||
[.1, .3, .6], | ||
[.2, .7, .1] | ||
]), | ||
np.array([[0], [1]]) | ||
], | ||
[ | ||
np.array([1, 0]), | ||
np.array([ | ||
[.7, .12, .18], | ||
[.5, .24, .26] | ||
]), | ||
np.array([[2], [0]]) | ||
] | ||
] | ||
|
||
|
||
def test_shape_get_true_label_position() -> None: | ||
""" | ||
Check the shape returned by the function | ||
""" | ||
y_pred_proba = np.random.rand(5, 3) | ||
y = np.random.randint(0, 3, size=(5, 1)) | ||
position = get_true_label_position(y_pred_proba, y) | ||
assert position.shape == y.shape | ||
|
||
|
||
@pytest.mark.parametrize("y_true_proba_place", Y_TRUE_PROBA_PLACE) | ||
def test_get_true_label_position( | ||
y_true_proba_place: List[NDArray] | ||
) -> None: | ||
""" | ||
Check that the returned true label position the good. | ||
""" | ||
y_true = y_true_proba_place[0] | ||
y_pred_proba = y_true_proba_place[1] | ||
place = y_true_proba_place[2] | ||
|
||
found_place = get_true_label_position(y_pred_proba, y_true) | ||
|
||
assert (found_place == place).all() |