2727
2828
2929def accuracy (solution , prediction ):
30+ # function defining accuracy
31+ return np .mean (solution == prediction )
32+
33+
34+ def accuracy_wk (solution , prediction , dummy ):
35+ # function defining accuracy and accepting an additional argument
36+ assert dummy is None
3037 return np .mean (solution == prediction )
3138
32- def accuracy_with_kwargs (solution , prediction , )
3339
3440def main ():
3541 # Load adult dataset from openml.org, see https://www.openml.org/t/2117
@@ -52,22 +58,61 @@ def main():
5258 feat_type = ['categorical' if ci else 'numerical'
5359 for ci in categorical_indicator ]
5460
55- # Run auto-sklearn with our metric
56- accuracy_scorer = autosklearn .metrics .make_scorer (name = "accu_self" ,
61+ # Print a list of available metrics
62+ print ("Available CLASSIFICATION metrics autosklearn.metrics.*:" )
63+ print ("\t *" + "\n \t *" .join (autosklearn .metrics .CLASSIFICATION_METRICS ))
64+
65+ print ("Available REGRESSION autosklearn.metrics.*:" )
66+ print ("\t *" + "\n \t *" .join (autosklearn .metrics .REGRESSION_METRICS ))
67+
68+ # First example: Use predefined accuracy metric
69+ print ("#" * 80 )
70+ print ("Use predefined accuracy metric" )
71+ cls = autosklearn .classification .\
72+ AutoSklearnClassifier (time_left_for_this_task = 60 ,
73+ per_run_time_limit = 30 , seed = 1 )
74+ cls .fit (X_train , y_train , feat_type = feat_type ,
75+ metric = autosklearn .metrics .accuracy )
76+
77+ predictions = cls .predict (X_test )
78+ print ("Accuracy score {:g} using {:s}" .
79+ format (sklearn .metrics .accuracy_score (y_test , predictions ),
80+ cls ._automl ._automl ._metric .name ))
81+
82+ print ("#" * 80 )
83+ print ("Use self defined accuracy accuracy metric" )
84+ accuracy_scorer = autosklearn .metrics .make_scorer (name = "accu" ,
5785 score_func = accuracy ,
5886 greater_is_better = True ,
5987 needs_proba = False ,
6088 needs_threshold = False )
6189 cls = autosklearn .classification .\
6290 AutoSklearnClassifier (time_left_for_this_task = 60 ,
63- per_run_time_limit = 30 )
91+ per_run_time_limit = 30 , seed = 1 )
6492 cls .fit (X_train , y_train , feat_type = feat_type , metric = accuracy_scorer )
6593
6694 predictions = cls .predict (X_test )
67- print ("Accuracy score" , sklearn .metrics .accuracy_score (y_test , predictions ))
68-
69-
95+ print ("Accuracy score {:g} using {:s}" .
96+ format (sklearn .metrics .accuracy_score (y_test , predictions ),
97+ cls ._automl ._automl ._metric .name ))
98+
99+ print ("#" * 80 )
100+ print ("Use self defined accuracy with additional argument" )
101+ accuracy_scorer = autosklearn .metrics .make_scorer (name = "accu_add" ,
102+ score_func = accuracy_wk ,
103+ greater_is_better = True ,
104+ needs_proba = False ,
105+ needs_threshold = False ,
106+ dummy = None )
107+ cls = autosklearn .classification .\
108+ AutoSklearnClassifier (time_left_for_this_task = 60 ,
109+ per_run_time_limit = 30 , seed = 1 )
110+ cls .fit (X_train , y_train , feat_type = feat_type , metric = accuracy_scorer )
70111
112+ predictions = cls .predict (X_test )
113+ print ("Accuracy score {:g} using {:s}" .
114+ format (sklearn .metrics .accuracy_score (y_test , predictions ),
115+ cls ._automl ._automl ._metric .name ))
71116
72117
73118if __name__ == "__main__" :
0 commit comments