コード例 #1
0
    def dont_test_smac_choice(self):

        import numpy as np
        from sklearn import svm, datasets
        from sklearn.model_selection import cross_val_score

        # Import ConfigSpace and different types of parameters
        from smac.configspace import ConfigurationSpace

        # Import SMAC-utilities
        from smac.tae.execute_func import ExecuteTAFuncDict
        from smac.scenario.scenario import Scenario
        from smac.facade.smac_facade import SMAC

        tfm = PCA() | Nystroem() | NoOp()
        planned_pipeline1 = (
            OneHotEncoder(handle_unknown='ignore', sparse=False)
            | NoOp()) >> tfm >> (LogisticRegression() | KNeighborsClassifier())

        cs: ConfigurationSpace = get_smac_space(planned_pipeline1,
                                                lale_num_grids=5)

        # Scenario object
        scenario = Scenario({
            "run_obj":
            "quality",  # we optimize quality (alternatively runtime)
            "runcount-limit": 1,  # maximum function evaluations
            "cs": cs,  # configuration space
            "deterministic": "true"
        })

        # Optimize, using a SMAC-object
        tae = test_iris_fmin_tae(planned_pipeline1, num_folds=2)
        print(
            "Optimizing! Depending on your machine, this might take a few minutes."
        )
        smac = SMAC(scenario=scenario,
                    rng=np.random.RandomState(42),
                    tae_runner=tae)

        incumbent = smac.optimize()

        inc_value = tae(incumbent)

        print("Optimized Value: %.2f" % (inc_value))
コード例 #2
0
    def test_smac(self):

        import numpy as np
        from sklearn import svm, datasets
        from sklearn.model_selection import cross_val_score

        # Import ConfigSpace and different types of parameters
        from smac.configspace import ConfigurationSpace

        # Import SMAC-utilities
        from smac.tae.execute_func import ExecuteTAFuncDict
        from smac.scenario.scenario import Scenario
        from smac.facade.smac_facade import SMAC

        from lale.search.SMAC import get_smac_space

        lr = LogisticRegression()

        cs: ConfigurationSpace = get_smac_space(lr)

        # Scenario object
        scenario = Scenario({
            "run_obj":
            "quality",  # we optimize quality (alternatively runtime)
            "runcount-limit": 1,  # maximum function evaluations
            "cs": cs,  # configuration space
            "deterministic": "true",
            "abort_on_first_run_crash": False
        })

        # Optimize, using a SMAC-object
        tae = test_iris_fmin_tae(lr, num_folds=2)
        print(
            "Optimizing! Depending on your machine, this might take a few minutes."
        )
        smac = SMAC(scenario=scenario,
                    rng=np.random.RandomState(42),
                    tae_runner=tae)

        incumbent = smac.optimize()

        inc_value = tae(incumbent)

        print("Optimized Value: %.2f" % (inc_value))
コード例 #3
0
ファイル: test_optimizers.py プロジェクト: shinnar/lale-gpl
    def dont_test_car_smac(self):
        import numpy as np

        from lale.datasets.auto_weka import fetch_car
        from sklearn.metrics import accuracy_score, make_scorer
        from sklearn.preprocessing import LabelEncoder
        import pandas as pd
        from lale.lib.weka import J48
        from lalegpl.lib.r import ArulesCBAClassifier
        from lale.operators import make_pipeline
        from lale.lib.lale import HyperoptClassifier
        from lale.lib.sklearn import LogisticRegression, KNeighborsClassifier
        from smac.scenario.scenario import Scenario
        from smac.facade.smac_facade import SMAC
        from smac.configspace import ConfigurationSpace

        (X_train, y_train), (X_test, y_test) = fetch_car()
        y_name = y_train.name
        le = LabelEncoder()
        y_train = le.fit_transform(y_train)
        y_test = le.transform(y_test)

        y_train = pd.Series(y_train, name=y_name)
        y_test = pd.Series(y_test, name=y_name)

        #        planned_pipeline = make_pipeline(J48() | ArulesCBAClassifier() | LogisticRegression() | KNeighborsClassifier())
        planned_pipeline = make_pipeline(ArulesCBAClassifier()
                                         | KNeighborsClassifier()
                                         | LogisticRegression())

        cs: ConfigurationSpace = get_smac_space(planned_pipeline)
        print(cs)
        #        X_train = X_train[0:20]
        #        y_train = y_train[0:20]
        # Scenario object
        run_count_limit = 1
        scenario = Scenario({
            "run_obj":
            "quality",  # we optimize quality (alternatively runtime)
            "runcount-limit": run_count_limit,  # maximum function evaluations
            "cs": cs,  # configuration space
            "deterministic": "true",
            "abort_on_first_run_crash": False
        })

        # Optimize, using a SMAC-object
        def f_min(op):
            return test_f_min(op, X_train, y_train, num_folds=2)

        tae = lale_op_smac_tae(planned_pipeline, f_min)

        print(
            "Optimizing! Depending on your machine, this might take a few minutes."
        )
        smac = SMAC(scenario=scenario,
                    rng=np.random.RandomState(42),
                    tae_runner=tae)

        incumbent = smac.optimize()
        trainable_pipeline = lale_trainable_op_from_config(
            planned_pipeline, incumbent)
        trained_pipeline = trainable_pipeline.fit(X_train, y_train)
        pred = trained_pipeline.predict(X_test)
        accuracy = accuracy_score(y_test, pred)
        print("Accuracy: %.2f" % (accuracy))
        inc_value = tae(incumbent)

        print("Optimized Value: %.2f" % (inc_value))
        print(f"Run count limit: {run_count_limit}")