Skip to content

Commit

Permalink
REFACTOR & ENH: Split .fit into .fit_single_estimator and fit_multi_e…
Browse files Browse the repository at this point in the history
…stimator in MapieRegressor and EnsembleRegressor, implement (wip) CrossConformalRegressor (#545)
  • Loading branch information
jawadhussein462 authored and Valentin-Laurent committed Dec 29, 2024
1 parent 6e09d78 commit cf4a536
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
22 changes: 21 additions & 1 deletion mapie_v1/conformity_scores/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import Union, List
from mapie.conformity_scores import BaseRegressionScore
from . import REGRESSION_CONFORMITY_SCORES_STRING_MAP

Expand All @@ -12,3 +12,23 @@ def check_and_select_split_conformity_score(
return REGRESSION_CONFORMITY_SCORES_STRING_MAP[conformity_score]()
else:
raise ValueError("Invalid conformity_score type")


def process_confidence_level(
self, confidence_level: Union[float, List[float]]
) -> List[float]:
"""
Ensure confidence_level is always a list of floats.
"""
if isinstance(confidence_level, float):
return [confidence_level]
return confidence_level


def compute_alpha(
self, confidence_levels: List[float]
) -> List[float]:
"""
Compute alpha values from confidence levels.
"""
return [1 - level for level in confidence_levels]
39 changes: 30 additions & 9 deletions mapie_v1/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
from mapie.conformity_scores import BaseRegressionScore
from mapie.regression import MapieRegressor
from mapie.utils import check_estimator_fit_predict
from mapie_v1.conformity_scores.utils import \
check_and_select_split_conformity_score
from mapie_v1.conformity_scores.utils import (
check_and_select_split_conformity_score,
process_confidence_level,
compute_alpha,
)


class SplitConformalRegressor:
Expand Down Expand Up @@ -101,10 +104,8 @@ def __init__(
random_state=random_state,
)

if isinstance(confidence_level, float):
confidence_level = [confidence_level]

self.alpha = [1 - level for level in confidence_level]
self.confidence_level = process_confidence_level(confidence_level)
self.alpha = compute_alpha(self.confidence_level)

def fit(
self,
Expand Down Expand Up @@ -322,7 +323,19 @@ def __init__(
verbose: int = 0,
random_state: Optional[Union[int, np.random.RandomState]] = None
) -> None:
pass

self.mapie_regressor = MapieRegressor(
estimator=self.estimator,
method=method,
cv=cv,
n_jobs=n_jobs,
verbose=verbose,
conformity_score=self.conformity_score,
random_state=random_state,
)

self.confidence_level = process_confidence_level(confidence_level)
self.alpha = compute_alpha(self.confidence_level)

def fit(
self,
Expand Down Expand Up @@ -350,7 +363,10 @@ def fit(
Self
The fitted CrossConformalRegressor instance.
"""
pass
X, y, sample_weight, groups = self.init_fit(
X, y, fit_params=fit_params
)
self.mapie_regressor.fit_estimator(X, y, sample_weight, groups)

def conformalize(
self,
Expand Down Expand Up @@ -387,7 +403,12 @@ def conformalize(
Self
The conformalized SplitConformalRegressor instance.
"""
pass
self.mapie_regressor.conformalize(
X,
y,
groups=groups,
predict_params=predict_params
)

def predict_set(
self,
Expand Down

0 comments on commit cf4a536

Please sign in to comment.