def test_fit1(self): import warnings warnings.filterwarnings(action="ignore") from lale.lib.sklearn import MinMaxScaler, MLPClassifier pipeline = Batching( operator=MinMaxScaler() >> MLPClassifier(random_state=42), batch_size=112) trained = pipeline.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test) lale_accuracy = accuracy_score(self.y_test, predictions) from sklearn.preprocessing import MinMaxScaler from sklearn.neural_network import MLPClassifier prep = MinMaxScaler() trained_prep = prep.partial_fit(self.X_train, self.y_train) X_transformed = trained_prep.transform(self.X_train) clf = MLPClassifier(random_state=42) import numpy as np trained_clf = clf.partial_fit(X_transformed, self.y_train, classes=np.unique(self.y_train)) predictions = trained_clf.predict(trained_prep.transform(self.X_test)) sklearn_accuracy = accuracy_score(self.y_test, predictions) self.assertEqual(lale_accuracy, sklearn_accuracy)
def test_fit3(self): from lale.lib.sklearn import MinMaxScaler, MLPClassifier, PCA pipeline = PCA() >> Batching( operator=MinMaxScaler() >> MLPClassifier(random_state=42), batch_size=10) trained = pipeline.fit(self.X_train, self.y_train) predictions = trained.predict(self.X_test)
def test_fit2(self): import warnings warnings.filterwarnings(action="ignore") from lale.lib.sklearn import MinMaxScaler, MLPClassifier pipeline = Batching(operator=MinMaxScaler() >> MinMaxScaler(), batch_size=112) trained = pipeline.fit(self.X_train, self.y_train) lale_transforms = trained.transform(self.X_test) from sklearn.preprocessing import MinMaxScaler prep = MinMaxScaler() trained_prep = prep.partial_fit(self.X_train, self.y_train) X_transformed = trained_prep.transform(self.X_train) clf = MinMaxScaler() import numpy as np trained_clf = clf.partial_fit(X_transformed, self.y_train) sklearn_transforms = trained_clf.transform( trained_prep.transform(self.X_test)) for i in range(5): for j in range(2): self.assertAlmostEqual(lale_transforms[i, j], sklearn_transforms[i, j])
def test_batching_with_hyperopt(self): from lale.lib.lale import Batching, Hyperopt from lale.lib.sklearn import MinMaxScaler, SGDClassifier pipeline = Batching(operator=MinMaxScaler() >> SGDClassifier()) trained = pipeline.auto_configure( self.X_train, self.y_train, optimizer=Hyperopt, max_evals=1 ) _ = trained.predict(self.X_test)
def test_batching_with_hyperopt(self): from lale.lib.sklearn import MinMaxScaler, SGDClassifier from lale.lib.lale import Hyperopt, Batching from sklearn.metrics import accuracy_score pipeline = Batching(operator=MinMaxScaler() >> SGDClassifier()) trained = pipeline.auto_configure(self.X_train, self.y_train, optimizer=Hyperopt, max_evals=1) predictions = trained.predict(self.X_test)
def test_no_partial_fit(self): pipeline = Batching(operator=NoOp() >> LogisticRegression()) with self.assertRaises(AttributeError): trained = pipeline.fit(self.X_train, self.y_train)