|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +""" |
| 3 | +======= |
| 4 | +Metrics |
| 5 | +======= |
| 6 | +
|
| 7 | +In *Auto-sklearn*, model is optimized over a metric, either built-in or |
| 8 | +custom metric. Moreover, it is also possible to calculate multiple metrics |
| 9 | +per run. The following examples show how to calculate metrics built-in |
| 10 | +and self-defined metrics for a classification problem. |
| 11 | +""" |
| 12 | + |
| 13 | +import autosklearn.classification |
| 14 | +import custom_metrics |
| 15 | +import pandas as pd |
| 16 | +import sklearn.datasets |
| 17 | +import sklearn.metrics |
| 18 | +from autosklearn.metrics import balanced_accuracy, precision, recall, f1 |
| 19 | + |
| 20 | + |
| 21 | +def get_metric_result(cv_results): |
| 22 | + results = pd.DataFrame.from_dict(cv_results) |
| 23 | + results = results[results['status'] == "Success"] |
| 24 | + cols = ['rank_test_scores', 'param_classifier:__choice__', 'mean_test_score'] |
| 25 | + cols.extend([key for key in cv_results.keys() if key.startswith('metric_')]) |
| 26 | + return results[cols] |
| 27 | + |
| 28 | + |
| 29 | +if __name__ == "__main__": |
| 30 | + ############################################################################ |
| 31 | + # Data Loading |
| 32 | + # ============ |
| 33 | + |
| 34 | + X, y = sklearn.datasets.load_breast_cancer(return_X_y=True) |
| 35 | + X_train, X_test, y_train, y_test = \ |
| 36 | + sklearn.model_selection.train_test_split(X, y, random_state=1) |
| 37 | + |
| 38 | + ############################################################################ |
| 39 | + # Build and fit a classifier |
| 40 | + # ========================== |
| 41 | + |
| 42 | + error_rate = autosklearn.metrics.make_scorer( |
| 43 | + name='custom_error', |
| 44 | + score_func=custom_metrics.error, |
| 45 | + optimum=0, |
| 46 | + greater_is_better=False, |
| 47 | + needs_proba=False, |
| 48 | + needs_threshold=False |
| 49 | + ) |
| 50 | + cls = autosklearn.classification.AutoSklearnClassifier( |
| 51 | + time_left_for_this_task=120, |
| 52 | + per_run_time_limit=30, |
| 53 | + scoring_functions=[balanced_accuracy, precision, recall, f1, error_rate] |
| 54 | + ) |
| 55 | + cls.fit(X_train, y_train, X_test, y_test) |
| 56 | + |
| 57 | + ########################################################################### |
| 58 | + # Get the Score of the final ensemble |
| 59 | + # =================================== |
| 60 | + |
| 61 | + predictions = cls.predict(X_test) |
| 62 | + print("Accuracy score", sklearn.metrics.accuracy_score(y_test, predictions)) |
| 63 | + |
| 64 | + print("#" * 80) |
| 65 | + print("Metric results") |
| 66 | + print(get_metric_result(cls.cv_results_).to_string(index=False)) |
0 commit comments