-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add multi-output support to honest trees #86
Changes from 5 commits
7005dfb
2c3606b
f831bc4
f1ab592
dd0662d
8365c33
b259441
16f8561
2e00358
fd6643a
c182cad
72306d0
ee14b64
d4d2337
c14f629
2feb117
f172880
a31e1de
0fc8ec4
0f773e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,12 +360,6 @@ def fit(self, X, y, sample_weight=None, check_input=True): | |
) | ||
self._inherit_estimator_attributes() | ||
|
||
if self.n_outputs_ > 1: | ||
raise NotImplementedError( | ||
"Multi-target honest trees not yet \ | ||
implemented" | ||
) | ||
|
||
# update the number of classes, unsplit | ||
if y.ndim == 1: | ||
# reshape is necessary to preserve the data contiguity against vs | ||
|
@@ -423,18 +417,29 @@ def _inherit_estimator_attributes(self): | |
self.n_outputs_ = self.estimator_.n_outputs_ | ||
self.tree_ = self.estimator_.tree_ | ||
|
||
def _empty_leaf_correction(self, proba, normalizer): | ||
def _empty_leaf_correction(self, proba, pos=0): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you just add a short docstring to describe what's going on? I'm reading these lines and having trouble figuring out what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"""Leaves with empty posteriors are assigned values""" | ||
zero_mask = proba.sum(axis=1) == 0.0 | ||
if self.honest_prior == "empirical": | ||
proba[zero_mask] = self.empirical_prior_ | ||
elif self.honest_prior == "uniform": | ||
proba[zero_mask] = 1 / self.n_classes_ | ||
elif self.honest_prior == "ignore": | ||
proba[zero_mask] = np.nan | ||
else: | ||
raise ValueError(f"honest_prior {self.honest_prior} not a valid input.") | ||
|
||
# For multi-output cases | ||
if self.n_outputs_ > 1: | ||
if self.honest_prior == "empirical": | ||
proba[zero_mask] = self.empirical_prior_[pos] | ||
elif self.honest_prior == "uniform": | ||
proba[zero_mask] = 1 / self.n_classes_[pos] | ||
elif self.honest_prior == "ignore": | ||
proba[zero_mask] = np.nan | ||
else: | ||
raise ValueError(f"honest_prior {self.honest_prior} not a valid input.") | ||
else: | ||
if self.honest_prior == "empirical": | ||
proba[zero_mask] = self.empirical_prior_ | ||
elif self.honest_prior == "uniform": | ||
proba[zero_mask] = 1 / self.n_classes_ | ||
elif self.honest_prior == "ignore": | ||
proba[zero_mask] = np.nan | ||
else: | ||
raise ValueError(f"honest_prior {self.honest_prior} not a valid input.") | ||
return proba | ||
|
||
def _impute_missing_classes(self, proba): | ||
|
@@ -481,15 +486,24 @@ class in a leaf. | |
proba /= normalizer | ||
if self._tree_n_classes_ != self.n_classes_: | ||
proba = self._impute_missing_classes(proba) | ||
proba = self._empty_leaf_correction(proba, normalizer) | ||
proba = self._empty_leaf_correction(proba) | ||
|
||
return proba | ||
|
||
else: | ||
raise NotImplementedError( | ||
"Multi-target honest trees not yet \ | ||
implemented" | ||
) | ||
all_proba = [] | ||
|
||
for k in range(self.n_outputs_): | ||
proba_k = proba[:, k, : self._tree_n_classes_[k]] | ||
normalizer = proba_k.sum(axis=1)[:, np.newaxis] | ||
normalizer[normalizer == 0.0] = 1.0 | ||
proba_k /= normalizer | ||
if self._tree_n_classes_[k] != self.n_classes_[k]: | ||
proba_k = self._impute_missing_classes(proba_k) | ||
proba_k = self._empty_leaf_correction(proba_k, k) | ||
all_proba.append(proba_k) | ||
|
||
return all_proba | ||
|
||
def predict(self, X, check_input=True): | ||
"""Predict class for X. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html
and assert some non-trivial performance?
Is it possible to interpret MSE of 0.5-1.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that
accuracy_score
doesn't support multi-output. It seems that the default way they measure such predictions is throughsklearn.multioutput.MultiOutputClassifier
.