diff --git a/AUTHORS.rst b/AUTHORS.rst index 5ba2de610..6ef4fb579 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -52,4 +52,5 @@ Contributors * Faustin Pulvéric * Chaoqi Zhang * Leena Kamran Qidwai +* Aman Vishnoi To be continued ... diff --git a/mapie/classification.py b/mapie/classification.py index c4c92cfef..b29127a23 100644 --- a/mapie/classification.py +++ b/mapie/classification.py @@ -965,6 +965,7 @@ def fit( X=X, sample_weight=sample_weight, groups=groups, + predict_params=predict_params, ) return self diff --git a/mapie/conformity_scores/sets/raps.py b/mapie/conformity_scores/sets/raps.py index 30cfb6d00..3ab462656 100644 --- a/mapie/conformity_scores/sets/raps.py +++ b/mapie/conformity_scores/sets/raps.py @@ -180,9 +180,15 @@ def get_conformity_scores( Conformity scores. """ # Compute y_pred and position on the RAPS validation dataset - self.y_pred_proba_raps = self.predictor.single_estimator_.predict_proba( - self.X_raps - ) + predict_params = kwargs.pop("predict_params", None) + if predict_params is not None and len(predict_params) > 0: + self.y_pred_proba_raps = self.predictor.single_estimator_.predict_proba( + self.X_raps, **predict_params + ) + else: + self.y_pred_proba_raps = self.predictor.single_estimator_.predict_proba( + self.X_raps + ) self.position_raps = get_true_label_position( self.y_pred_proba_raps, self.y_raps ) diff --git a/mapie/estimator/classifier.py b/mapie/estimator/classifier.py index 5742e668c..20abfdd69 100644 --- a/mapie/estimator/classifier.py +++ b/mapie/estimator/classifier.py @@ -384,7 +384,7 @@ def predict_proba_calib( check_is_fitted(self, self.fit_attributes) if self.cv == "prefit": - y_pred_proba = self.single_estimator_.predict_proba(X) + y_pred_proba = self.single_estimator_.predict_proba(X, **predict_params) y_pred_proba = self._check_proba_normalized(y_pred_proba) else: X = cast(NDArray, X) diff --git a/mapie/tests/test_classification.py b/mapie/tests/test_classification.py index a4123c02a..9bf0f62a7 100644 --- a/mapie/tests/test_classification.py +++ b/mapie/tests/test_classification.py @@ -1764,6 +1764,42 @@ def test_predict_parameters_passing() -> None: np.testing.assert_equal(y_pred, 0) +def test_raps_with_predict_params() -> None: + """Test that predict_params are correctly passed when using RAPS.""" + X, y = make_classification( + n_samples=500, + n_features=10, + n_informative=3, + n_classes=3, + random_state=random_state, + ) + X_train, X_test, y_train, y_test = train_test_split( + X, y, test_size=0.2, random_state=random_state + ) + estimator = CustomGradientBoostingClassifier(random_state=random_state) + predict_params = {"check_predict_params": True} + mapie_clf = _MapieClassifier( + estimator=estimator, + conformity_score=RAPSConformityScore(size_raps=0.2), + cv="split", + test_size=0.2, + random_state=random_state, + ) + + mapie_clf.fit(X_train, y_train, predict_params=predict_params) + + y_pred, y_ps = mapie_clf.predict( + X_test, + alpha=0.1, + include_last_label="randomized", + agg_scores="mean", + **predict_params, + ) + # Ensure the output shapes are correct + assert y_pred.shape == (X_test.shape[0],) + assert y_ps.shape == (X_test.shape[0], len(np.unique(y)), 1) + + def test_with_no_predict_parameters_passing() -> None: """ Test passing with no predict parameters from the