Skip to content

Commit a535d29

Browse files
committed
.
1 parent f5086fa commit a535d29

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

autosklearn/metrics/__init__.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,35 +185,49 @@ def make_scorer(name, score_func, optimum=1, greater_is_better=True,
185185
# Standard regression scores
186186
r2 = make_scorer('r2', sklearn.metrics.r2_score)
187187
mean_squared_error = make_scorer('mean_squared_error',
188-
sklearn.metrics.mean_squared_error, optimum=0,
188+
sklearn.metrics.mean_squared_error,
189+
optimum=0,
189190
greater_is_better=False)
190191
mean_absolute_error = make_scorer('mean_absolute_error',
191192
sklearn.metrics.mean_absolute_error,
192-
optimum=0, greater_is_better=False)
193+
optimum=0,
194+
greater_is_better=False)
193195
median_absolute_error = make_scorer('median_absolute_error',
194196
sklearn.metrics.median_absolute_error,
195-
optimum=0, greater_is_better=False)
197+
optimum=0,
198+
greater_is_better=False)
196199

197200
# Standard Classification Scores
198-
accuracy = make_scorer('accuracy', sklearn.metrics.accuracy_score)
201+
accuracy = make_scorer('accuracy',
202+
sklearn.metrics.accuracy_score)
199203
balanced_accuracy = make_scorer('balanced_accuracy',
200204
classification_metrics.balanced_accuracy)
201-
f1 = make_scorer('f1', sklearn.metrics.f1_score)
205+
f1 = make_scorer('f1',
206+
sklearn.metrics.f1_score)
202207

203208
# Score functions that need decision values
204-
roc_auc = make_scorer('roc_auc', sklearn.metrics.roc_auc_score,
205-
greater_is_better=True, needs_threshold=True)
209+
roc_auc = make_scorer('roc_auc',
210+
sklearn.metrics.roc_auc_score,
211+
greater_is_better=True,
212+
needs_threshold=True)
206213
average_precision = make_scorer('average_precision',
207214
sklearn.metrics.average_precision_score,
208215
needs_threshold=True)
209-
precision = make_scorer('precision', sklearn.metrics.precision_score)
210-
recall = make_scorer('recall', sklearn.metrics.recall_score)
216+
precision = make_scorer('precision',
217+
sklearn.metrics.precision_score)
218+
recall = make_scorer('recall',
219+
sklearn.metrics.recall_score)
211220

212221
# Score function for probabilistic classification
213-
log_loss = make_scorer('log_loss', sklearn.metrics.log_loss, optimum=0,
214-
greater_is_better=False, needs_proba=True)
215-
pac_score = make_scorer('pac_score', classification_metrics.pac_score,
216-
greater_is_better=True, needs_proba=True)
222+
log_loss = make_scorer('log_loss',
223+
sklearn.metrics.log_loss,
224+
optimum=0,
225+
greater_is_better=False,
226+
needs_proba=True)
227+
pac_score = make_scorer('pac_score',
228+
classification_metrics.pac_score,
229+
greater_is_better=True,
230+
needs_proba=True)
217231
# TODO what about mathews correlation coefficient etc?
218232

219233

test/test_metric/test_metrics.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,7 @@ def test_regression_all(self):
317317
for metric, scorer in autosklearn.metrics.REGRESSION_METRICS.items():
318318
y_true = np.array([1, 2, 3, 4])
319319
y_pred = y_true.copy()
320-
321-
# the best possible score of r2 loss is 1.
322-
if metric == 'r2':
323-
previous_score = 1
324-
else:
325-
previous_score = 0
320+
previous_score = scorer._optimum
326321
current_score = scorer(y_true, y_pred)
327322
self.assertAlmostEqual(current_score, previous_score)
328323

@@ -346,18 +341,17 @@ def test_classification_binary(self):
346341
for metric, scorer in autosklearn.metrics.CLASSIFICATION_METRICS.items():
347342
# Skip functions not applicable for binary classification.
348343
# TODO: Average precision should work for binary classification,
349-
# TODO: but its behavior is not right.
344+
# TODO: but its behavior is not right. When y_pred is completely
345+
# TODO: wrong, it does return 0.5, but when it is not completely
346+
# TODO: wrong, it returns value smaller than 0.5.
350347
if metric in ['average_precision', 'pac_score',
351348
'precision_samples', 'recall_samples', 'f1_samples']:
352349
continue
353350

354351
y_true = np.array([1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
355352
y_pred = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [1.0, 0.0],
356353
[1.0, 0.0], [1.0, 0.0]])
357-
if metric is 'log_loss':
358-
previous_score = 0 # the best value for log loss is 0.
359-
else:
360-
previous_score = 1 # the best value for other losses is 1.
354+
previous_score = scorer._optimum
361355
current_score = scorer(y_true, y_pred)
362356
self.assertAlmostEqual(current_score, previous_score)
363357

@@ -390,10 +384,7 @@ def test_classification_multiclass(self):
390384
y_true = np.array([0.0, 0.0, 1.0, 1.0, 2.0])
391385
y_pred = np.array([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0],
392386
[0.0, 1.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
393-
if metric is 'log_loss': # the best possible score for log_loss is 0.
394-
previous_score = 0
395-
else:
396-
previous_score = 1 # the best value for other losses is 1.
387+
previous_score = scorer._optimum
397388
current_score = scorer(y_true, y_pred)
398389
self.assertAlmostEqual(current_score, previous_score)
399390

@@ -424,7 +415,7 @@ def test_classification_multilabel(self):
424415
continue
425416
y_true = np.array([[1, 0, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1]])
426417
y_pred = y_true.copy()
427-
previous_score = 1
418+
previous_score = scorer._optimum
428419
current_score = scorer(y_true, y_pred)
429420
self.assertAlmostEqual(current_score, previous_score)
430421

0 commit comments

Comments
 (0)