def test_runtime_limit_hor(self): import time planned_pipeline = (MinMaxScaler | Normalizer) >> LinearRegression from sklearn.datasets import load_boston X, y = load_boston(return_X_y=True) max_opt_time = 3.0 hor = GridSearchCV( estimator=planned_pipeline, cv=3, max_opt_time=max_opt_time, scoring="r2", ) start = time.time() with self.assertRaises(BaseException): _ = hor.fit(X[:500, :], y[:500]) end = time.time() opt_time = end - start rel_diff = (opt_time - max_opt_time) / max_opt_time assert ( rel_diff < 0.2 ), "Max time: {}, Actual time: {}, relative diff: {}".format( max_opt_time, opt_time, rel_diff )
def test_runtime_limit_hoc(self): import time planned_pipeline = (MinMaxScaler | Normalizer) >> ( LogisticRegression | KNeighborsClassifier) from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) max_opt_time = 10.0 hoc = GridSearchCV( estimator=planned_pipeline, cv=3, scoring="accuracy", max_opt_time=max_opt_time, ) start = time.time() with self.assertRaises(BaseException): _ = hoc.fit(X, y) end = time.time() opt_time = end - start rel_diff = (opt_time - max_opt_time) / max_opt_time assert (rel_diff < 0.7 ), "Max time: {}, Actual time: {}, relative diff: {}".format( max_opt_time, opt_time, rel_diff)
def test_grid_search_on_trained_auto(self): from sklearn.metrics import accuracy_score, make_scorer from sklearn.model_selection import GridSearchCV iris = load_iris() X, y = iris.data, iris.target lr = LogisticRegression() trained = lr.fit(X, y) parameters = get_grid_search_parameter_grids(lr, num_samples=2) _ = GridSearchCV(trained, parameters, cv=5, scoring=make_scorer(accuracy_score))
def test_grid_search_on_trained(self): from sklearn.metrics import accuracy_score, make_scorer from sklearn.model_selection import GridSearchCV iris = load_iris() X, y = iris.data, iris.target lr = LogisticRegression() trained = lr.fit(X, y) parameters = {"solver": ("liblinear", "lbfgs"), "penalty": ["l2"]} _ = GridSearchCV(trained, parameters, cv=5, scoring=make_scorer(accuracy_score))
def test_manual_grid(self): from lale.lib.sklearn import SVC from sklearn.datasets import load_iris from lale.lib.lale import GridSearchCV warnings.simplefilter("ignore") from lale import wrap_imported_operators wrap_imported_operators() iris = load_iris() parameters = {'kernel': ('linear', 'rbf'), 'C': [1, 10]} svc = SVC() clf = GridSearchCV(estimator=svc, param_grid=parameters) clf.fit(iris.data, iris.target) clf.predict(iris.data)
def test_with_sklearn_gridsearchcv(self): from sklearn.datasets import load_iris from sklearn.metrics import accuracy_score, make_scorer from sklearn.model_selection import GridSearchCV lr = LogisticRegression() parameters = {"solver": ("liblinear", "lbfgs"), "penalty": ["l2"]} with warnings.catch_warnings(): warnings.simplefilter("ignore") clf = GridSearchCV( lr, parameters, cv=5, scoring=make_scorer(accuracy_score) ) iris = load_iris() clf.fit(iris.data, iris.target)
def test_with_gridsearchcv_auto_wrapped_pipe1(self): from sklearn.datasets import load_iris from sklearn.metrics import accuracy_score, make_scorer lr = LogisticRegression() pca = PCA() trainable = pca >> lr with warnings.catch_warnings(): warnings.simplefilter("ignore") from lale.lib.lale import GridSearchCV clf = GridSearchCV( estimator=trainable, lale_num_samples=1, lale_num_grids=1, cv=2, scoring=make_scorer(accuracy_score)) iris = load_iris() clf.fit(iris.data, iris.target)