예제 #1
0
    optimizer="random_grid_search",
    optimizer_params={"n_configurations": 10},
    metrics=["accuracy", "precision", "recall"],
    best_config_metric="recall",
    outer_cv=KFold(n_splits=3),
    inner_cv=KFold(n_splits=5),
    verbosity=1,
    output_settings=OutputSettings(project_folder="./tmp/"),
)


# ADD ELEMENTS TO YOUR PIPELINE
my_pipe += PipelineElement("StandardScaler")

my_pipe += PipelineElement(
    "PCA", hyperparameters={"n_components": IntegerRange(5, 20)}, test_disabled=True
)

my_pipe += PipelineElement(
    "ImbalancedDataTransformer",
    hyperparameters={"method_name": Categorical(["RandomUnderSampler", "SMOTE"])},
    test_disabled=True,
)

my_pipe += PipelineElement(
    "SVC",
    hyperparameters={"kernel": Categorical(["rbf", "linear"]), "C": FloatRange(0.5, 2)},
)


# NOW TRAIN YOUR PIPELINE
예제 #2
0
 def setUp(self):
     self.scaler = PipelineElement('StandardScaler', test_disabled=True)
     self.pca = PipelineElement('PCA', {'n_components': IntegerRange(1, 3)})
     self.svc = PipelineElement('SVC', {'C': [0.1, 1], 'kernel': ['rbf', 'sigmoid']})
     self.rf = PipelineElement("RandomForestClassifier")  # no hyperparameter
     self.pipeline_elements = [self.scaler, self.pca, self.svc, self.rf]
예제 #3
0
    verbosity=1,
    output_settings=settings,
)

preprocessing = Preprocessing()
preprocessing += PipelineElement("LabelEncoder", batch_size=10)

my_pipe += preprocessing

# ADD ELEMENTS TO YOUR PIPELINE
# first normalize all features
my_pipe.add(PipelineElement("StandardScaler"))
# then do feature selection using a PCA, specify which values to try in the hyperparameter search
my_pipe += PipelineElement(
    "PCA",
    hyperparameters={"n_components": IntegerRange(5, 10)},
    test_disabled=True)
# engage and optimize the good old SVM for Classification
my_pipe += PipelineElement(
    "SVC",
    hyperparameters={
        "kernel": Categorical(["rbf", "linear"]),
        "C": FloatRange(0.5, 1)
    },
    gamma="scale",
)

# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)

debug = True
예제 #4
0
my_pipe = Hyperpipe('feature_selection',
                    optimizer='grid_search',
                    metrics=[
                        'mean_squared_error', 'pearson_correlation',
                        'mean_absolute_error', 'explained_variance'
                    ],
                    best_config_metric='mean_squared_error',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=3),
                    verbosity=1,
                    output_settings=OutputSettings(project_folder='./tmp/'))

my_pipe += PipelineElement('StandardScaler')

lasso = PipelineElement('LassoFeatureSelection',
                        hyperparameters={
                            'percentile_to_keep': [0.1, 0.2, 0.3],
                            'alpha': 1
                        })

f_regression = PipelineElement('FRegressionSelectPercentile',
                               hyperparameters={'percentile': [10, 20, 30]})

my_pipe += Switch('FeatureSelection', [lasso, f_regression])

my_pipe += PipelineElement(
    'RandomForestRegressor',
    hyperparameters={'n_estimators': IntegerRange(10, 50)})

my_pipe.fit(X, y)
예제 #5
0
    outer_cv=KFold(n_splits=3),
    inner_cv=KFold(n_splits=5),
    verbosity=1,
    output_settings=settings)

preprocessing = Preprocessing()
preprocessing += PipelineElement("LabelEncoder", batch_size=10)

my_pipe += preprocessing

# ADD ELEMENTS TO YOUR PIPELINE
# first normalize all features
my_pipe.add(PipelineElement('StandardScaler'))
# then do feature selection using a PCA, specify which values to try in the hyperparameter search
my_pipe += PipelineElement(
    'PCA',
    hyperparameters={'n_components': IntegerRange(5, 10)},
    test_disabled=True)
# engage and optimize the good old SVM for Classification
my_pipe += PipelineElement('SVC',
                           hyperparameters={
                               'kernel': Categorical(['rbf', 'linear']),
                               'C': FloatRange(0.5, 1)
                           },
                           gamma='scale')

# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)

debug = True
예제 #6
0
my_pipe += PipelineElement('SimpleImputer')
my_pipe += PipelineElement('StandardScaler', {}, with_mean=True)

# Use only "mean" features: [mean_radius, mean_texture, mean_perimeter, mean_area, mean_smoothness, mean_compactness,
# mean_concavity, mean_concave_points, mean_symmetry, mean_fractal_dimension
mean_branch = Branch('MeanFeature')
mean_branch += DataFilter(indices=np.arange(10))
mean_branch += PipelineElement('SVC', {'C': FloatRange(0.1, 10)},
                               kernel='linear')

# Use only "error" features
error_branch = Branch('ErrorFeature')
error_branch += DataFilter(indices=np.arange(10, 20))
error_branch += PipelineElement('SVC', {'C': Categorical([100, 1000, 1000])},
                                kernel='linear')

# use only "worst" features: [worst_radius, worst_texture, ..., worst_fractal_dimension]
worst_branch = Branch('WorstFeature')
worst_branch += DataFilter(indices=np.arange(20, 30))
worst_branch += PipelineElement('SVC')

my_pipe += Stack('SourceStack', [mean_branch, error_branch, worst_branch])

my_pipe += Switch('EstimatorSwitch', [
    PipelineElement('RandomForestClassifier',
                    {'n_estimators': IntegerRange(2, 5)}),
    PipelineElement('SVC')
])

my_pipe.fit(X, y)
예제 #7
0
    def test_regression_4(self):
        for original_hyperpipe in self.hyperpipes:
            pipe = original_hyperpipe.copy_me()

            # Transformer Switch
            my_switch = Switch('trans_switch')
            my_switch += PipelineElement('PCA')
            my_switch += PipelineElement('FRegressionSelectPercentile', hyperparameters={'percentile': IntegerRange(start=5, step=20, stop=66, range_type='range')}, test_disabled=True)
            pipe += my_switch
            pipe += PipelineElement('RandomForestRegressor')

            self.run_hyperpipe(pipe, self.regression)
예제 #8
0
    def test_against_smac(self):
        # PHOTON implementation
        self.pipe.add(PipelineElement('StandardScaler'))
        # then do feature selection using a PCA, specify which values to try in the hyperparameter search
        self.pipe += PipelineElement(
            'PCA', hyperparameters={'n_components': IntegerRange(5, 30)})
        # engage and optimize the good old SVM for Classification
        self.pipe += PipelineElement(
            'SVC',
            hyperparameters={
                'kernel': Categorical(["linear", "rbf", 'poly', 'sigmoid']),
                'C': FloatRange(0.5, 200)
            },
            gamma='auto')

        self.X, self.y = self.simple_classification()
        self.pipe.fit(self.X, self.y)

        # AUTO ML direct
        # Build Configuration Space which defines all parameters and their ranges
        cs = ConfigurationSpace()

        # We define a few possible types of SVM-kernels and add them as "kernel" to our cs
        n_components = UniformIntegerHyperparameter("PCA__n_components", 5,
                                                    30)  # , default_value=5)
        cs.add_hyperparameter(n_components)

        kernel = CategoricalHyperparameter(
            "SVC__kernel",
            ["linear", "rbf", 'poly', 'sigmoid'])  #, default_value="linear")
        cs.add_hyperparameter(kernel)

        c = UniformFloatHyperparameter("SVC__C", 0.5, 200)  #, default_value=1)
        cs.add_hyperparameter(c)

        # Scenario object
        scenario = Scenario({
            "run_obj":
            "quality",  # we optimize quality (alternatively runtime)
            "runcount-limit": 800,  # maximum function evaluations
            "cs": cs,  # configuration space
            "deterministic": "true",
            "shared_model": "false",  # !!!!
            "wallclock_limit": self.time_limit
        })

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

        self.traurig = smac

        incumbent = smac.optimize()

        inc_value = self.objective_function(incumbent)

        print(incumbent)
        print(inc_value)

        runhistory_photon = self.smac_helper["data"].solver.runhistory
        runhistory_original = smac.solver.runhistory

        x_ax = range(
            1,
            min(len(runhistory_original.cost_per_config.keys()),
                len(runhistory_photon.cost_per_config.keys())) + 1)
        y_ax_original = [
            runhistory_original.cost_per_config[tmp] for tmp in x_ax
        ]
        y_ax_photon = [runhistory_photon.cost_per_config[tmp] for tmp in x_ax]

        y_ax_original_inc = [min(y_ax_original[:tmp + 1]) for tmp in x_ax]
        y_ax_photon_inc = [min(y_ax_photon[:tmp + 1]) for tmp in x_ax]

        plt.figure(figsize=(10, 7))
        plt.plot(x_ax, y_ax_original, 'g', label='Original')
        plt.plot(x_ax, y_ax_photon, 'b', label='PHOTON')
        plt.plot(x_ax, y_ax_photon_inc, 'r', label='PHOTON Incumbent')
        plt.plot(x_ax, y_ax_original_inc, 'k', label='Original Incumbent')
        plt.title('Photon Prove')
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.legend(loc='best')
        plt.show()

        def neighbours(items, fill=None):
            before = itertools.chain([fill], items)
            after = itertools.chain(
                items,
                [fill])  # You could use itertools.zip_longest() later instead.
            next(after)
            for a, b, c in zip(before, items, after):
                yield [value for value in (a, b, c) if value is not fill]

        print("---------------")
        original_pairing = [
            sum(values) / len(values) for values in neighbours(y_ax_original)
        ]
        bias_term = np.mean([
            abs(y_ax_original_inc[t] - y_ax_photon_inc[t])
            for t in range(len(y_ax_photon_inc))
        ])
        photon_pairing = [
            sum(values) / len(values) - bias_term
            for values in neighbours(y_ax_photon)
        ]
        counter = 0
        for i, x in enumerate(x_ax):
            if abs(original_pairing[i] - photon_pairing[i]) > 0.05:
                counter += 1
            self.assertLessEqual(counter / len(x_ax), 0.15)
예제 #9
0
파일: stack.py 프로젝트: TimHahn1981/photon
from photonai.optimization import FloatRange, IntegerRange

X, y = load_breast_cancer(True)

my_pipe = Hyperpipe('basic_stack_pipe',
                    optimizer='sk_opt',
                    optimizer_params={'n_configurations': 5},
                    metrics=['accuracy', 'precision', 'recall'],
                    best_config_metric='accuracy',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=3),
                    verbosity=1,
                    output_settings=OutputSettings(project_folder='./tmp/'))

my_pipe += PipelineElement('StandardScaler')

tree = PipelineElement('DecisionTreeClassifier',
                       hyperparameters={
                           'criterion': ['gini'],
                           'min_samples_split': IntegerRange(2, 4)
                       })

svc = PipelineElement('LinearSVC', hyperparameters={'C': FloatRange(0.5, 25)})

# for a stack that includes estimators you can choose whether predict or predict_proba is called for all estimators
# in case only some implement predict_proba, predict is called for the remaining estimators
my_pipe += Stack('final_stack', [tree, svc], use_probabilities=True)

my_pipe += PipelineElement('LinearSVC')
my_pipe.fit(X, y)
예제 #10
0
# Use only "mean" features: [mean_radius, mean_texture, mean_perimeter, mean_area, mean_smoothness, mean_compactness,
# mean_concavity, mean_concave_points, mean_symmetry, mean_fractal_dimension
mean_branch = Branch("MeanFeature")
mean_branch += DataFilter(indices=np.arange(10))
mean_branch += PipelineElement("SVC", {"C": FloatRange(0.1, 10)}, kernel="linear")

# Use only "error" features
error_branch = Branch("ErrorFeature")
error_branch += DataFilter(indices=np.arange(10, 20))
error_branch += PipelineElement(
    "SVC", {"C": Categorical([100, 1000, 1000])}, kernel="linear"
)

# use only "worst" features: [worst_radius, worst_texture, ..., worst_fractal_dimension]
worst_branch = Branch("WorstFeature")
worst_branch += DataFilter(indices=np.arange(20, 30))
worst_branch += PipelineElement("SVC")

my_pipe += Stack("SourceStack", [mean_branch, error_branch, worst_branch])

my_pipe += Switch(
    "EstimatorSwitch",
    [
        PipelineElement("RandomForestClassifier", {"n_estimators": IntegerRange(2, 5)}),
        PipelineElement("SVC"),
    ],
)

my_pipe.fit(X, y)
예제 #11
0
from photonai.base import Hyperpipe, PipelineElement, OutputSettings
from photonai.optimization import IntegerRange

# WE USE THE BREAST CANCER SET FROM SKLEARN
X, y = load_breast_cancer(True)

# DESIGN YOUR PIPELINE
my_pipe = Hyperpipe('basic_svm_pipe',
                    optimizer='sk_opt',
                    optimizer_params={'n_configurations': 10},
                    metrics=['accuracy', 'precision', 'recall', 'balanced_accuracy'],
                    best_config_metric='accuracy',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=3),
                    verbosity=1,
                    output_settings=OutputSettings(project_folder='./tmp/'))


# ADD ELEMENTS TO YOUR PIPELINE
my_pipe.add(PipelineElement('StandardScaler'))

my_pipe += PipelineElement('PhotonMLPClassifier', hyperparameters={'layer_1': IntegerRange(0, 5),
                                                                   'layer_2': IntegerRange(0, 5),
                                                                   'layer_3': IntegerRange(0, 5)})

# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)



예제 #12
0
    def test_save_optimum_pipe(self):
        # todo: test .save() of custom model
        tmp_path = os.path.join(self.tmp_folder_path, 'optimum_pipypipe')
        settings = OutputSettings(project_folder=tmp_path,
                                  overwrite_results=True)

        my_pipe = Hyperpipe('hyperpipe',
                            optimizer='random_grid_search',
                            optimizer_params={'n_configurations': 3},
                            metrics=['accuracy', 'precision', 'recall'],
                            best_config_metric='f1_score',
                            outer_cv=KFold(n_splits=2),
                            inner_cv=KFold(n_splits=2),
                            verbosity=1,
                            output_settings=settings)

        preproc = Preprocessing()
        preproc += PipelineElement('StandardScaler')

        # BRANCH WITH QUANTILTRANSFORMER AND DECISIONTREECLASSIFIER
        tree_qua_branch = Branch('tree_branch')
        tree_qua_branch += PipelineElement('QuantileTransformer')
        tree_qua_branch += PipelineElement(
            'DecisionTreeClassifier',
            {'min_samples_split': IntegerRange(2, 4)},
            criterion='gini')

        # BRANCH WITH MinMaxScaler AND DecisionTreeClassifier
        svm_mima_branch = Branch('svm_branch')
        svm_mima_branch += PipelineElement('MinMaxScaler')
        svm_mima_branch += PipelineElement(
            'SVC', {
                'kernel': Categorical(['rbf', 'linear']),
                'C': 2.0
            },
            gamma='auto')

        # BRANCH WITH StandardScaler AND KNeighborsClassifier
        knn_sta_branch = Branch('neighbour_branch')
        knn_sta_branch += PipelineElement.create("dummy", DummyTransformer(),
                                                 {})
        knn_sta_branch += PipelineElement('KNeighborsClassifier')

        my_pipe += preproc
        # voting = True to mean the result of every branch
        my_pipe += Stack('final_stack',
                         [tree_qua_branch, svm_mima_branch, knn_sta_branch])

        my_pipe += PipelineElement('LogisticRegression', solver='lbfgs')

        my_pipe.fit(self.__X, self.__y)
        model_path = os.path.join(my_pipe.output_settings.results_folder,
                                  'photon_best_model.photon')
        self.assertTrue(os.path.exists(model_path))

        # now move optimum pipe to new folder
        test_folder = os.path.join(my_pipe.output_settings.results_folder,
                                   'new_test_folder')
        new_model_path = os.path.join(test_folder, 'photon_best_model.photon')
        os.makedirs(test_folder)
        shutil.copyfile(model_path, new_model_path)

        # check if load_optimum_pipe also works
        # check if we have the meta information recovered
        loaded_optimum_pipe = Hyperpipe.load_optimum_pipe(new_model_path)
        self.assertIsNotNone(loaded_optimum_pipe._meta_information)
        self.assertIsNotNone(
            loaded_optimum_pipe._meta_information['photon_version'])

        # check if predictions stay realiably the same
        y_pred_loaded = loaded_optimum_pipe.predict(self.__X)
        y_pred = my_pipe.optimum_pipe.predict(self.__X)
        np.testing.assert_array_equal(y_pred_loaded, y_pred)
예제 #13
0
    metrics=["accuracy"],
    best_config_metric="accuracy",
    inner_cv=KFold(n_splits=3),
    cache_folder=cache_grid_folder_path,
    verbosity=0,
)

pipeline_elements = []

# scaling
pipeline_elements.append(PipelineElement("StandardScaler", test_disabled=True))
pipeline_elements.append(PipelineElement("Normalizer", test_disabled=True))

prepro_switch = Switch("PreprocSwitch")
prepro_switch += PipelineElement(
    "PCA", hyperparameters={"n_components": IntegerRange(5, 30)})
prepro_switch += PipelineElement(
    "RandomTreesEmbedding",
    hyperparameters={
        "n_estimators": IntegerRange(10, 30),
        "max_depth": IntegerRange(3, 6),
    },
)
prepro_switch += PipelineElement(
    "SelectPercentile", hyperparameters={"percentile": IntegerRange(5, 15)})
# prepro_switch += PipelineElement('FastICA', hyperparameters={'algorithm': Categorical(['parallel', 'deflation'])})

estimator_switch = Switch("EstimatorSwitch")
estimator_switch += PipelineElement(
    "SVC",
    hyperparameters={
예제 #14
0
파일: driver.py 프로젝트: nkourkou/photon
# DESIGN YOUR PIPELINE
settings = OutputSettings(project_folder="./tmp/")


my_pipe = Hyperpipe('batching',
                    optimizer='sk_opt',
#                    optimizer_params={'n_configurations': 25},
                    metrics=['accuracy', 'precision', 'recall', 'balanced_accuracy'],
                    # forced to 0th element in metrics
                    best_config_metric='accuracy',
                    outer_cv=KFold(n_splits=2),
                    inner_cv=KFold(n_splits=2),
                    verbosity=1,
                    output_settings=settings)

# ADD ELEMENTS TO YOUR PIPELINE
my_pipe += PipelineElement("StandardScaler")

my_pipe += PipelineElement('RandomForestClassifier', hyperparameters={
                                                    'n_estimators': IntegerRange(2, 1999),
                                                    },random_state=777)

# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)

debug = True

####        # ====================== Data ===========================
#    self.data = Hyperpipe.Data()
# self.results = None
#self.best_config = None
예제 #15
0
                    inner_cv=KFold(n_splits=10),
                    outer_cv=KFold(n_splits=5),
                    metrics=['balanced_accuracy', 'precision'],
                    best_config_metric='balanced_accuracy',
                    output_settings=OutputSettings(project_folder='./tmp'))

my_pipe += PipelineElement("SimpleImputer")

switch = Switch("my_copy_switch")
switch += PipelineElement("StandardScaler")
switch += PipelineElement("RobustScaler", test_disabled=True)
my_pipe += switch

stack = Stack("EstimatorStack")

branch1 = Branch("SVMBranch")
branch1 += PipelineElement("PCA", hyperparameters={'n_components': IntegerRange(5, 10)})
branch1 += PipelineElement("SVC")

branch2 = Branch('TreeBranch')
branch2 += PipelineElement("PCA", hyperparameters={'n_components': IntegerRange(5, 10)})
branch2 += PipelineElement("DecisionTreeClassifier")

stack += branch1
stack += branch2
my_pipe += stack

my_pipe += PipelineElement("PhotonVotingClassifier")

my_pipe.fit(X, y)
예제 #16
0
        def test_photon_implementation_switch(self):
            # PHOTON implementation
            self.pipe.add(PipelineElement('StandardScaler'))
            self.pipe += PipelineElement(
                'PCA', hyperparameters={'n_components': IntegerRange(5, 30)})
            estimator_siwtch = Switch("Estimator")
            estimator_siwtch += PipelineElement('SVC',
                                                hyperparameters={
                                                    'kernel':
                                                    Categorical(
                                                        ["rbf", 'poly']),
                                                    'C':
                                                    FloatRange(0.5, 200)
                                                },
                                                gamma='auto')
            estimator_siwtch += PipelineElement('RandomForestClassifier',
                                                hyperparameters={
                                                    'criterion':
                                                    Categorical(
                                                        ['gini', 'entropy']),
                                                    'min_samples_split':
                                                    IntegerRange(2, 4)
                                                })
            self.pipe += estimator_siwtch
            self.X, self.y = self.simple_classification()
            self.pipe.fit(self.X, self.y)

            # direct AUTO ML implementation

            # Build Configuration Space which defines all parameters and their ranges
            cs = ConfigurationSpace()
            n_components = UniformIntegerHyperparameter(
                "PCA__n_components", 5, 30)
            cs.add_hyperparameter(n_components)

            switch = CategoricalHyperparameter("Estimator_switch",
                                               ['svc', 'rf'])
            cs.add_hyperparameter(switch)

            kernel = CategoricalHyperparameter("SVC__kernel", ["rbf", 'poly'])
            cs.add_hyperparameter(kernel)
            c = UniformFloatHyperparameter("SVC__C", 0.5, 200)
            cs.add_hyperparameter(c)
            use_svc_c = InCondition(child=kernel,
                                    parent=switch,
                                    values=["svc"])
            use_svc_kernel = InCondition(child=c,
                                         parent=switch,
                                         values=["svc"])

            criterion = CategoricalHyperparameter(
                "RandomForestClassifier__criterion", ['gini', 'entropy'])
            cs.add_hyperparameter(criterion)
            minsplit = UniformIntegerHyperparameter(
                "RandomForestClassifier__min_samples_split", 2, 4)
            cs.add_hyperparameter(minsplit)

            use_rf_crit = InCondition(child=criterion,
                                      parent=switch,
                                      values=["rf"])
            use_rf_minsplit = InCondition(child=minsplit,
                                          parent=switch,
                                          values=["rf"])

            cs.add_conditions(
                [use_svc_c, use_svc_kernel, use_rf_crit, use_rf_minsplit])

            # Scenario object
            scenario = Scenario({
                "run_obj": "quality",
                "cs": cs,
                "deterministic": "true",
                "wallclock_limit": self.time_limit,
                "limit_resources": False,
                'abort_on_first_run_crash': False
            })

            # Optimize, using a SMAC directly
            smac = SMAC4HPO(scenario=scenario,
                            rng=42,
                            tae_runner=self.objective_function_switch)
            _ = smac.optimize()

            runhistory_photon = self.smac_helper["data"].solver.runhistory
            runhistory_original = smac.solver.runhistory

            x_ax = range(
                1,
                min(len(runhistory_original._cost_per_config.keys()),
                    len(runhistory_photon._cost_per_config.keys())) + 1)
            y_ax_original = [
                runhistory_original._cost_per_config[tmp] for tmp in x_ax
            ]
            y_ax_photon = [
                runhistory_photon._cost_per_config[tmp] for tmp in x_ax
            ]

            min_len = min(len(y_ax_original), len(y_ax_photon))
            self.assertLessEqual(
                np.max(
                    np.abs(
                        np.array(y_ax_original[:min_len]) -
                        np.array(y_ax_photon[:min_len]))), 0.01)
예제 #17
0
    def test_NumberRange(self):
        """
        Test for class IntegerRange and FloatRange.
        """
        dtypes = {int: np.int32, float: np.float32}

        expected_linspace = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        number_range_linspace = IntegerRange(start=1, stop=9, num=9, range_type="linspace")
        number_range_linspace.transform()
        self.assertListEqual(expected_linspace, number_range_linspace.values)

        expected_geomspace = [1, 10, 100, 1000, 10000]
        number_range_geomspace = IntegerRange(1, 10000, num=5, range_type="geomspace")
        number_range_geomspace.transform()
        self.assertListEqual(expected_geomspace, number_range_geomspace.values)

        number_range_range = IntegerRange(self.start, self.end, step=2, range_type="range")
        number_range_range.transform()
        self.assertListEqual(number_range_range.values, list(np.arange(self.start, self.end, 2)))

        number_range_logspace = FloatRange(-1, 1, num=50, range_type='logspace')
        number_range_logspace.transform()
        np.testing.assert_array_almost_equal(number_range_logspace.values,  np.logspace(-1, 1, num=50).tolist())

        # error tests
        with self.assertRaises(ValueError):
            number_range = IntegerRange(start=0, stop=self.end, range_type="geomspace")
            number_range.transform()

        with self.assertRaises(ValueError):
            number_range = IntegerRange(start=1, stop=15, range_type="logspace")
            number_range.transform()

        with self.assertRaises(ValueError):
            IntegerRange(start=self.start, stop=self.end, range_type="ownspace")
예제 #18
0
        def test_photon_implementation_simple(self):
            # PHOTON implementation
            self.pipe.add(PipelineElement('StandardScaler'))
            self.pipe += PipelineElement(
                'PCA', hyperparameters={'n_components': IntegerRange(5, 30)})
            self.pipe += PipelineElement('SVC',
                                         hyperparameters={
                                             'kernel':
                                             Categorical(["rbf", 'poly']),
                                             'C': FloatRange(0.5, 200)
                                         },
                                         gamma='auto')
            self.X, self.y = self.simple_classification()
            self.pipe.fit(self.X, self.y)

            # direct AUTO ML implementation
            # Build Configuration Space which defines all parameters and their ranges
            cs = ConfigurationSpace()
            n_components = UniformIntegerHyperparameter(
                "PCA__n_components", 5, 30)
            cs.add_hyperparameter(n_components)
            kernel = CategoricalHyperparameter("SVC__kernel", ["rbf", 'poly'])
            cs.add_hyperparameter(kernel)
            c = UniformFloatHyperparameter("SVC__C", 0.5, 200)
            cs.add_hyperparameter(c)

            # Scenario object
            scenario = Scenario({
                "run_obj": "quality",
                "cs": cs,
                "deterministic": "true",
                "wallclock_limit": self.time_limit,
                "limit_resources": False,
                'abort_on_first_run_crash': False
            })

            # Optimize, using a SMAC directly
            smac = SMAC4HPO(scenario=scenario,
                            rng=42,
                            tae_runner=self.objective_function_simple)
            _ = smac.optimize()

            runhistory_photon = self.smac_helper["data"].solver.runhistory
            runhistory_original = smac.solver.runhistory

            x_ax = range(
                1,
                min(len(runhistory_original._cost_per_config.keys()),
                    len(runhistory_photon._cost_per_config.keys())) + 1)
            y_ax_original = [
                runhistory_original._cost_per_config[tmp] for tmp in x_ax
            ]
            y_ax_photon = [
                runhistory_photon._cost_per_config[tmp] for tmp in x_ax
            ]

            y_ax_original_inc = [min(y_ax_original[:tmp + 1]) for tmp in x_ax]
            y_ax_photon_inc = [min(y_ax_photon[:tmp + 1]) for tmp in x_ax]

            plot = False
            if plot:
                plt.figure(figsize=(10, 7))
                plt.plot(x_ax, y_ax_original, 'g', label='Original')
                plt.plot(x_ax, y_ax_photon, 'b', label='PHOTON')
                plt.plot(x_ax, y_ax_photon_inc, 'r', label='PHOTON Incumbent')
                plt.plot(x_ax,
                         y_ax_original_inc,
                         'k',
                         label='Original Incumbent')
                plt.title('Photon Prove')
                plt.xlabel('X')
                plt.ylabel('Y')
                plt.legend(loc='best')
                plt.savefig("smac.png")

            min_len = min(len(y_ax_original), len(y_ax_photon))
            self.assertLessEqual(
                np.max(
                    np.abs(
                        np.array(y_ax_original[:min_len]) -
                        np.array(y_ax_photon[:min_len]))), 0.01)
예제 #19
0
# DESIGN YOUR PIPELINE
my_pipe = Hyperpipe('custom_estimator_pipe',
                    optimizer='random_grid_search',
                    optimizer_params={'n_configurations': 2},
                    metrics=['accuracy', 'precision', 'recall', 'balanced_accuracy'],
                    best_config_metric='accuracy',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=3),
                    verbosity=1,
                    output_settings=settings)


# SHOW WHAT IS POSSIBLE IN THE CONSOLE
registry.list_available_elements()

# NOW FIND OUT MORE ABOUT A SPECIFIC ELEMENT
registry.info('MyCustomEstimator')
registry.info('MyCustomTransformer')

my_pipe.add(PipelineElement('StandardScaler'))

my_pipe += PipelineElement('PCA', hyperparameters={'n_components': IntegerRange(5, 20)}, test_disabled=True)

my_pipe += PipelineElement('MyCustomEstimator')


# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)


예제 #20
0
                        optimizer_params={'n_configurations': 300, 'acq_func': 'LCB', 'acq_func_kwargs': {'kappa': 1.96}},
                        metrics=['accuracy'],
                        best_config_metric='accuracy',
                        inner_cv=KFold(n_splits=5),
                        cache_folder=cache_skopt_folder_path,
                        verbosity=0,
                        output_settings=settings)

pipeline_elements = []

# scaling
pipeline_elements.append(PipelineElement('StandardScaler',  test_disabled=True))
pipeline_elements.append(PipelineElement('Normalizer', test_disabled=True))

prepro_switch = Switch("PreprocSwitch")
prepro_switch += PipelineElement('PCA', hyperparameters={'n_components': IntegerRange(2, 30)})
prepro_switch += PipelineElement('RandomTreesEmbedding',
                                 hyperparameters={'n_estimators': IntegerRange(1,50),
                                                  'max_depth':IntegerRange(1,6)})
prepro_switch += PipelineElement('SelectPercentile', hyperparameters={'percentile': IntegerRange(5,15)}, test_disabled=True)
#prepro_switch += PipelineElement('FastICA', hyperparameters={'algorithm': Categorical(['parallel', 'deflation'])})


estimator_switch = Switch("EstimatorSwitch")
estimator_switch += PipelineElement('SVC', hyperparameters={'kernel': Categorical(["linear", "rbf", 'poly', 'sigmoid']),
                                                            'C': FloatRange(0.5, 200),
                                                            'decision_function_shape': Categorical(['ovo', 'ovr']),
                                                            'degree': IntegerRange(2,5)})
estimator_switch += PipelineElement("RandomForestClassifier", hyperparameters={'n_estimators': IntegerRange(10, 200),
                                                                               "min_samples_split": IntegerRange(2, 6)})
estimator_switch += PipelineElement("ExtraTreesClassifier", hyperparameters={'n_estimators':IntegerRange(5,500)})