Skip to content

Commit aebcb16

Browse files
committed
ENH use hasattr "predict_proba" in bagging
More robust than catching exceptions.
1 parent 0f5685b commit aebcb16

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

sklearn/ensemble/bagging.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _parallel_predict_proba(estimators, estimators_features, X, n_classes):
127127
proba = np.zeros((n_samples, n_classes))
128128

129129
for estimator, features in zip(estimators, estimators_features):
130-
try:
130+
if hasattr(estimator, "predict_proba"):
131131
proba_estimator = estimator.predict_proba(X[:, features])
132132

133133
if n_classes == len(estimator.classes_):
@@ -137,7 +137,7 @@ def _parallel_predict_proba(estimators, estimators_features, X, n_classes):
137137
proba[:, estimator.classes_] += \
138138
proba_estimator[:, range(len(estimator.classes_))]
139139

140-
except (AttributeError, NotImplementedError):
140+
else:
141141
# Resort to voting
142142
predictions = estimator.predict(X[:, features])
143143

@@ -463,11 +463,11 @@ def _set_oob_score(self, X, y):
463463
mask = np.ones(n_samples, dtype=np.bool)
464464
mask[samples] = False
465465

466-
try:
466+
if hasattr(estimator, "predict_proba"):
467467
predictions[mask, :] += estimator.predict_proba(
468468
(X[mask, :])[:, features])
469469

470-
except (AttributeError, NotImplementedError):
470+
else:
471471
p = estimator.predict((X[mask, :])[:, features])
472472
j = 0
473473

0 commit comments

Comments
 (0)