def test_operator_choice(self): self.maxDiff = None from lale.json_operator import from_json, to_json from lale.lib.sklearn import PCA from lale.lib.sklearn import MinMaxScaler as Scl operator = PCA | Scl json_expected = { "class": "lale.operators.OperatorChoice", "operator": "OperatorChoice", "state": "planned", "steps": { "pca": { "class": PCA.class_name(), "state": "planned", "operator": "PCA", "label": "PCA", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html", }, "scl": { "class": Scl.class_name(), "state": "planned", "operator": "MinMaxScaler", "label": "Scl", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.min_max_scaler.html", }, }, } json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json_2, json_expected)
def test_pipeline_1(self): self.maxDiff = None from lale.json_operator import from_json, to_json from lale.lib.lale import ConcatFeatures, NoOp from lale.lib.sklearn import PCA from lale.lib.sklearn import LogisticRegression as LR operator = (PCA & NoOp) >> ConcatFeatures >> LR json_expected = { "class": "lale.operators.PlannedPipeline", "state": "planned", "edges": [ ["pca", "concat_features"], ["no_op", "concat_features"], ["concat_features", "lr"], ], "steps": { "pca": { "class": PCA.class_name(), "state": "planned", "operator": "PCA", "label": "PCA", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html", }, "no_op": { "class": NoOp.class_name(), "state": "trained", "operator": "NoOp", "label": "NoOp", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.no_op.html", "hyperparams": None, "coefs": None, "is_frozen_trainable": True, "is_frozen_trained": True, }, "concat_features": { "class": ConcatFeatures.class_name(), "state": "trained", "operator": "ConcatFeatures", "label": "ConcatFeatures", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.concat_features.html", "hyperparams": None, "coefs": None, "is_frozen_trainable": True, "is_frozen_trained": True, }, "lr": { "class": LR.class_name(), "state": "planned", "operator": "LogisticRegression", "label": "LR", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html", }, }, } json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json, json_2)
def test_higher_order_1(self): from lale.lib.lale import Both from lale.lib.sklearn import PCA, Nystroem from lale.json_operator import from_json operator = Both(op1=PCA(n_components=2), op2=Nystroem) json_expected = { 'class': 'lale.lib.lale.both.BothImpl', 'state': 'trainable', 'operator': 'Both', 'label': 'Both', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.both.html', 'hyperparams': { 'op1': {'$ref': '../steps/pca'}, 'op2': {'$ref': '../steps/nystroem'}}, 'steps': { 'pca': { 'class': 'lale.lib.sklearn.pca.PCAImpl', 'state': 'trainable', 'operator': 'PCA', 'label': 'PCA', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html', 'hyperparams': {'n_components': 2}, 'is_frozen_trainable': False}, 'nystroem': { 'class': 'lale.lib.sklearn.nystroem.NystroemImpl', 'state': 'planned', 'operator': 'Nystroem', 'label': 'Nystroem', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.nystroem.html'}}, 'is_frozen_trainable': False} json = operator.to_json() self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = operator_2.to_json() self.assertEqual(json, json_2)
def test_operator_choice(self): self.maxDiff = None from lale.json_operator import to_json, from_json from lale.lib.sklearn import PCA from lale.lib.sklearn import MinMaxScaler as Scl operator = PCA | Scl json_expected = { 'class': 'lale.operators.OperatorChoice', 'operator': 'OperatorChoice', 'state': 'planned', 'steps': { 'pca': { 'class': 'lale.lib.sklearn.pca.PCAImpl', 'state': 'planned', 'operator': 'PCA', 'label': 'PCA', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html'}, 'scl': { 'class': 'lale.lib.sklearn.min_max_scaler.MinMaxScalerImpl', 'state': 'planned', 'operator': 'MinMaxScaler', 'label': 'Scl', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.min_max_scaler.html'}}} json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json_2, json_expected)
def test_trainable_individual_op(self): self.maxDiff = None from lale.json_operator import from_json, to_json from lale.lib.sklearn import LogisticRegression as LR operator = LR(LR.solver.sag, C=0.1) json_expected = { "class": "lale.lib.sklearn.logistic_regression.LogisticRegressionImpl", "state": "trainable", "operator": "LogisticRegression", "label": "LR", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html", "hyperparams": { "C": 0.1, "solver": "sag" }, "is_frozen_trainable": False, } json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json_2, json_expected)
def test_nested(self): self.maxDiff = None from lale.json_operator import to_json, from_json from lale.lib.lale import NoOp from lale.lib.sklearn import LogisticRegression as LR from lale.lib.sklearn import PCA operator = PCA >> (LR(C=0.09) | NoOp >> LR(C=0.19)) json_expected = { 'class': 'lale.operators.PlannedPipeline', 'state': 'planned', 'edges': [['pca', 'choice']], 'steps': { 'pca': { 'class': 'lale.lib.sklearn.pca.PCAImpl', 'state': 'planned', 'operator': 'PCA', 'label': 'PCA', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html'}, 'choice': { 'class': 'lale.operators.OperatorChoice', 'state': 'planned', 'operator': 'OperatorChoice', 'steps': { 'lr_0': { 'class': 'lale.lib.sklearn.logistic_regression.LogisticRegressionImpl', 'state': 'trainable', 'operator': 'LogisticRegression', 'label': 'LR', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html', 'hyperparams': {'C': 0.09}, 'is_frozen_trainable': False}, 'pipeline_1': { 'class': 'lale.operators.TrainablePipeline', 'state': 'trainable', 'edges': [['no_op', 'lr_1']], 'steps': { 'no_op': { 'class': 'lale.lib.lale.no_op.NoOpImpl', 'state': 'trained', 'operator': 'NoOp', 'label': 'NoOp', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.no_op.html', 'hyperparams': None, 'coefs': None, 'is_frozen_trainable': True, 'is_frozen_trained': True}, 'lr_1': { 'class': 'lale.lib.sklearn.logistic_regression.LogisticRegressionImpl', 'state': 'trainable', 'operator': 'LogisticRegression', 'label': 'LR', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html', 'hyperparams': {'C': 0.19}, 'is_frozen_trainable': False}}}}}}} json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json, json_2)
def test_higher_order_1(self): from lale.json_operator import from_json from lale.lib.lale import Both from lale.lib.sklearn import PCA, Nystroem operator = Both(op1=PCA(n_components=2), op2=Nystroem) json_expected = { "class": Both.class_name(), "state": "trainable", "operator": "Both", "label": "Both", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.both.html", "hyperparams": { "op1": { "$ref": "../steps/pca" }, "op2": { "$ref": "../steps/nystroem" }, }, "steps": { "pca": { "class": PCA.class_name(), "state": "trainable", "operator": "PCA", "label": "PCA", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html", "hyperparams": { "n_components": 2 }, "is_frozen_trainable": False, }, "nystroem": { "class": Nystroem.class_name(), "state": "planned", "operator": "Nystroem", "label": "Nystroem", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.nystroem.html", }, }, "is_frozen_trainable": False, } json = operator.to_json() self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = operator_2.to_json() self.assertEqual(json, json_2)
def test_higher_order_2(self): self.maxDiff = None from lale.lib.sklearn import VotingClassifier as Vote from lale.lib.sklearn import KNeighborsClassifier as KNN from lale.lib.sklearn import PCA from lale.lib.sklearn import LogisticRegression as LR from lale.json_operator import from_json operator = Vote(estimators=[('knn',KNN), ('pipeline',PCA()>>LR)], voting='soft') json_expected = { 'class': 'lale.lib.sklearn.voting_classifier.VotingClassifierImpl', 'state': 'trainable', 'operator': 'VotingClassifier', 'is_frozen_trainable': True, 'label': 'Vote', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.voting_classifier.html', 'hyperparams': { 'estimators': [ ('knn', {'$ref': '../steps/knn'}), ('pipeline', {'$ref': '../steps/pipeline'})], 'voting': 'soft'}, 'steps': { 'knn': { 'class': 'lale.lib.sklearn.k_neighbors_classifier.KNeighborsClassifierImpl', 'state': 'planned', 'operator': 'KNeighborsClassifier', 'label': 'KNN', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.k_neighbors_classifier.html'}, 'pipeline': { 'class': 'lale.operators.PlannedPipeline', 'state': 'planned', 'edges': [['pca', 'lr']], 'steps': { 'pca': { 'class': 'lale.lib.sklearn.pca.PCAImpl', 'state': 'trainable', 'operator': 'PCA', 'label': 'PCA', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html', 'hyperparams': {}, 'is_frozen_trainable': False}, 'lr': { 'class': 'lale.lib.sklearn.logistic_regression.LogisticRegressionImpl', 'state': 'planned', 'operator': 'LogisticRegression', 'label': 'LR', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html'}}}}} json = operator.to_json() self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = operator_2.to_json() self.assertEqual(json, json_2)
def test_customize_schema(self): from lale.json_operator import from_json, to_json from lale.lib.sklearn import LogisticRegression as LR operator = LR.customize_schema( solver={ "enum": ["lbfgs", "liblinear"], "default": "liblinear" }, tol={ "type": "number", "minimum": 0.00001, "maximum": 0.1, "default": 0.0001, }, ) json_expected = { "class": LR.class_name(), "state": "planned", "operator": "LogisticRegression", "label": "LR", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html", "customize_schema": { "properties": { "hyperparams": { "allOf": [{ "solver": { "default": "liblinear", "enum": ["lbfgs", "liblinear"], }, "tol": { "type": "number", "minimum": 0.00001, "maximum": 0.1, "default": 0.0001, }, }] } } }, } json = to_json(operator) self.maxDiff = None self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json, json_2)
def test_pipeline_2(self): from lale.lib.lale import NoOp from lale.lib.sklearn import Nystroem from lale.lib.sklearn import PCA from lale.lib.sklearn import LogisticRegression from lale.lib.sklearn import KNeighborsClassifier from lale.operators import make_choice, make_pipeline from lale.json_operator import to_json, from_json kernel_tfm_or_not = make_choice(NoOp, Nystroem) tfm = PCA clf = make_choice(LogisticRegression, KNeighborsClassifier) operator = make_pipeline(kernel_tfm_or_not, tfm, clf) json = to_json(operator) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json, json_2)
def test_pipeline_1(self): self.maxDiff = None from lale.json_operator import to_json, from_json from lale.lib.lale import ConcatFeatures, NoOp from lale.lib.sklearn import LogisticRegression as LR from lale.lib.sklearn import PCA operator = (PCA & NoOp) >> ConcatFeatures >> LR json_expected = { 'class': 'lale.operators.PlannedPipeline', 'state': 'planned', 'edges': [ ['pca', 'concat_features'], ['no_op', 'concat_features'], ['concat_features', 'lr']], 'steps': { 'pca': { 'class': 'lale.lib.sklearn.pca.PCAImpl', 'state': 'planned', 'operator': 'PCA', 'label': 'PCA', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html'}, 'no_op': { 'class': 'lale.lib.lale.no_op.NoOpImpl', 'state': 'trained', 'operator': 'NoOp', 'label': 'NoOp', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.no_op.html', 'hyperparams': None, 'coefs': None, 'is_frozen_trainable': True, 'is_frozen_trained': True}, 'concat_features': { 'class': 'lale.lib.lale.concat_features.ConcatFeaturesImpl', 'state': 'trained', 'operator': 'ConcatFeatures', 'label': 'ConcatFeatures', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.concat_features.html', 'hyperparams': None, 'coefs': None, 'is_frozen_trainable': True, 'is_frozen_trained': True}, 'lr': { 'class': 'lale.lib.sklearn.logistic_regression.LogisticRegressionImpl', 'state': 'planned', 'operator': 'LogisticRegression', 'label': 'LR', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html'}}} json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json, json_2)
def test_trainable_individual_op(self): self.maxDiff = None from lale.json_operator import to_json, from_json from lale.lib.sklearn import LogisticRegression as LR operator = LR(LR.solver.sag, C=0.1) json_expected = { 'class': 'lale.lib.sklearn.logistic_regression.LogisticRegressionImpl', 'state': 'trainable', 'operator': 'LogisticRegression', 'label': 'LR', 'documentation_url': 'https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html', 'hyperparams': {'C': 0.1, 'solver': 'sag'}, 'is_frozen_trainable': False} json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json_2, json_expected)
def optimize(pipeline_spec): # load training data (X_train, y_train), (X_test, y_test) = openml_datasets.fetch("credit-g", "classification") optimizable = from_json(pipeline_spec) optimized = optimizable.fit(X_train, y_train) # predict # predicted = optimized.predict(X_test) # score = metrics.accuracy_score(y_test, predicted) return [ { "id": x, "pipeline": optimized.get_pipeline(x).to_json(), "metrics": { "loss": optimized.summary()["loss"][x], "accuracy": metrics.accuracy_score(y_test, optimized.get_pipeline(x).fit(X_train, y_train).predict(X_test)).item(), "f1": metrics.f1_score(y_test, optimized.get_pipeline(x).fit(X_train, y_train).predict(X_test)).item(), "precision": metrics.precision_score(y_test, optimized.get_pipeline(x).fit(X_train, y_train).predict(X_test)).item(), "recall": metrics.recall_score(y_test, optimized.get_pipeline(x).fit(X_train, y_train).predict(X_test)).item(), "roc_auc": metrics.roc_auc_score(y_test, optimized.get_pipeline(x).fit(X_train, y_train).predict(X_test)).item() } } for x in list(optimized.summary().index) ]
def test_nested(self): self.maxDiff = None from lale.json_operator import from_json, to_json from lale.lib.lale import NoOp from lale.lib.sklearn import PCA from lale.lib.sklearn import LogisticRegression as LR operator = PCA >> (LR(C=0.09) | NoOp >> LR(C=0.19)) json_expected = { "class": "lale.operators.PlannedPipeline", "state": "planned", "edges": [["pca", "choice"]], "steps": { "pca": { "class": PCA.class_name(), "state": "planned", "operator": "PCA", "label": "PCA", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html", }, "choice": { "class": "lale.operators.OperatorChoice", "state": "planned", "operator": "OperatorChoice", "steps": { "lr_0": { "class": LR.class_name(), "state": "trainable", "operator": "LogisticRegression", "label": "LR", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html", "hyperparams": {"C": 0.09}, "is_frozen_trainable": False, }, "pipeline_1": { "class": "lale.operators.TrainablePipeline", "state": "trainable", "edges": [["no_op", "lr_1"]], "steps": { "no_op": { "class": NoOp.class_name(), "state": "trained", "operator": "NoOp", "label": "NoOp", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.lale.no_op.html", "hyperparams": None, "coefs": None, "is_frozen_trainable": True, "is_frozen_trained": True, }, "lr_1": { "class": LR.class_name(), "state": "trainable", "operator": "LogisticRegression", "label": "LR", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html", "hyperparams": {"C": 0.19}, "is_frozen_trainable": False, }, }, }, }, }, }, } json = to_json(operator) self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = to_json(operator_2) self.assertEqual(json, json_2)
def test_higher_order_2(self): self.maxDiff = None from lale.json_operator import from_json from lale.lib.sklearn import PCA from lale.lib.sklearn import KNeighborsClassifier as KNN from lale.lib.sklearn import LogisticRegression as LR from lale.lib.sklearn import VotingClassifier as Vote operator = Vote( estimators=[("knn", KNN), ("pipeline", PCA() >> LR)], voting="soft" ) json_expected = { "class": Vote.class_name(), "state": "trainable", "operator": "VotingClassifier", "is_frozen_trainable": True, "label": "Vote", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.voting_classifier.html", "hyperparams": { "estimators": [ ("knn", {"$ref": "../steps/knn"}), ("pipeline", {"$ref": "../steps/pipeline"}), ], "voting": "soft", }, "steps": { "knn": { "class": KNN.class_name(), "state": "planned", "operator": "KNeighborsClassifier", "label": "KNN", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.k_neighbors_classifier.html", }, "pipeline": { "class": "lale.operators.PlannedPipeline", "state": "planned", "edges": [["pca", "lr"]], "steps": { "pca": { "class": PCA.class_name(), "state": "trainable", "operator": "PCA", "label": "PCA", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.pca.html", "hyperparams": {}, "is_frozen_trainable": False, }, "lr": { "class": LR.class_name(), "state": "planned", "operator": "LogisticRegression", "label": "LR", "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.logistic_regression.html", }, }, }, }, } json = operator.to_json() self.assertEqual(json, json_expected) operator_2 = from_json(json) json_2 = operator_2.to_json() self.assertEqual(json, json_2)