Esempio n. 1
0
def _calculate_meta_features(data_feat_type, data_info_task, basename,
                            metalearning_cnt, x_train, y_train, watcher,
                            log_function):
    # == Calculate metafeatures
    task_name = 'CalculateMetafeatures'
    watcher.start_task(task_name)
    categorical = [True if feat_type.lower() in ['categorical'] else False
                   for feat_type in data_feat_type]

    if metalearning_cnt > 0 and data_info_task in \
            [MULTICLASS_CLASSIFICATION, BINARY_CLASSIFICATION]:

        log_function('Start calculating metafeatures for %s' % basename)
        result = calc_meta_features(x_train,
                                    y_train,
                                    categorical=categorical,
                                    dataset_name=basename)
    else:
        result = None
        log_function('Metafeatures not calculated')
    watcher.stop_task(task_name)
    log_function(
        'Calculating Metafeatures (categorical attributes) took %5.2f' %
        watcher.wall_elapsed(task_name))
    return result
    def test_metalearning(self):
        dataset_name = 'digits'

        initial_challengers = {
            'acc_metric': ["--initial-challengers \" "
                           "-adaboost:algorithm 'SAMME.R' "
                           "-adaboost:learning_rate '0.400363929326' "
                           "-adaboost:max_depth '5' "
                           "-adaboost:n_estimators '319' "
                           "-balancing:strategy 'none' "
                           "-classifier 'adaboost' "
                           "-imputation:strategy 'most_frequent' "
                           "-preprocessor 'no_preprocessing' "
                           "-rescaling:strategy 'min/max'\""],
            'auc_metric': ["--initial-challengers \" "
                           "-adaboost:algorithm 'SAMME.R' "
                           "-adaboost:learning_rate '0.966883114819' "
                           "-adaboost:max_depth '5' "
                           "-adaboost:n_estimators '412' "
                           "-balancing:strategy 'weighting' "
                           "-classifier 'adaboost' "
                           "-imputation:strategy 'median' "
                           "-preprocessor 'no_preprocessing' "
                           "-rescaling:strategy 'min/max'\""],
            'bac_metric': ["--initial-challengers \" "
                           "-adaboost:algorithm 'SAMME.R' "
                           "-adaboost:learning_rate '0.400363929326' "
                           "-adaboost:max_depth '5' "
                           "-adaboost:n_estimators '319' "
                           "-balancing:strategy 'none' "
                           "-classifier 'adaboost' "
                           "-imputation:strategy 'most_frequent' "
                           "-preprocessor 'no_preprocessing' "
                           "-rescaling:strategy 'min/max'\""],
            'f1_metric': ["--initial-challengers \" "
                          "-adaboost:algorithm 'SAMME.R' "
                          "-adaboost:learning_rate '0.966883114819' "
                          "-adaboost:max_depth '5' "
                          "-adaboost:n_estimators '412' "
                          "-balancing:strategy 'weighting' "
                          "-classifier 'adaboost' "
                          "-imputation:strategy 'median' "
                          "-preprocessor 'no_preprocessing' "
                          "-rescaling:strategy 'min/max'\""],
            'pac_metric': ["--initial-challengers \" "
                           "-adaboost:algorithm 'SAMME.R' "
                           "-adaboost:learning_rate '0.400363929326' "
                           "-adaboost:max_depth '5' "
                           "-adaboost:n_estimators '319' "
                           "-balancing:strategy 'none' "
                           "-classifier 'adaboost' "
                           "-imputation:strategy 'most_frequent' "
                           "-preprocessor 'no_preprocessing' "
                           "-rescaling:strategy 'min/max'\""]
        }

        for metric in initial_challengers:
            configuration_space = get_configuration_space(
                {
                    'metric': metric,
                    'task': MULTICLASS_CLASSIFICATION,
                    'is_sparse': False
                },
                include_preprocessors=['no_preprocessing'])

            X_train, Y_train, X_test, Y_test = get_dataset(dataset_name)
            categorical = [False] * X_train.shape[1]

            meta_features_label = calc_meta_features(X_train, Y_train,
                                                     categorical, dataset_name)
            meta_features_encoded_label = calc_meta_features_encoded(X_train,
                                                                     Y_train,
                                                                     categorical,
                                                                     dataset_name)
            initial_configuration_strings_for_smac = \
                create_metalearning_string_for_smac_call(
                    meta_features_label,
                    meta_features_encoded_label,
                    configuration_space, dataset_name, metric,
                    MULTICLASS_CLASSIFICATION, False, 1, None)

            print(metric)
            self.assertEqual(initial_challengers[metric],
                             initial_configuration_strings_for_smac)