MAPIE icon indicating copy to clipboard operation
MAPIE copied to clipboard

Accept directly EnsembleEstimator as model for MapieRegressor

Open vincentblot28 opened this issue 2 years ago • 0 comments

Why this can be useful ? When creating some custom non-conformity score with your very nice interface, one might want to compare different scores on the same dataset. For instance, I might want to use the gamma conformity score with different powers of y_pred in the denominator. Right now, if I want to do this, I have to fit a new MapieRegressor each time, and if I'm using the Jackknife method it can be very long

Solution

def fit(X_train, y_train, ...):
        (estimator,
         self.conformity_score_function_,
         agg_function,
         cv,
         X,
         y,
         sample_weight) = self._check_fit_parameters(X, y, sample_weight)
        if not isinstance(estimator, EnsembleRegressor):
            my_regressor = (
                EnsembleRegressor if not self.model_has_std else EnsembleStdRegressor
            )
            self.estimator_ = my_regressor(
                estimator,
                self.method,
                cv,
                agg_function,
                self.n_jobs,
                self.random_state,
                self.test_size,
                self.verbose
            )
        else:
            self.estimator_ = estimator
        if not self.estimator_.is_fitted:
            self.estimator_ = self.estimator_.fit(X, y, sample_weight)

        if self.model_has_std:
            y_pred, y_std = self.estimator_.predict_calib(X)
            self.conformity_scores_ = \
                self.conformity_score_function_.get_conformity_scores(
                    X, y, y_pred, y_std
                )
        else:
            y_pred = self.estimator_.predict_calib(X)
            self.conformity_scores_ = \
                self.conformity_score_function_.get_conformity_scores(
                    X, y, y_pred
                )

        return self

Here, what is done is that if my estimator is an EnsembleRegressor that is already fitted, then I don't need re-fit all the LOO models, by I can change my conformity score (and even change the method plus to minmax if I change the attribute in the EnsembleRegressor estimator

We should also need to have a is_fitted method in the EnsembleRegressor

vincentblot28 avatar Nov 09 '23 16:11 vincentblot28