Skip to content

Commit

Permalink
Merge pull request #89 from SimonBlanke/feature/fix-sklearn-best-para…
Browse files Browse the repository at this point in the history
…ms-scores

Feature/fix sklearn best params scores
  • Loading branch information
SimonBlanke authored Aug 30, 2024
2 parents 87dff2d + 160e668 commit ee07244
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
23 changes: 12 additions & 11 deletions src/hyperactive/integrations/sklearn/hyperactive_search_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,43 @@ def __init__(
self.refit = refit
self.cv = cv

def _refit(
self,
X,
y=None,
**fit_params,
):
self.best_estimator_ = clone(self.estimator)
def _refit(self, X, y=None, **fit_params):
self.best_estimator_ = clone(self.estimator).set_params(
**clone(self.best_params_, safe=False)
)

self.best_estimator_.fit(X, y, **fit_params)
return self

def fit(self, X, y, **params):
def fit(self, X, y, **fit_params):
X, y = indexable(X, y)
X, y = self._validate_data(X, y)

params = _check_method_params(X, params=params)
params = _check_method_params(X, params=fit_params)
self.scorer_ = check_scoring(self.estimator, scoring=self.scoring)

objective_function_adapter = ObjectiveFunctionAdapter(
self.estimator,
)
objective_function_adapter.add_dataset(X, y)
objective_function_adapter.add_validation(self.scorer_, self.cv)
objective_function = objective_function_adapter.objective_function

hyper = Hyperactive(verbosity=False)
hyper.add_search(
objective_function_adapter.objective_function,
objective_function,
search_space=self.params_config,
optimizer=self.optimizer,
n_iter=self.n_iter,
n_jobs=self.n_jobs,
random_state=self.random_state,
)
hyper.run()
self.best_params_ = hyper.best_para(objective_function)
self.best_score_ = hyper.best_score(objective_function)

if self.refit:
self._refit(X, y, **params)
self._refit(X, y, **fit_params)

return self

Expand Down
49 changes: 30 additions & 19 deletions tests/integrations/sklearn/test_sklearn_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from sklearn import svm, datasets
from sklearn.naive_bayes import GaussianNB
from sklearn.isotonic import IsotonicRegression
from sklearn.decomposition import PCA


Expand All @@ -16,114 +15,126 @@
iris = datasets.load_iris()
X, y = iris.data, iris.target


ir = IsotonicRegression()
nb = GaussianNB()
svc = svm.SVC()
pca = PCA(n_components=2)
pca = PCA()


parameters_svc = {"kernel": ["linear", "rbf"], "C": [1, 10]}
parameters_nb = {"var_smoothing": [1e-7, 1e-8, 1e-9]}
parameters_pca = {"n_components": [2, 3, 4]}

parameters = {"kernel": ["linear", "rbf"], "C": [1, 10]}
opt = RandomSearchOptimizer()


def test_fit():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

check_is_fitted(search)


def test_score():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)
score = search.score(X, y)

assert isinstance(score, float)


def test_classes_():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

assert [0, 1, 2] == list(search.classes_)


def test_score_samples():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

with pytest.raises(AttributeError):
search.score_samples(X)


def test_predict():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)
result = search.predict(X)

assert isinstance(result, np.ndarray)


def test_predict_proba():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

with pytest.raises(AttributeError):
search.predict_proba(X)

search = HyperactiveSearchCV(nb, opt, parameters)
search = HyperactiveSearchCV(nb, opt, parameters_nb)
search.fit(X, y)
result = search.predict(X)

assert isinstance(result, np.ndarray)


def test_predict_log_proba():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

with pytest.raises(AttributeError):
search.predict_log_proba(X)

search = HyperactiveSearchCV(nb, opt, parameters)
search = HyperactiveSearchCV(nb, opt, parameters_nb)
search.fit(X, y)
result = search.predict_log_proba(X)

assert isinstance(result, np.ndarray)


def test_decision_function():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)
result = search.decision_function(X)

assert isinstance(result, np.ndarray)


def test_transform():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

with pytest.raises(AttributeError):
search.transform(X)

search = HyperactiveSearchCV(pca, opt, parameters)
search = HyperactiveSearchCV(pca, opt, parameters_pca)
search.fit(X, y)
result = search.transform(X)

assert isinstance(result, np.ndarray)


def test_inverse_transform():
search = HyperactiveSearchCV(svc, opt, parameters)
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

with pytest.raises(AttributeError):
search.inverse_transform(X)

search = HyperactiveSearchCV(pca, opt, parameters)
search = HyperactiveSearchCV(pca, opt, parameters_pca)
search.fit(X, y)
result = search.inverse_transform(search.transform(X))

assert isinstance(result, np.ndarray)


def test_best_params_and_score():
search = HyperactiveSearchCV(svc, opt, parameters_svc)
search.fit(X, y)

best_params = search.best_params_
best_score = search.best_score_

assert "kernel" in best_params and "C" in best_params
assert isinstance(best_score, float)

0 comments on commit ee07244

Please sign in to comment.