Skip to content
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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 7 additions & 35 deletions mapie/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
compute_quantiles, fit_estimator, fix_number_of_classes)


from mapie.conformity_scores.utils_classification_conformity_scores import (
get_true_label_position,
)


class MapieClassifier(BaseEstimator, ClassifierMixin):
"""
Prediction sets for classification.
Expand Down Expand Up @@ -737,39 +742,6 @@ def _regularize_conformity_score(
)
return conf_score

def _get_true_label_position(
self,
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_calsses)
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

def _get_last_included_proba(
self,
y_pred_proba: NDArray,
Expand Down Expand Up @@ -1217,7 +1189,7 @@ def fit(
self.y_pred_proba_raps = self.single_estimator_.predict_proba(
self.X_raps
)
self.position_raps = self._get_true_label_position(
self.position_raps = get_true_label_position(
self.y_pred_proba_raps,
self.y_raps
)
Expand Down Expand Up @@ -1249,7 +1221,7 @@ def fit(
# Here we reorder the labels by decreasing probability
# and get the position of each label from decreasing
# probability
self.conformity_scores_ = self._get_true_label_position(
self.conformity_scores_ = get_true_label_position(
y_pred_proba,
y_enc
)
Expand Down
26 changes: 26 additions & 0 deletions mapie/conformity_scores/utils_classification_conformity_scores.py
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
7 changes: 5 additions & 2 deletions mapie/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
from mapie.metrics import classification_coverage_score
from mapie.utils import check_alpha

from mapie.conformity_scores.utils_classification_conformity_scores import (
get_true_label_position
)

random_state = 42

METHODS = ["lac", "aps", "raps"]
Expand Down Expand Up @@ -1865,8 +1869,7 @@ def test_get_true_label_position(
y_pred_proba = y_true_proba_place[1]
place = y_true_proba_place[2]

mapie = MapieClassifier(random_state=random_state)
found_place = mapie._get_true_label_position(y_pred_proba, y_true)
found_place = get_true_label_position(y_pred_proba, y_true)
Copy link
Collaborator

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


assert (found_place == place).all()

Expand Down
21 changes: 21 additions & 0 deletions mapie/tests/test_utils_classification_conformity_scores.py
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Loading