|
4 | 4 |
|
5 | 5 | import numpy as np |
6 | 6 | from sklearn.model_selection import check_cv |
7 | | -from sklearn.utils.metaestimators import if_delegate_has_method |
| 7 | +from sklearn.utils.metaestimators import available_if |
8 | 8 | from sklearn.utils import check_array, check_random_state |
9 | 9 | from sklearn.base import ( |
10 | 10 | BaseEstimator, |
|
20 | 20 | if pandas_available: |
21 | 21 | import pandas as pd |
22 | 22 |
|
| 23 | +def _estimator_has(attr): |
| 24 | + def check(self): |
| 25 | + return hasattr(self.wrapped_estimator_, attr) |
| 26 | + |
| 27 | + return check |
| 28 | + |
23 | 29 | CAVEATS_CV_NONE = """ |
24 | 30 | Feature importances are computed on the same data as used for training, |
25 | 31 | i.e. feature importances don't reflect importance of features for |
@@ -247,23 +253,23 @@ def caveats_(self): |
247 | 253 |
|
248 | 254 | # ============= Exposed methods of a wrapped estimator: |
249 | 255 |
|
250 | | - @if_delegate_has_method(delegate='wrapped_estimator_') |
| 256 | + @available_if(_estimator_has('score')) |
251 | 257 | def score(self, X, y=None, *args, **kwargs): |
252 | 258 | return self.wrapped_estimator_.score(X, y, *args, **kwargs) |
253 | 259 |
|
254 | | - @if_delegate_has_method(delegate='wrapped_estimator_') |
| 260 | + @available_if(_estimator_has('predict')) |
255 | 261 | def predict(self, X): |
256 | 262 | return self.wrapped_estimator_.predict(X) |
257 | 263 |
|
258 | | - @if_delegate_has_method(delegate='wrapped_estimator_') |
| 264 | + @available_if(_estimator_has('predict_proba')) |
259 | 265 | def predict_proba(self, X): |
260 | 266 | return self.wrapped_estimator_.predict_proba(X) |
261 | 267 |
|
262 | | - @if_delegate_has_method(delegate='wrapped_estimator_') |
| 268 | + @available_if(_estimator_has('predict_log_proba')) |
263 | 269 | def predict_log_proba(self, X): |
264 | 270 | return self.wrapped_estimator_.predict_log_proba(X) |
265 | 271 |
|
266 | | - @if_delegate_has_method(delegate='wrapped_estimator_') |
| 272 | + @available_if(_estimator_has('decision_function')) |
267 | 273 | def decision_function(self, X): |
268 | 274 | return self.wrapped_estimator_.decision_function(X) |
269 | 275 |
|
|
0 commit comments