18
18
from sklearn .base import BaseEstimator
19
19
from sklearn .metrics import (f1_score , r2_score , roc_auc_score , fbeta_score ,
20
20
log_loss , precision_score , recall_score )
21
- from sklearn .metrics . cluster import adjusted_rand_score
21
+ from sklearn .metrics import cluster as cluster_module
22
22
from sklearn .metrics .scorer import (check_scoring , _PredictScorer ,
23
23
_passthrough_scorer )
24
24
from sklearn .metrics import make_scorer , get_scorer , SCORERS
47
47
'roc_auc' , 'average_precision' , 'precision' ,
48
48
'precision_weighted' , 'precision_macro' , 'precision_micro' ,
49
49
'recall' , 'recall_weighted' , 'recall_macro' , 'recall_micro' ,
50
- 'neg_log_loss' , 'log_loss' ,
51
- 'adjusted_rand_score' # not really, but works
52
- ]
50
+ 'neg_log_loss' , 'log_loss' ]
51
+
52
+ # All supervised cluster scorers (They behave like classification metric)
53
+ CLUSTER_SCORERS = ["adjusted_rand_score" ,
54
+ "homogeneity_score" ,
55
+ "completeness_score" ,
56
+ "v_measure_score" ,
57
+ "mutual_info_score" ,
58
+ "adjusted_mutual_info_score" ,
59
+ "normalized_mutual_info_score" ,
60
+ "fowlkes_mallows_score" ]
53
61
54
62
MULTILABEL_ONLY_SCORERS = ['precision_samples' , 'recall_samples' , 'f1_samples' ]
55
63
@@ -65,6 +73,7 @@ def _make_estimators(X_train, y_train, y_ml_train):
65
73
return dict (
66
74
[(name , sensible_regr ) for name in REGRESSION_SCORERS ] +
67
75
[(name , sensible_clf ) for name in CLF_SCORERS ] +
76
+ [(name , sensible_clf ) for name in CLUSTER_SCORERS ] +
68
77
[(name , sensible_ml_clf ) for name in MULTILABEL_ONLY_SCORERS ]
69
78
)
70
79
@@ -330,16 +339,16 @@ def test_thresholded_scorers_multilabel_indicator_data():
330
339
assert_almost_equal (score1 , score2 )
331
340
332
341
333
- def test_unsupervised_scorers ():
342
+ def test_supervised_cluster_scorers ():
334
343
# Test clustering scorers against gold standard labeling.
335
- # We don't have any real unsupervised Scorers yet.
336
344
X , y = make_blobs (random_state = 0 , centers = 2 )
337
345
X_train , X_test , y_train , y_test = train_test_split (X , y , random_state = 0 )
338
346
km = KMeans (n_clusters = 3 )
339
347
km .fit (X_train )
340
- score1 = get_scorer ('adjusted_rand_score' )(km , X_test , y_test )
341
- score2 = adjusted_rand_score (y_test , km .predict (X_test ))
342
- assert_almost_equal (score1 , score2 )
348
+ for name in CLUSTER_SCORERS :
349
+ score1 = get_scorer (name )(km , X_test , y_test )
350
+ score2 = getattr (cluster_module , name )(y_test , km .predict (X_test ))
351
+ assert_almost_equal (score1 , score2 )
343
352
344
353
345
354
@ignore_warnings
@@ -445,4 +454,4 @@ def test_scoring_is_not_metric():
445
454
assert_raises_regexp (ValueError , 'make_scorer' , check_scoring ,
446
455
Ridge (), r2_score )
447
456
assert_raises_regexp (ValueError , 'make_scorer' , check_scoring ,
448
- KMeans (), adjusted_rand_score )
457
+ KMeans (), cluster_module . adjusted_rand_score )
0 commit comments