diff --git a/keras/src/wrappers/fixes.py b/keras/src/wrappers/fixes.py index e16819782526..b503e4e88e82 100644 --- a/keras/src/wrappers/fixes.py +++ b/keras/src/wrappers/fixes.py @@ -34,9 +34,9 @@ def _raise_or_return(target_type): else: return target_type - target_type = sklearn.utils.multiclass.type_of_target( - y, input_name=input_name - ) + from sklearn.utils.multiclass import type_of_target as sk_type_of_target + + target_type = sk_type_of_target(y, input_name=input_name) return _raise_or_return(target_type) diff --git a/keras/src/wrappers/sklearn_wrapper.py b/keras/src/wrappers/sklearn_wrapper.py index 77c3488b6ee9..2c714277bc89 100644 --- a/keras/src/wrappers/sklearn_wrapper.py +++ b/keras/src/wrappers/sklearn_wrapper.py @@ -172,7 +172,9 @@ def fit(self, X, y, **kwargs): def predict(self, X): """Predict using the model.""" - sklearn.base.check_is_fitted(self) + from sklearn.utils.validation import check_is_fitted + + check_is_fitted(self) X = _validate_data(self, X, reset=False) raw_output = self.model_.predict(X) return self._reverse_process_target(raw_output) @@ -472,7 +474,9 @@ def transform(self, X): X_transformed: array-like, shape=(n_samples, n_features) The transformed data. """ - sklearn.base.check_is_fitted(self) + from sklearn.utils.validation import check_is_fitted + + check_is_fitted(self) X = _validate_data(self, X, reset=False) return self.model_.predict(X) diff --git a/keras/src/wrappers/utils.py b/keras/src/wrappers/utils.py index 301c4b562912..8c2954b055ad 100644 --- a/keras/src/wrappers/utils.py +++ b/keras/src/wrappers/utils.py @@ -1,3 +1,5 @@ +import numpy as np + try: import sklearn from sklearn.base import BaseEstimator @@ -25,8 +27,8 @@ def _check_model(model): # compile model if user gave us an un-compiled model if not model.compiled or not model.loss or not model.optimizer: raise RuntimeError( - "Given model needs to be compiled, and have a loss and an " - "optimizer." + "Given model needs to be compiled, and have a loss " + "and an optimizer." ) @@ -80,8 +82,9 @@ def inverse_transform(self, y): is passed, it will be squeezed back to 1D. Otherwise, it will eb left untouched. """ - sklearn.base.check_is_fitted(self) - xp, _ = sklearn.utils._array_api.get_namespace(y) + from sklearn.utils.validation import check_is_fitted + + check_is_fitted(self) if self.ndim_ == 1 and y.ndim == 2: - return xp.squeeze(y, axis=1) + return np.squeeze(y, axis=1) return y