Skip to content

Commit

Permalink
import
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyBagnall committed Jan 24, 2025
1 parent 6e24ef0 commit c731117
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
17 changes: 3 additions & 14 deletions aeon/transformations/collection/imbalance/_adasyn.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""ADASYN over sampling algorithm."""

import numpy as np
from scipy import sparse
from sklearn.utils import check_random_state

from aeon.transformations.collection.imbalance import SMOTE
from aeon.transformations.collection.imbalance._smote import SMOTE

__maintainer__ = ["TonyBagnall"]
__all__ = ["ADASYN"]
Expand Down Expand Up @@ -75,23 +74,13 @@ def _transform(self, X, y=None):
cols = random_state.choice(n_neighbors, size=n_samples)
diffs = X_class[nns[rows, cols]] - X_class[rows]
steps = random_state.uniform(size=(n_samples, 1))

if sparse.issparse(X):
sparse_func = type(X).__name__
steps = getattr(sparse, sparse_func)(steps)
X_new = X_class[rows] + steps.multiply(diffs)
else:
X_new = X_class[rows] + steps * diffs
X_new = X_class[rows] + steps * diffs

X_new = X_new.astype(X.dtype)
y_new = np.full(n_samples, fill_value=class_sample, dtype=y.dtype)
X_resampled.append(X_new)
y_resampled.append(y_new)

if sparse.issparse(X):
X_resampled = sparse.vstack(X_resampled, format=X.format)
else:
X_resampled = np.vstack(X_resampled)
X_resampled = np.vstack(X_resampled)
y_resampled = np.hstack(y_resampled)

X_resampled = X_resampled[:, np.newaxis, :]
Expand Down
29 changes: 9 additions & 20 deletions aeon/transformations/collection/imbalance/_smote.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from collections import OrderedDict

import numpy as np
from scipy import sparse
from sklearn.neighbors import NearestNeighbors
from sklearn.utils import check_random_state

Expand All @@ -33,10 +32,10 @@ class SMOTE(BaseCollectionTransformer):
Parameters
----------
k_neighbors : int or object, default=5
The nearest neighbors used to define the neighborhood of samples to use
to generate the synthetic samples. `~sklearn.neighbors.NearestNeighbors`
instance will be fitted in this case.
k_neighbors : int, default=5
The number of nearest neighbors used to define the neighborhood of samples
to use to generate the synthetic time series.
`~sklearn.neighbors.NearestNeighbors` instance will be fitted in this case.
random_state : int, RandomState instance or None, default=None
If `int`, random_state is the seed used by the random number generator;
If `RandomState` instance, random_state is the random number generator;
Expand Down Expand Up @@ -102,11 +101,7 @@ def _transform(self, X, y=None):
)
X_resampled.append(X_new)
y_resampled.append(y_new)

if sparse.issparse(X):
X_resampled = sparse.vstack(X_resampled, format=X.format)
else:
X_resampled = np.vstack(X_resampled)
X_resampled = np.vstack(X_resampled)
y_resampled = np.hstack(y_resampled)
X_resampled = X_resampled[:, np.newaxis, :]
return X_resampled, y_resampled
Expand All @@ -118,8 +113,9 @@ def _make_samples(
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
Points from which the points will be created.
X : np.ndarray
Shape (n_cases, n_timepoints), time series from which the new series will
be created.
y_dtype : dtype
The data type of the targets.
Expand Down Expand Up @@ -222,12 +218,5 @@ def _generate_samples(
diffs[mask_pair_samples] *= random_state.uniform(
low=0.0, high=0.5, size=(mask_pair_samples.sum(), 1)
)

if sparse.issparse(X):
sparse_func = type(X).__name__
steps = getattr(sparse, sparse_func)(steps)
X_new = X[rows] + steps.multiply(diffs)
else:
X_new = X[rows] + steps * diffs

X_new = X[rows] + steps * diffs
return X_new.astype(X.dtype)

0 comments on commit c731117

Please sign in to comment.