@@ -594,33 +594,34 @@ def test_param_sampler():
59
341A
4
594
assert_equal ([x for x in sampler ], [x for x in sampler ])
595
595
596
596
597
- def check_cv_results_array_types (results , param_keys , score_keys ):
598
- # Check if the search results' array are of correct types
599
- assert_true (all (isinstance (results [param ], np .ma .MaskedArray )
597
+ def check_cv_results_array_types (cv_results , param_keys , score_keys ):
598
+ # Check if the search `cv_results`'s array are of correct types
599
+ assert_true (all (isinstance (cv_results [param ], np .ma .MaskedArray )
600
600
for param in param_keys ))
601
- assert_true (all (results [key ].dtype == object for key in param_keys ))
602
- assert_false (any (isinstance (results [key ], np .ma .MaskedArray )
601
+ assert_true (all (cv_results [key ].dtype == object for key in param_keys ))
602
+ assert_false (any (isinstance (cv_results [key ], np .ma .MaskedArray )
603
603
for key in score_keys ))
604
- assert_true (all (results [key ].dtype == np .float64
605
- for key in score_keys if key != 'rank_test_score' ))
606
- assert_true (results ['rank_test_score' ].dtype == np .int32 )
604
+ assert_true (all (cv_results [key ].dtype == np .float64
605
+ for key in score_keys if key not in ('rank_test_score' ,
606
+ 'rank_train_score' )))
607
+ assert_true (cv_results ['rank_test_score' ].dtype == np .int32 )
607
608
608
609
609
- def check_cv_results_keys (results , param_keys , score_keys , n_cand ):
610
+ def check_cv_results_keys (cv_results , param_keys , score_keys , n_cand ):
610
611
# Test the search.cv_results_ contains all the required results
611
- assert_array_equal (sorted (results .keys ()),
612
+ assert_array_equal (sorted (cv_results .keys ()),
612
613
sorted (param_keys + score_keys + ('params' ,)))
613
- assert_true (all (results [key ].shape == (n_cand ,)
614
+ assert_true (all (cv_results [key ].shape == (n_cand ,)
614
615
for key in param_keys + score_keys ))
615
616
616
617
617
618
def check_cv_results_grid_scores_consistency (search ):
618
619
# TODO Remove in 0.20
619
- results = search .cv_results_
620
- res_scores = np .vstack (list ([results ["split%d_test_score" % i ]
620
+ cv_results = search .cv_results_
621
+ res_scores = np .vstack (list ([cv_results ["split%d_test_score" % i ]
621
622
for i in range (search .n_splits_ )])).T
622
- res_means = results ["mean_test_score" ]
623
- res_params = results ["params" ]
623
+ res_means = cv_results ["mean_test_score" ]
624
+ res_params = cv_results ["params" ]
624
625
n_cand = len (res_params )
625
626
grid_scores = assert_warns (DeprecationWarning , getattr ,
626
627
search , 'grid_scores_' )
@@ -633,7 +634,7 @@ def check_cv_results_grid_scores_consistency(search):
633
634
assert_array_equal (grid_scores [i ].mean_validation_score , res_means [i ])
634
635
635
636
636
- def test_grid_search_results ():
637
+ def test_grid_search_cv_results ():
637
638
X , y = make_classification (n_samples = 50 , n_features = 4 ,
638
639
random_state = 42 )
639
640
@@ -649,43 +650,46 @@ def test_grid_search_results():
649
650
grid_search_iid .fit (X , y )
650
651
651
652
param_keys = ('param_C' , 'param_degree' , 'param_gamma' , 'param_kernel' )
652
- score_keys = ('mean_test_score' , 'mean_train_score' , 'mean_test_time' ,
653
- 'rank_test_score' , 'split0_test_score' , 'split1_test_score' ,
654
- 'split2_test_score' , 'split0_train_score' ,
655
- 'split1_train_score' , 'split2_train_score' ,
656
- 'std_test_score' , 'std_train_score' , 'std_test_time' )
653
+ score_keys = ('mean_test_score' , 'mean_train_score' ,
654
+ 'rank_test_score' , 'rank_train_score' ,
655
+ 'split0_test_score' , 'split1_test_score' ,
656
+ 'split2_test_score' ,
657
+ 'split0_train_score' , 'split1_train_score' ,
658
+ 'split2_train_score' ,
659
+ 'std_test_score' , 'std_train_score' ,
660
+ 'mean_test_time' , 'std_test_time' )
657
661
n_candidates = n_grid_points
658
662
659
663
for search , iid in zip ((grid_search , grid_search_iid ), (False , True )):
660
664
assert_equal (iid , search .iid )
661
- results = search .cv_results_
665
+ cv_results = search .cv_results_
662
666
# Check if score and timing are reasonable
663
- assert_true (all (results [ 'test_rank_test_score ' ] >= 1 ))
664
- assert_true (all (results [k ] >= 0 ) for k in score_keys
667
+ assert_true (all (cv_results [ 'rank_test_score ' ] >= 1 ))
668
+ assert_true (all (cv_results [k ] >= 0 ) for k in score_keys
665
669
if k is not 'rank_test_score' )
666
- assert_true (all (results [k ] <= 1 ) for k in score_keys
670
+ assert_true (all (cv_results [k ] <= 1 ) for k in score_keys
667
671
if not k .endswith ('time' ) and
668
672
k is not 'rank_test_score' )
669
- # Check results structure
670
- check_cv_results_array_types (results , param_keys , score_keys )
671
- check_cv_results_keys (results , param_keys , score_keys , n_candidates )
673
+ # Check cv_results structure
674
+ check_cv_results_array_types (cv_results , param_keys , score_keys )
675
+ check_cv_results_keys (cv_results , param_keys , score_keys , n_candidates )
672
676
# Check masking
673
- results = grid_search .cv_results_
677
+ cv_results = grid_search .cv_results_
674
678
n_candidates = len (grid_search .cv_results_ ['params' ])
675
- assert_true (all ((results ['param_C' ].mask [i ] and
676
- results ['param_gamma' ].mask [i ] and
677
- not results ['param_degree' ].mask [i ])
679
+ assert_true (all ((cv_results ['param_C' ].mask [i ] and
680
+ cv_results ['param_gamma' ].mask [i ] and
681
+ not cv_results ['param_degree' ].mask [i ])
678
682
for i in range (n_candidates )
679
- if results ['param_kernel' ][i ] == 'linear' ))
680
- assert_true (all ((not results ['param_C' ].mask [i ] and
681
- not results ['param_gamma' ].mask [i ] and
682
- results ['param_degree' ].mask [i ])
683
+ if cv_results ['param_kernel' ][i ] == 'linear' ))
684
+ assert_true (all ((not cv_results ['param_C' ].mask [i ] and
685
+ not cv_results ['param_gamma' ].mask [i ] and
686
+ cv_results ['param_degree' ].mask [i ])
683
687
for i in range (n_candidates )
684
- if results ['param_kernel' ][i ] == 'rbf' ))
688
+ if cv_results ['param_kernel' ][i ] == 'rbf' ))
685
689
check_cv_results_grid_scores_consistency (search )
686
690
687
691
688
- def test_random_search_results ():
692
+ def test_random_search_cv_results ():
689
693
# Make a dataset with a lot of noise to get various kind of prediction
690
694
# errors across CV folds and parameter settings
691
695
X , y = make_classification (n_samples = 200 , n_features = 100 , n_informative = 3 ,
@@ -709,22 +713,25 @@ def test_random_search_results():
709
713
random_search_iid .fit (X , y )
710
714
711
715
param_keys = ('param_C' , 'param_gamma' )
712
- score_keys = ('test_mean_score' , 'train_mean_score' , 'test_mean_time' ,
713
- 'test_rank_score' , 'test_split0_score' , 'test_split1_score' ,
714
- 'test_split2_score' , 'train_split0_score' ,
715
- 'train_split1_score' , 'train_split2_score' ,
716
- 'test_std_score' , 'train_std_score' , 'test_std_time' )
716
+ score_keys = ('mean_test_score' , 'mean_train_score' ,
717
+ 'rank_test_score' , 'rank_train_score' ,
718
+ 'split0_test_score' , 'split1_test_score' ,
719
+ 'split2_test_score' ,
720
+ 'split0_train_score' , 'split1_train_score' ,
721
+ 'split2_train_score' ,
722
+ 'std_test_score' , 'std_train_score' ,
723
+ 'mean_test_time' , 'std_test_time' )
717
724
n_cand = n_search_iter
718
725
719
726
for search , iid in zip ((random_search , random_search_iid ), (False , True )):
720
727
assert_equal (iid , search .iid )
721
- results = search .cv_results_
728
+ cv_results = search .cv_results_
722
729
# Check results structure
723
- check_cv_results_array_types (results , param_keys , score_keys )
724
- check_cv_results_keys (results , param_keys , score_keys , n_cand )
730
+ check_cv_results_array_types (cv_results , param_keys , score_keys )
731
+ check_cv_results_keys (cv_results , param_keys , score_keys , n_cand )
725
732
# For random_search, all the param array vals should be unmasked
726
- assert_false (any (results ['param_C' ].mask ) or
727
- any (results ['param_gamma' ].mask ))
733
+ assert_false (any (cv_results ['param_C' ].mask ) or
734
+ any (cv_results ['param_gamma' ].mask ))
728
735
check_cv_results_grid_scores_consistency (search )
729
736
730
737
@@ -806,15 +813,15 @@ def test_search_cv_results_rank_tie_breaking():
806
813
807
814
for search in (grid_search , random_search ):
808
815
search .fit (X , y )
809
- results = search .cv_results_
816
+ cv_results = search .cv_results_
810
817
# Check tie breaking strategy -
811
818
# Check that there is a tie in the mean scores between
812
819
# candidates 1 and 2 alone
813
- assert_almost_equal (results ['mean_test_score' ][0 ],
814
- results ['mean_test_score' ][1 ])
820
+ assert_almost_equal (cv_results ['mean_test_score' ][0 ],
821
+ cv_results ['mean_test_score' ][1 ])
815
822
try :
816
- assert_almost_equal (results ['mean_test_score' ][1 ],
817
- results ['mean_test_score' ][2 ])
823
+ assert_almost_equal (cv_results ['mean_test_score' ][1 ],
824
+ cv_results ['mean_test_score' ][2 ])
818
825
except AssertionError :
819
826
pass
820
827
# 'min' rank should be assigned to the tied candidates
@@ -841,10 +848,10 @@ def test_grid_search_correct_score_results():
841
848
Cs = [.1 , 1 , 10 ]
842
849
for score in ['f1' , 'roc_auc' ]:
843
850
grid_search = GridSearchCV (clf , {'C' : Cs }, scoring = score , cv = n_splits )
844
- results = grid_search .fit (X , y ).cv_results_
851
+ cv_results = grid_search .fit (X , y ).cv_results_
845
852
846
853
# Test scorer names
847
- result_keys = list (results .keys ())
854
+ result_keys = list (cv_results .keys ())
848
855
expected_keys = (("mean_test_score" , "rank_test_score" ) +
849
856
tuple ("split%d_test_score" % cv_i
850
857
for cv_i in range (n_splits )))
0 commit comments