Skip to content

Commit

Permalink
Merge pull request #427 from scikit-learn-contrib/426-move-some-metho…
Browse files Browse the repository at this point in the history
…ds-of-mapieclassifier-to-a-utils-file

ENH: move get_true_label_position to utils in classification
  • Loading branch information
thibaultcordier authored May 14, 2024
2 parents cfbba58 + ec9d3bb commit 1b57a04
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 72 deletions.
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
38 changes: 1 addition & 37 deletions mapie/tests/test_classification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from copy import deepcopy
from typing import Any, Dict, Iterable, List, Optional, Union, cast
from typing import Any, Dict, Iterable, Optional, Union, cast

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -57,25 +57,6 @@
)
]

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]])
]
]

Params = TypedDict(
"Params",
{
Expand Down Expand Up @@ -1854,23 +1835,6 @@ def test_get_last_included_proba_shape(k_lambda, strategy):
assert y_p_p_i_l.shape == (len(X), 1, len(thresholds))


@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]

mapie = MapieClassifier(random_state=random_state)
found_place = mapie._get_true_label_position(y_pred_proba, y_true)

assert (found_place == place).all()


@pytest.mark.parametrize("cv", [5, None])
def test_error_raps_cv_not_prefit(cv: Union[int, None]) -> None:
"""
Expand Down
54 changes: 54 additions & 0 deletions mapie/tests/test_utils_classification_conformity_scores.py
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()

0 comments on commit 1b57a04

Please sign in to comment.