def test_concat_with_hyperopt(self): from lale.lib.lale import Hyperopt pca = PCA(n_components=3) nys = Nystroem(n_components=10) concat = ConcatFeatures() lr = LogisticRegression(random_state=42, C=0.1) trainable = (pca & nys) >> concat >> lr clf = Hyperopt(estimator=trainable, max_evals=2) from sklearn.datasets import load_iris iris_data = load_iris() clf.fit(iris_data.data, iris_data.target) clf.predict(iris_data.data)
def test_concat_with_hyperopt2(self): from lale.lib.lale import Hyperopt from lale.operators import make_pipeline, make_union pca = PCA(n_components=3) nys = Nystroem(n_components=10) lr = LogisticRegression(random_state=42, C=0.1) trainable = make_pipeline(make_union(pca, nys), lr) clf = Hyperopt(estimator=trainable, max_evals=2) from sklearn.datasets import load_iris iris_data = load_iris() clf.fit(iris_data.data, iris_data.target) clf.predict(iris_data.data)
def test_using_scoring(self): lr = LogisticRegression() clf = Hyperopt(estimator=lr, scoring="accuracy", cv=5, max_evals=1) trained = clf.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test) predictions_1 = clf.predict(self.X_test) assert np.array_equal(predictions_1, predictions)
def test_using_scoring(self): from sklearn.metrics import hinge_loss, make_scorer, f1_score, accuracy_score lr = LogisticRegression() clf = Hyperopt(estimator=lr, scoring='accuracy', cv=5, max_evals=1) trained = clf.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test) predictions_1 = clf.predict(self.X_test) assert np.array_equal(predictions_1, predictions)
def test_custom_scoring(self): from sklearn.metrics import f1_score, make_scorer lr = LogisticRegression() clf = Hyperopt(estimator=lr, scoring=make_scorer(f1_score, average='macro'), cv = 5, max_evals=1) trained = clf.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test) predictions_1 = clf.predict(self.X_test) assert np.array_equal(predictions_1, predictions)
def test_other_algorithms(self): for alg in ["rand", "tpe", "atpe", "anneal"]: hyperopt = Hyperopt( estimator=LogisticRegression, algo=alg, cv=3, max_evals=3 ) trained = hyperopt.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test) predictions_1 = hyperopt.predict(self.X_test) self.assertTrue(np.array_equal(predictions_1, predictions), alg)
def test_custom_scorer(self): from sklearn.metrics import f1_score, make_scorer pipeline = PCA() >> LogisticRegression() def custom_scorer(estimator, X, y, factor=0.1): #This is a custom scorer for demonstrating the use of kwargs #Just applies some factor to the accuracy from sklearn.metrics import accuracy_score predictions = estimator.predict(X) self.assertEqual(factor, 0.5) return factor*accuracy_score(y, predictions) clf = Hyperopt(estimator=pipeline, scoring=custom_scorer, cv = 5, max_evals=1, args_to_scorer={'factor':0.5}) trained = clf.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test) predictions_1 = clf.predict(self.X_test) assert np.array_equal(predictions_1, predictions)