Skip to content

[WIP] Chain on decision_function or predict_proba in ClassifierChain #9316

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 14 additions & 3 deletions sklearn/multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,12 @@ class labels for each estimator in the chain.
Chains for Multi-label Classification", 2009.

"""
def __init__(self, base_estimator, order=None, cv=None, random_state=None):
def __init__(self, base_estimator, order=None, cv=None, random_state=None, chain_method="predict"):
self.base_estimator = base_estimator
self.order = order
self.cv = cv
self.random_state = random_state
self.chain_method = chain_method

def fit(self, X, Y):
"""Fit the model to data matrix X and targets Y.
Expand Down Expand Up @@ -568,7 +569,12 @@ def predict_proba(self, X):
else:
X_aug = np.hstack((X, previous_predictions))
Y_prob_chain[:, chain_idx] = estimator.predict_proba(X_aug)[:, 1]
Y_pred_chain[:, chain_idx] = estimator.predict(X_aug)
if self.chain_method == "predict":
Y_pred_chain[:, chain_idx] = estimator.predict(X_aug)
elif self.chain_method == "predict_proba":
Y_pred_chain[:, chain_idx] = estimator.predict_proba(X_aug)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of these methods may be a different shape, more than a single column.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Predict_proba returns a 2d array

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be done in fit too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, predict_proba returns a 2d array, and I had mentioned this in the issue comment. This was just a mock implementation, to know whether I was going on the right path.

elif self.chain_method == "decision_function":
Y_pred_chain[:, chain_idx] = estimator.decision_function(X_aug)
inv_order = np.empty_like(self.order_)
inv_order[self.order_] = np.arange(len(self.order_))
Y_prob = Y_prob_chain[:, inv_order]
Expand Down Expand Up @@ -598,7 +604,12 @@ def decision_function(self, X):
else:
X_aug = np.hstack((X, previous_predictions))
Y_decision_chain[:, chain_idx] = estimator.decision_function(X_aug)
Y_pred_chain[:, chain_idx] = estimator.predict(X_aug)
if self.chain_method == "predict":
Y_pred_chain[:, chain_idx] = estimator.predict(X_aug)
elif self.chain_method == "predict_proba":
Y_pred_chain[:, chain_idx] = estimator.predict_proba(X_aug)
elif self.chain_method == "decision_function":
Y_pred_chain[:, chain_idx] = estimator.decision_function(X_aug)

inv_order = np.empty_like(self.order_)
inv_order[self.order_] = np.arange(len(self.order_))
Expand Down