Пример #1
0
def evaluate(candidate: CandidateFeature, classifier, grid_search_parameters, preprocessed_folds, score, train_data, current_target, train_X_all, train_y_all_target, test_data, test_target):
    pipeline = Pipeline([('features', FeatureUnion(
        [
            (candidate.get_name(), candidate.pipeline)
        ])),
                         ('classifier', classifier())
                         ])

    refit = False
    if Config.get_default('score.test', 'False') == 'True' and not Config.get_default('instance.selection',
                                                                                      'False') == 'True':
        refit = True

    clf = GridSearchCV(pipeline, grid_search_parameters, cv=preprocessed_folds, scoring=score, iid=False,
                       error_score='raise', refit=refit)
    clf.fit(train_data, current_target) #dataset.splitted_values['train']
    candidate.runtime_properties['score'] = clf.best_score_
    candidate.runtime_properties['hyperparameters'] = clf.best_params_

    if Config.get_default('score.test', 'False') == 'True':
        if Config.get_default('instance.selection', 'False') == 'True':
            clf = GridSearchCV(pipeline, grid_search_parameters, cv=preprocessed_folds, scoring=score,
                               iid=False, error_score='raise', refit=True)

            clf.fit(train_X_all, train_y_all_target)
        candidate.runtime_properties['test_score'] = clf.score(test_data, test_target) #self.dataset.splitted_values['test']
    else:
        candidate.runtime_properties['test_score'] = 0.0

    return candidate
Пример #2
0
def evaluate(candidate: CandidateFeature,
             classifier,
             grid_search_parameters,
             preprocessed_folds,
             score,
             train_data,
             current_target,
             train_X_all,
             train_y_all_target,
             test_data,
             test_target,
             cv_jobs=1):
    pipeline = Pipeline([('features',
                          FeatureUnion([(candidate.get_name(),
                                         candidate.pipeline)])),
                         ('classifier', classifier())])

    refit = False
    if Config.get_default('score.test',
                          'False') == 'True' and not Config.get_default(
                              'instance.selection', 'False') == 'True':
        refit = True

    print(grid_search_parameters)
    clf = GridSearchCV(pipeline,
                       grid_search_parameters,
                       cv=preprocessed_folds,
                       scoring=score,
                       iid=False,
                       error_score='raise',
                       refit=refit,
                       n_jobs=cv_jobs)
    clf.fit(train_data, current_target)  #dataset.splitted_values['train']
    candidate.runtime_properties['score'] = clf.best_score_
    candidate.runtime_properties['hyperparameters'] = clf.best_params_

    #for
    test_fold_predictions = []
    for fold in range(len(preprocessed_folds)):
        test_fold_predictions.append(
            clf.predict(train_data[preprocessed_folds[fold][1]]) ==
            current_target[preprocessed_folds[fold][1]])
    candidate.runtime_properties[
        'test_fold_predictions'] = test_fold_predictions

    if Config.get_default('score.test',
                          'False') == 'True' and len(test_data) > 0:
        if Config.get_default('instance.selection', 'False') == 'True':
            clf = GridSearchCV(pipeline,
                               grid_search_parameters,
                               cv=preprocessed_folds,
                               scoring=score,
                               iid=False,
                               error_score='raise',
                               refit=True)

            clf.fit(train_X_all, train_y_all_target)
        candidate.runtime_properties['test_score'] = clf.score(
            test_data, test_target)  #self.dataset.splitted_values['test']
    else:
        candidate.runtime_properties['test_score'] = 0.0

    return candidate