Skip to content
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

FIX ignore nan values when summing posteriors #291

Merged
merged 16 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion doc/whats_new/v0.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ Thanks to everyone who has contributed to the maintenance and improvement of
the project since version inception, including:

* `Adam Li`_

* `Sambit Panda`_
8 changes: 5 additions & 3 deletions doc/whats_new/v0.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ Version 0.9
Changelog
---------

-
- |Fix| Fixed a bug in the :class:`sktree.HonestForestClassifier` where posteriors
estimated on empty leaf with ``ignore`` prior would result in ``np.nan``
values for all trees on that sample.
By `Haoyin Xu`_ (:pr:`#291`)

Code and Documentation Contributors
-----------------------------------

Thanks to everyone who has contributed to the maintenance and improvement of
the project since version inception, including:

* `Adam Li`_

* `Haoyin Xu`_
13 changes: 5 additions & 8 deletions sktree/ensemble/_honest_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class HonestForestClassifier(ForestClassifier, ForestClassifierMixin):
- If int, then draw `max_samples` samples.
- If float, then draw `max_samples * X.shape[0]` samples.

honest_prior : {"ignore", "uniform", "empirical"}, default="empirical"
honest_prior : {"ignore", "uniform", "empirical"}, default="ignore"
Method for dealing with empty leaves during evaluation of a test
sample. If "ignore", the tree is ignored. If "uniform", the prior tree
posterior is 1/(number of classes). If "empirical", the prior tree
Expand Down Expand Up @@ -444,7 +444,7 @@ def __init__(
class_weight=None,
ccp_alpha=0.0,
max_samples=None,
honest_prior="empirical",
honest_prior="ignore",
honest_fraction=0.5,
tree_estimator=None,
stratify=False,
Expand Down Expand Up @@ -672,10 +672,7 @@ def _predict_proba(self, X, indices=None, impute_missing=None):
zero_mask = posteriors.sum(2) == 0
posteriors[~zero_mask] /= posteriors[~zero_mask].sum(1, keepdims=True)

if impute_missing is None:
pass
else:
posteriors[zero_mask] = impute_missing
posteriors[zero_mask] = impute_missing
PSSF23 marked this conversation as resolved.
Show resolved Hide resolved

# preserve shape of multi-outputs
if self.n_outputs_ > 1:
Expand Down Expand Up @@ -823,7 +820,7 @@ def _accumulate_prediction(predict, X, out, lock, indices=None):

with lock:
if len(out) == 1:
out[0][indices] += proba
out[0][indices] = np.nansum([out[0][indices], proba], axis=0)
else:
for i in range(len(out)):
out[i][indices] += proba[i]
out[i][indices] = np.nansum([out[i][indices], proba[i]], axis=0)
5 changes: 4 additions & 1 deletion sktree/stats/tests/test_forestht.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,15 @@ def test_small_dataset_dependent(seed):
n_repeats=1000,
metric="mi",
return_posteriors=False,
seed=seed,
)
assert ~np.isnan(result.pvalue)
assert ~np.isnan(result.observe_test_stat)
assert result.pvalue <= 0.05

result = build_coleman_forest(clf, perm_clf, X, y, metric="mi", return_posteriors=False)
result = build_coleman_forest(
clf, perm_clf, X, y, metric="mi", return_posteriors=False, seed=seed
)
assert result.pvalue <= 0.05


Expand Down
21 changes: 21 additions & 0 deletions sktree/tests/test_honest_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ def test_impute_posteriors(honest_prior, val):
), f"Failed with {honest_prior}, prior {clf.estimators_[0].empirical_prior_}"


@pytest.mark.parametrize(
"honest_prior, val",
[
("ignore", np.nan),
],
)
PSSF23 marked this conversation as resolved.
Show resolved Hide resolved
def test_ignore_posteriors(honest_prior, val):
PSSF23 marked this conversation as resolved.
Show resolved Hide resolved
X = rng.normal(0, 1, (100, 2))
y = [0] * 75 + [1] * 25
clf = HonestForestClassifier(
honest_fraction=0.5, random_state=0, honest_prior=honest_prior, n_estimators=100
)
clf = clf.fit(X, y)

y_proba = clf.predict_proba(X)

assert (
len(np.where(np.isnan(y_proba[:, 0]))[0]) < 10
), f"Failed with {honest_prior}, prior {clf.estimators_[0].empirical_prior_}"

PSSF23 marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.parametrize(
"honest_fraction, val",
[
Expand Down
Loading