Skip to content

Commit

Permalink
[BUG] Fixed subsampling in highly imbalances datasets giving subsampl…
Browse files Browse the repository at this point in the history
…es with only a single class (#2305)

* added argument 'max_subsamples' to subsample multiple times in case of unbalanced datasets giving subsamples with only one class.

* removed parameter max_subsamples and resample until valid subsample is found

---------

Co-authored-by: Ferdinand Rewicki <[email protected]>
  • Loading branch information
ferewi and Ferdinand Rewicki authored Nov 14, 2024
1 parent bc0d574 commit 2e98d33
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit 2e98d33

Please sign in to comment.