Skip to content

ENH add predict_log_proba to ClassifierChain #27720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ Changelog
object in the parameter grid if it's an estimator. :pr:`26786` by `Adrin
Jalali`_.

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

- |Enhancement| Add method `predict_log_proba` to :class:`multioutput.ClassifierChain`.
:pr:`27720` by :user:`Guillaume Lemaitre <glemaitre>`.

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

Expand Down
15 changes: 15 additions & 0 deletions sklearn/multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,21 @@ def predict_proba(self, X):

return Y_prob

def predict_log_proba(self, X):
"""Predict logarithm of probability estimates.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The input data.

Returns
-------
Y_log_prob : array-like of shape (n_samples, n_classes)
The predicted logarithm of the probabilities.
"""
return np.log(self.predict_proba(X))

@_available_if_base_estimator_has("decision_function")
def decision_function(self, X):
"""Evaluate the decision_function of the models in the chain.
Expand Down
7 changes: 5 additions & 2 deletions sklearn/tests/test_multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ def test_classifier_chain_vs_independent_models():
)


def test_base_chain_fit_and_predict():
@pytest.mark.parametrize("response_method", ["predict_proba", "predict_log_proba"])
def test_base_chain_fit_and_predict(response_method):
# Fit base chain and verify predict performance
X, Y = generate_multilabel_dataset_with_correlations()
chains = [RegressorChain(Ridge()), ClassifierChain(LogisticRegression())]
Expand All @@ -560,7 +561,9 @@ def test_base_chain_fit_and_predict():
range(X.shape[1], X.shape[1] + Y.shape[1])
)

Y_prob = chains[1].predict_proba(X)
Y_prob = getattr(chains[1], response_method)(X)
if response_method == "predict_log_proba":
Y_prob = np.exp(Y_prob)
Y_binary = Y_prob >= 0.5
assert_array_equal(Y_binary, Y_pred)

Expand Down