Skip to content

Commit 11e6abb

Browse files
committed
Merge branch 'master' into 400-create-citations-using-github
2 parents ff6ff00 + 1b57a04 commit 11e6abb

File tree

4 files changed

+88
-72
lines changed

4 files changed

+88
-72
lines changed

mapie/classification.py

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
compute_quantiles, fit_estimator, fix_number_of_classes)
2424

2525

26+
from mapie.conformity_scores.utils_classification_conformity_scores import (
27+
get_true_label_position,
28+
)
29+
30+
2631
class MapieClassifier(BaseEstimator, ClassifierMixin):
2732
"""
2833
Prediction sets for classification.
@@ -737,39 +742,6 @@ def _regularize_conformity_score(
737742
)
738743
return conf_score
739744

740-
def _get_true_label_position(
741-
self,
742-
y_pred_proba: NDArray,
743-
y: NDArray
744-
) -> NDArray:
745-
"""
746-
Return the sorted position of the true label in the
747-
prediction
748-
749-
Parameters
750-
----------
751-
y_pred_proba: NDArray of shape (n_samples, n_calsses)
752-
Model prediction.
753-
754-
y: NDArray of shape (n_samples)
755-
Labels.
756-
757-
Returns
758-
-------
759-
NDArray of shape (n_samples, 1)
760-
Position of the true label in the prediction.
761-
"""
762-
index = np.argsort(
763-
np.fliplr(np.argsort(y_pred_proba, axis=1))
764-
)
765-
position = np.take_along_axis(
766-
index,
767-
y.reshape(-1, 1),
768-
axis=1
769-
)
770-
771-
return position
772-
773745
def _get_last_included_proba(
774746
self,
775747
y_pred_proba: NDArray,
@@ -1217,7 +1189,7 @@ def fit(
12171189
self.y_pred_proba_raps = self.single_estimator_.predict_proba(
12181190
self.X_raps
12191191
)
1220-
self.position_raps = self._get_true_label_position(
1192+
self.position_raps = get_true_label_position(
12211193
self.y_pred_proba_raps,
12221194
self.y_raps
12231195
)
@@ -1249,7 +1221,7 @@ def fit(
12491221
# Here we reorder the labels by decreasing probability
12501222
# and get the position of each label from decreasing
12511223
# probability
1252-
self.conformity_scores_ = self._get_true_label_position(
1224+
self.conformity_scores_ = get_true_label_position(
12531225
y_pred_proba,
12541226
y_enc
12551227
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
from mapie._typing import NDArray
3+
4+
5+
def get_true_label_position(y_pred_proba: NDArray, y: NDArray) -> NDArray:
6+
"""
7+
Return the sorted position of the true label in the
8+
prediction
9+
10+
Parameters
11+
----------
12+
y_pred_proba: NDArray of shape (n_samples, n_classes)
13+
Model prediction.
14+
15+
y: NDArray of shape (n_samples)
16+
Labels.
17+
18+
Returns
19+
-------
20+
NDArray of shape (n_samples, 1)
21+
Position of the true label in the prediction.
22+
"""
23+
index = np.argsort(np.fliplr(np.argsort(y_pred_proba, axis=1)))
24+
position = np.take_along_axis(index, y.reshape(-1, 1), axis=1)
25+
26+
return position

mapie/tests/test_classification.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from copy import deepcopy
4-
from typing import Any, Dict, Iterable, List, Optional, Union, cast
4+
from typing import Any, Dict, Iterable, Optional, Union, cast
55

66
import numpy as np
77
import pandas as pd
@@ -57,25 +57,6 @@
5757
)
5858
]
5959

60-
Y_TRUE_PROBA_PLACE = [
61-
[
62-
np.array([2, 0]),
63-
np.array([
64-
[.1, .3, .6],
65-
[.2, .7, .1]
66-
]),
67-
np.array([[0], [1]])
68-
],
69-
[
70-
np.array([1, 0]),
71-
np.array([
72-
[.7, .12, .18],
73-
[.5, .24, .26]
74-
]),
75-
np.array([[2], [0]])
76-
]
77-
]
78-
7960
Params = TypedDict(
8061
"Params",
8162
{
@@ -1854,23 +1835,6 @@ def test_get_last_included_proba_shape(k_lambda, strategy):
18541835
assert y_p_p_i_l.shape == (len(X), 1, len(thresholds))
18551836

18561837

1857-
@pytest.mark.parametrize("y_true_proba_place", Y_TRUE_PROBA_PLACE)
1858-
def test_get_true_label_position(
1859-
y_true_proba_place: List[NDArray]
1860-
) -> None:
1861-
"""
1862-
Check that the returned true label position the good.
1863-
"""
1864-
y_true = y_true_proba_place[0]
1865-
y_pred_proba = y_true_proba_place[1]
1866-
place = y_true_proba_place[2]
1867-
1868-
mapie = MapieClassifier(random_state=random_state)
1869-
found_place = mapie._get_true_label_position(y_pred_proba, y_true)
1870-
1871-
assert (found_place == place).all()
1872-
1873-
18741838
@pytest.mark.parametrize("cv", [5, None])
18751839
def test_error_raps_cv_not_prefit(cv: Union[int, None]) -> None:
18761840
"""
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List
2+
3+
import numpy as np
4+
import pytest
5+
6+
from mapie.conformity_scores.utils_classification_conformity_scores import (
7+
get_true_label_position,
8+
)
9+
from mapie._typing import NDArray
10+
11+
Y_TRUE_PROBA_PLACE = [
12+
[
13+
np.array([2, 0]),
14+
np.array([
15+
[.1, .3, .6],
16+
[.2, .7, .1]
17+
]),
18+
np.array([[0], [1]])
19+
],
20+
[
21+
np.array([1, 0]),
22+
np.array([
23+
[.7, .12, .18],
24+
[.5, .24, .26]
25+
]),
26+
np.array([[2], [0]])
27+
]
28+
]
29+
30+
31+
def test_shape_get_true_label_position() -> None:
32+
"""
33+
Check the shape returned by the function
34+
"""
35+
y_pred_proba = np.random.rand(5, 3)
36+
y = np.random.randint(0, 3, size=(5, 1))
37+
position = get_true_label_position(y_pred_proba, y)
38+
assert position.shape == y.shape
39+
40+
41+
@pytest.mark.parametrize("y_true_proba_place", Y_TRUE_PROBA_PLACE)
42+
def test_get_true_label_position(
43+
y_true_proba_place: List[NDArray]
44+
) -> None:
45+
"""
46+
Check that the returned true label position the good.
47+
"""
48+
y_true = y_true_proba_place[0]
49+
y_pred_proba = y_true_proba_place[1]
50+
place = y_true_proba_place[2]
51+
52+
found_place = get_true_label_position(y_pred_proba, y_true)
53+
54+
assert (found_place == place).all()

0 commit comments

Comments
 (0)