@@ -435,17 +435,22 @@ def fit(self, X: ArrayLike, y: ArrayLike, sample_weight: Optional[ArrayLike] = N
435435 MapieRegressor
436436 The model itself.
437437 """
438+ # Checks
438439 self ._check_parameters ()
439440 cv = self ._check_cv (self .cv )
440441 estimator = self ._check_estimator (self .estimator )
441442 X , y = check_X_y (X , y , force_all_finite = False , dtype = ["float64" , "object" ])
442443 fit_parameters = signature (estimator .fit ).parameters
443444 supports_sw = "sample_weight" in fit_parameters
444445 sample_weight , X , y = self ._check_null_weight (sample_weight , X , y )
445- y_pred = np .empty_like (y , dtype = float )
446+
447+ # Initialization
446448 self .estimators_ : List [RegressorMixin ] = []
447449 self .n_features_in_ = X .shape [1 ]
448450 self .k_ = np .empty_like (y , dtype = int )
451+ y_pred = np .empty_like (y , dtype = float )
452+
453+ # Work
449454 self .single_estimator_ = self ._fit_estimator (clone (estimator ), X , y , supports_sw , sample_weight )
450455 if self .method == "naive" :
451456 y_pred = self .single_estimator_ .predict (X )
@@ -479,39 +484,50 @@ def predict(self, X: ArrayLike) -> np.ndarray:
479484
480485 Returns
481486 -------
482- np.ndarray of shape (n_samples, 3, len(alpha) )
487+ np.ndarray of shape (n_samples, 3, n_alpha )
483488
484489 - [:, 0, :]: Center of the prediction interval
485490 - [:, 1, :]: Lower bound of the prediction interval
486491 - [:, 2, :]: Upper bound of the prediction interval
487492 """
493+ # Checks
488494 check_is_fitted (self , ["single_estimator_" , "estimators_" , "k_" , "residuals_" ])
489495 X = check_array (X , force_all_finite = False , dtype = ["float64" , "object" ])
490- y_pred = self .single_estimator_ .predict (X )
491496 alpha = self ._check_alpha (self .alpha )
497+
498+ # SHAPES:
499+ # (n_samples_train,) : self.residuals_
500+ # (n_alpha,) : alpha
501+ # (n_samples_test, n_alpha) : y_pred_low, y_pred_up
502+ # (n_samples_test, n_samples_train) : y_pred_multi, lower_bounds, upper_bounds
503+ n_alpha = len (alpha )
504+ y_pred = self .single_estimator_ .predict (X )
505+ # At this point, y_pred is of shape (n_samples_test,)
492506 if self .method in ["naive" , "base" ]:
493507 quantile = np .quantile (self .residuals_ , 1 - alpha , interpolation = "higher" )
494- # broadcast y_pred to get y_pred_low/up of shape (n_samples_test, len(alpha))
495508 y_pred_low = y_pred [:, np .newaxis ] - quantile
496509 y_pred_up = y_pred [:, np .newaxis ] + quantile
497510 else :
498- y_pred_multi = np .stack ([e .predict (X ) for e in self .estimators_ ], axis = 1 )
511+ y_pred_multi = np .column_stack ([e .predict (X ) for e in self .estimators_ ])
499512 if self .method == "plus" :
513+ # At this point, y_pred_multi is of shape (n_samples_test, n_estimators_)
514+ # We thus enforce y_pred_multi to be of shape (n_samples_test, n_samples_train)
500515 if len (self .estimators_ ) < len (self .k_ ):
501516 y_pred_multi = y_pred_multi [:, self .k_ ]
502517 lower_bounds = y_pred_multi - self .residuals_
503518 upper_bounds = y_pred_multi + self .residuals_
504519 if self .method == "minmax" :
505520 lower_bounds = np .min (y_pred_multi , axis = 1 , keepdims = True ) - self .residuals_
506521 upper_bounds = np .max (y_pred_multi , axis = 1 , keepdims = True ) + self .residuals_
507- y_pred_low = np .stack ([
522+ y_pred_low = np .column_stack ([
508523 np .quantile (lower_bounds , _alpha , axis = 1 , interpolation = "lower" ) for _alpha in alpha
509- ], axis = 1 )
510- y_pred_up = np .stack ([
524+ ])
525+ y_pred_up = np .column_stack ([
511526 np .quantile (upper_bounds , 1 - _alpha , axis = 1 , interpolation = "higher" ) for _alpha in alpha
512- ], axis = 1 )
527+ ])
513528 if self .ensemble :
514529 y_pred = np .median (y_pred_multi , axis = 1 )
515- # tile y_pred to get same shape as y_pred_low/up
516- y_pred = np .tile (y_pred , (alpha .shape [0 ], 1 )).T
517- return np .stack ([y_pred , y_pred_low , y_pred_up ], axis = 1 )
530+ y_pred = np .column_stack ([y_pred ]* n_alpha )
531+ # At this point, y_pred is of shape (n_samples_test, n_alpha)
532+ y_preds = np .stack ([y_pred , y_pred_low , y_pred_up ], axis = 1 )
533+ return y_preds
0 commit comments