Skip to content

Commit

Permalink
Revert "Fix MultiOutputClassifier checking for predict_proba method o…
Browse files Browse the repository at this point in the history
…f base estimator (scikit-learn#12222)"

This reverts commit 7248191.
  • Loading branch information
Xing committed Apr 28, 2019
1 parent fbebabc commit 8e0ebd8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 47 deletions.
8 changes: 0 additions & 8 deletions doc/whats_new/v0.21.rst
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,6 @@ Support for Python 3.4 and below has been officially dropped.
containing this same sample due to the scaling used in decision_function.
:issue:`10440` by :user:`Jonathan Ohayon <Johayon>`.

:mod:`sklearn.multioutput`
........................

- |Fix| Fixed a bug in :class:`multiout.MultiOutputClassifier` where the
`predict_proba` method incorrectly checked for `predict_proba` attribute in
the estimator object.
:issue:`12222` by :user:`Rebekah Kim <rebekahkim>`

:mod:`sklearn.neighbors`
........................

Expand Down
15 changes: 5 additions & 10 deletions sklearn/multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def fit(self, X, y, sample_weight=None):

if not hasattr(self.estimator, "fit"):
raise ValueError("The base estimator should implement"
" a fit method")
" a fit method")

X, y = check_X_y(X, y,
multi_output=True,
Expand Down Expand Up @@ -186,8 +186,7 @@ def predict(self, X):
"""
check_is_fitted(self, 'estimators_')
if not hasattr(self.estimator, "predict"):
raise ValueError("The base estimator should implement"
" a predict method")
raise ValueError("The base estimator should implement a predict method")

X = check_array(X, accept_sparse=True)

Expand Down Expand Up @@ -328,9 +327,6 @@ def predict_proba(self, X):
"""Probability estimates.
Returns prediction probabilities for each class of each output.
This method will raise a ``ValueError`` if any of the
estimators do not have ``predict_proba``.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Expand All @@ -344,17 +340,16 @@ def predict_proba(self, X):
classes corresponds to that in the attribute `classes_`.
"""
check_is_fitted(self, 'estimators_')
if not all([hasattr(estimator, "predict_proba")
for estimator in self.estimators_]):
raise ValueError("The base estimator should implement "
if not hasattr(self.estimator, "predict_proba"):
raise ValueError("The base estimator should implement"
"predict_proba method")

results = [estimator.predict_proba(X) for estimator in
self.estimators_]
return results

def score(self, X, y):
"""Returns the mean accuracy on the given test data and labels.
""""Returns the mean accuracy on the given test data and labels.
Parameters
----------
Expand Down
29 changes: 0 additions & 29 deletions sklearn/tests/test_multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from sklearn.svm import LinearSVC
from sklearn.base import ClassifierMixin
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV


def test_multi_target_regression():
Expand Down Expand Up @@ -177,34 +176,6 @@ def test_multi_output_classification_partial_fit_parallelism():
assert est1 is not est2


# check predict_proba passes
def test_multi_output_predict_proba():
sgd_linear_clf = SGDClassifier(random_state=1, max_iter=5, tol=1e-3)
param = {'loss': ('hinge', 'log', 'modified_huber')}

# inner function for custom scoring
def custom_scorer(estimator, X, y):
if hasattr(estimator, "predict_proba"):
return 1.0
else:
return 0.0
grid_clf = GridSearchCV(sgd_linear_clf, param_grid=param,
scoring=custom_scorer, cv=3, error_score=np.nan)
multi_target_linear = MultiOutputClassifier(grid_clf)
multi_target_linear.fit(X, y)

multi_target_linear.predict_proba(X)

# SGDClassifier defaults to loss='hinge' which is not a probabilistic
# loss function; therefore it does not expose a predict_proba method
sgd_linear_clf = SGDClassifier(random_state=1, max_iter=5, tol=1e-3)
multi_target_linear = MultiOutputClassifier(sgd_linear_clf)
multi_target_linear.fit(X, y)
err_msg = "The base estimator should implement predict_proba method"
with pytest.raises(ValueError, match=err_msg):
multi_target_linear.predict_proba(X)


# 0.23. warning about tol not having its correct default value.
@pytest.mark.filterwarnings('ignore:max_iter and tol parameters have been')
def test_multi_output_classification_partial_fit():
Expand Down

0 comments on commit 8e0ebd8

Please sign in to comment.