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

[BUG] Fixed subsampling in highly imbalances datasets giving subsamples with only a single class #2305

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
11 changes: 8 additions & 3 deletions aeon/classification/dictionary_based/_tde.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,14 @@ def _fit(self, X, y, keep_train_preds=False):
rng.choice(np.flatnonzero(preds == preds.max()))
)

subsample = rng.choice(self.n_cases_, size=subsample_size, replace=False)
X_subsample = X[subsample]
y_subsample = y[subsample]
while True:
subsample = rng.choice(
self.n_cases_, size=subsample_size, replace=False
)
X_subsample = X[subsample]
y_subsample = y[subsample]
if len(np.unique(y_subsample)) > 1:
break

tde = IndividualTDE(
*parameters,
Expand Down
15 changes: 15 additions & 0 deletions aeon/classification/dictionary_based/tests/test_tde.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,18 @@ def test_histogram_intersection():

res = histogram_intersection(numba_first, numba_second)
assert res == 2


def test_subsampling_in_highly_imbalanced_datasets():
"""Test the subsampling during fit for highly imbalanced datasets.

This test case tests the fix for bug #1726.
https://github.com/aeon-toolkit/aeon/issues/1726
"""
X = np.random.rand(10, 1, 20)
y_sc = np.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0])

tde = TemporalDictionaryEnsemble(random_state=42)
tde.fit(X, y_sc)

assert tde.is_fitted