Skip to content
Open
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
13 changes: 9 additions & 4 deletions aeon/anomaly_detection/collection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def fit(self, X, y=None):

X = self._preprocess_collection(X)
if y is not None:
y = self._check_y(y, self.metadata_["n_cases"])
self._check_y(y, self.metadata_["n_cases"])
y = self._convert_y(y)

self._fit(X, y)

Expand Down Expand Up @@ -208,7 +209,8 @@ def fit_predict(self, X, y=None, axis=1) -> np.ndarray:
return self._predict(X)

if y is not None:
y = self._check_y(y)
self._check_y(y, self.metadata_["n_cases"])
y = self._convert_y(y)

pred = self._fit_predict(X, y)

Expand Down Expand Up @@ -251,7 +253,10 @@ def _check_y(self, y, n_cases):
f"Mismatch in number of cases. Found X = {n_cases} and y = {n_labels}"
)

return y

def _convert_y(self, y):
"""Convert y to correct anomaly detection format after validation."""
if isinstance(y, pd.Series):
y = pd.Series.to_numpy(y)

return y
return y.astype(bool)
15 changes: 12 additions & 3 deletions aeon/anomaly_detection/series/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def fit(self, X, y=None, axis=1):

X = self._preprocess_series(X, axis, True)
if y is not None:
y = self._check_y(y)
self._check_y(y)
y = self._convert_y(y)

self._fit(X=X, y=y)

Expand Down Expand Up @@ -203,7 +204,8 @@ def fit_predict(self, X, y=None, axis=1) -> np.ndarray:
return self._predict(X)

if y is not None:
y = self._check_y(y)
self._check_y(y)
y = self._convert_y(y)

pred = self._fit_predict(X, y)

Expand Down Expand Up @@ -284,5 +286,12 @@ def _check_y(self, y: VALID_SERIES_INPUT_TYPES) -> np.ndarray:
f"{VALID_SERIES_INPUT_TYPES}, saw {type(y)}"
)

new_y = new_y.astype(bool)
return new_y

def _convert_y(self, y):
"""Convert y to correct anomaly detection format after validation."""
if isinstance(y, pd.Series):
y = y.values
elif isinstance(y, pd.DataFrame):
y = y.squeeze().values
return y.astype(bool)
15 changes: 10 additions & 5 deletions aeon/regression/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def score(self, X, y, metric="r2", metric_params=None) -> float:
MSE score of predict(X) vs y
"""
self._check_is_fitted()
y = self._check_y(y, len(X))
self._check_y(y, len(X))
y = self._convert_y(y)
_metric_params = metric_params
if metric_params is None:
_metric_params = {}
Expand Down Expand Up @@ -351,7 +352,8 @@ def _fit_setup(self, X, y):
self.reset()

X = self._preprocess_collection(X)
y = self._check_y(y, self.metadata_["n_cases"])
self._check_y(y, self.metadata_["n_cases"])
y = self._convert_y(y)

# return processed X and y
return X, y
Expand Down Expand Up @@ -380,13 +382,16 @@ def _check_y(self, y, n_cases):
f"sklearn.utils.multiclass.type_of_target"
)

if isinstance(y, pd.Series):
y = pd.Series.to_numpy(y)

if any([isinstance(label, str) for label in y]):
raise ValueError(
"y contains strings, cannot fit a regressor. If suitable, convert "
"to floats or consider classification."
)

return y

def _convert_y(self, y):
"""Convert y to the correct regression format after validation."""
if isinstance(y, pd.Series):
y = pd.Series.to_numpy(y)
return y.astype(float)
3 changes: 2 additions & 1 deletion aeon/regression/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def test__check_y():
reg._check_y(y, 100)
assert isinstance(y, np.ndarray)
y = pd.Series(y)
y = reg._check_y(y, 100)
reg._check_y(y, 100)
y = reg._convert_y(y)
assert isinstance(y, np.ndarray)
# Test error raising
# y wrong length
Expand Down