-
Notifications
You must be signed in to change notification settings - Fork 112
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
ENH: move get_true_label_position to utils in classification #427
Merged
thibaultcordier
merged 6 commits into
master
from
426-move-some-methods-of-mapieclassifier-to-a-utils-file
May 14, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d0dec9
ENH: move get_true_label_position to utils in classification
BaptisteCalot 019ebfe
FIX formatage effectué par Black Formatter et correction d'un test un…
BaptisteCalot 91fec31
FIX ligne qui dépasse les 79 caractères
BaptisteCalot 61756ab
FIX ajout de lignes à la fin du fichier
BaptisteCalot 5d8b7e2
TEST prise en compte des commentaires de la PR
BaptisteCalot ec9d3bb
Merge branch 'master' into 426-move-some-methods-of-mapieclassifier-t…
BaptisteCalot 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 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
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
import numpy as np | ||
from mapie.conformity_scores.utils_classification_conformity_scores import ( | ||
get_true_label_position, | ||
) | ||
|
||
|
||
def test_get_true_label_position() -> None: | ||
y_pred_proba = np.array( | ||
[[0.1, 0.5, 0.4], [0.3, 0.2, 0.5], [0.2, 0.8, 0.0], [0.4, 0.35, 0.25]] | ||
) | ||
y = np.array([1, 2, 0, 1]) | ||
y = np.reshape( | ||
y, (-1, 1) | ||
) # add in order to have shape of form (4,1) instead of (4,) | ||
|
||
position = get_true_label_position(y_pred_proba, y) | ||
|
||
expected_position = np.array([[0], [0], [1], [1]]) | ||
|
||
assert np.array_equal(position, expected_position) | ||
assert position.shape == y.shape | ||
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. Your tests must test only one thing at a time. For the test on the shape, you can take some random np arrays with larger sizes |
Oops, something went wrong.
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.
This test should be implemented directly in test_utils_classification_conformity_scores