Skip to content

Commit 17271bb

Browse files
authored
Merge pull request #49 from simai-ml/aesthetics
Aesthetics
2 parents c7b6430 + 548d46a commit 17271bb

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ matrix:
1616
install:
1717
- cmd: call %MINICONDA%\Scripts\activate.bat
1818
- conda update --yes conda
19-
- conda create -n test-env --yes -c conda-forge python=%PYTHON_VERSION% codecov mypy numpy=%NUMPY_VERSION% scikit-learn pytest-cov
19+
- conda create -n test-env --yes -c conda-forge python=%PYTHON_VERSION% codecov mypy numpy=%NUMPY_VERSION% scikit-learn pandas pytest-cov
2020
- conda activate test-env
2121

2222
test_script:

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ install:
1818
- export PATH=$HOME/miniconda/bin:$PATH
1919
- source $HOME/miniconda/etc/profile.d/conda.sh
2020
- conda update --yes conda
21-
- conda create -n test-env --yes -c conda-forge python=$TRAVIS_PYTHON_VERSION codecov mypy numpy=$NUMPY_VERSION scikit-learn pytest-cov
21+
- conda create -n test-env --yes -c conda-forge python=$TRAVIS_PYTHON_VERSION codecov mypy numpy=$NUMPY_VERSION scikit-learn pandas pytest-cov
2222
- conda activate test-env
2323

2424
script:

RELEASE_CHECKLIST.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- [ ] Check whether any new files need to go in MANIFEST.in
55
- [ ] Make sure tests run, pass and cover 100% of the package:
66
* `flake8 . --exclude=doc`
7-
* `mypy mapie examples --strict --config-file mypy.ini`
7+
* `mypy mapie examples --strict`
88
* `pytest -vs --doctest-modules mapie`
99
* `pytest -vs --doctest-modules --cov-branch --cov=mapie --pyargs mapie`
1010
- [ ] Make sure documentation builds without warnings and shows nicely:

environment.dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies:
77
- flake8=3.9.0
88
- mypy=0.800
99
- numpydoc=1.1.0
10+
- pandas=1.2.4
1011
- pytest=6.2.2
1112
- pytest-cov=2.11.1
1213
- python=3.9.2

mapie/estimators.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

requirements.dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- flake8==3.9.0
33
- mypy==0.800
44
- numpydoc==1.1.0
5+
- pandas==1.2.4
56
- pytest==6.2.2
67
- pytest-cov==2.11.1
78
- scikit-learn==0.24.1

0 commit comments

Comments
 (0)