<s> """<STR_LIT>""" <EOL> from collections import Iterable , Sized <EOL> from sklearn . externals . six . moves import cStringIO as StringIO <EOL> from sklearn . externals . six . moves import xrange <EOL> from itertools import chain , product <EOL> import pickle <EOL> import sys <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> from sklearn . utils . fixes import sp_version <EOL> from sklearn . utils . testing import assert_equal <EOL> from sklearn . utils . testing import assert_not_equal <EOL> from sklearn . utils . testing import assert_raises <EOL> from sklearn . utils . testing import assert_warns <EOL> from sklearn . utils . testing import assert_raise_message <EOL> from sklearn . utils . testing import assert_false , assert_true <EOL> from sklearn . utils . testing import assert_array_equal <EOL> from sklearn . utils . testing import assert_almost_equal <EOL> from sklearn . utils . testing import assert_array_almost_equal <EOL> from sklearn . utils . testing import assert_no_warnings <EOL> from sklearn . utils . testing import ignore_warnings <EOL> from sklearn . utils . mocking import CheckingClassifier , MockDataFrame <EOL> from scipy . stats import bernoulli , expon , uniform <EOL> from sklearn . externals . six . moves import zip <EOL> from sklearn . base import BaseEstimator <EOL> from sklearn . datasets import make_classification <EOL> from sklearn . datasets import make_blobs <EOL> from sklearn . datasets import make_multilabel_classification <EOL> from sklearn . model_selection import KFold <EOL> from sklearn . model_selection import StratifiedKFold <EOL> from sklearn . model_selection import StratifiedShuffleSplit <EOL> from sklearn . model_selection import LeaveOneLabelOut <EOL> from sklearn . model_selection import LeavePLabelOut <EOL> from sklearn . model_selection import LabelKFold <EOL> from sklearn . model_selection import LabelShuffleSplit <EOL> from sklearn . model_selection import GridSearchCV <EOL> from sklearn . model_selection import RandomizedSearchCV <EOL> from sklearn . model_selection import ParameterGrid <EOL> from sklearn . model_selection import ParameterSampler <EOL> from sklearn . base import ChangedBehaviorWarning <EOL> from sklearn . model_selection . _validation import FitFailedWarning <EOL> from sklearn . svm import LinearSVC , SVC <EOL> from sklearn . tree import DecisionTreeRegressor <EOL> from sklearn . tree import DecisionTreeClassifier <EOL> from sklearn . cluster import KMeans <EOL> from sklearn . neighbors import KernelDensity <EOL> from sklearn . metrics import f1_score <EOL> from sklearn . metrics import make_scorer <EOL> from sklearn . metrics import roc_auc_score <EOL> from sklearn . preprocessing import Imputer <EOL> from sklearn . pipeline import Pipeline <EOL> class MockClassifier ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , foo_param = <NUM_LIT:0> ) : <EOL> self . foo_param = foo_param <EOL> def fit ( self , X , Y ) : <EOL> assert_true ( len ( X ) == len ( Y ) ) <EOL> return self <EOL> def predict ( self , T ) : <EOL> return T . shape [ <NUM_LIT:0> ] <EOL> predict_proba = predict <EOL> decision_function = predict <EOL> transform = predict <EOL> def score ( self , X = None , Y = None ) : <EOL> if self . foo_param > <NUM_LIT:1> : <EOL> score = <NUM_LIT:1.> <EOL> else : <EOL> score = <NUM_LIT:0.> <EOL> return score <EOL> def get_params ( self , deep = False ) : <EOL> return { '<STR_LIT>' : self . foo_param } <EOL> def set_params ( self , ** params ) : <EOL> self . foo_param = params [ '<STR_LIT>' ] <EOL> return self <EOL> class LinearSVCNoScore ( LinearSVC ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def score ( self ) : <EOL> raise AttributeError <EOL> X = np . array ( [ [ - <NUM_LIT:1> , - <NUM_LIT:1> ] , [ - <NUM_LIT:2> , - <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:1> ] ] ) <EOL> y = np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> def assert_grid_iter_equals_getitem ( grid ) : <EOL> assert_equal ( list ( grid ) , [ grid [ i ] for i in range ( len ( grid ) ) ] ) <EOL> def test_parameter_grid ( ) : <EOL> params1 = { "<STR_LIT:foo>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> grid1 = ParameterGrid ( params1 ) <EOL> assert_true ( isinstance ( grid1 , Iterable ) ) <EOL> assert_true ( isinstance ( grid1 , Sized ) ) <EOL> assert_equal ( len ( grid1 ) , <NUM_LIT:3> ) <EOL> assert_grid_iter_equals_getitem ( grid1 ) <EOL> params2 = { "<STR_LIT:foo>" : [ <NUM_LIT:4> , <NUM_LIT:2> ] , <EOL> "<STR_LIT:bar>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> grid2 = ParameterGrid ( params2 ) <EOL> assert_equal ( len ( grid2 ) , <NUM_LIT:6> ) <EOL> for i in xrange ( <NUM_LIT:2> ) : <EOL> points = set ( tuple ( chain ( * ( sorted ( p . items ( ) ) ) ) ) for p in grid2 ) <EOL> assert_equal ( points , <EOL> set ( ( "<STR_LIT:bar>" , x , "<STR_LIT:foo>" , y ) <EOL> for x , y in product ( params2 [ "<STR_LIT:bar>" ] , params2 [ "<STR_LIT:foo>" ] ) ) ) <EOL> assert_grid_iter_equals_getitem ( grid2 ) <EOL> empty = ParameterGrid ( { } ) <EOL> assert_equal ( len ( empty ) , <NUM_LIT:1> ) <EOL> assert_equal ( list ( empty ) , [ { } ] ) <EOL> assert_grid_iter_equals_getitem ( empty ) <EOL> assert_raises ( IndexError , lambda : empty [ <NUM_LIT:1> ] ) <EOL> has_empty = ParameterGrid ( [ { '<STR_LIT:C>' : [ <NUM_LIT:1> , <NUM_LIT:10> ] } , { } , { '<STR_LIT:C>' : [ <NUM_LIT> ] } ] ) <EOL> assert_equal ( len ( has_empty ) , <NUM_LIT:4> ) <EOL> assert_equal ( list ( has_empty ) , [ { '<STR_LIT:C>' : <NUM_LIT:1> } , { '<STR_LIT:C>' : <NUM_LIT:10> } , { } , { '<STR_LIT:C>' : <NUM_LIT> } ] ) <EOL> assert_grid_iter_equals_getitem ( has_empty ) <EOL> def test_grid_search ( ) : <EOL> clf = MockClassifier ( ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , verbose = <NUM_LIT:3> ) <EOL> old_stdout = sys . stdout <EOL> sys . stdout = StringIO ( ) <EOL> grid_search . fit ( X , y ) <EOL> sys . stdout = old_stdout <EOL> assert_equal ( grid_search . best_estimator_ . foo_param , <NUM_LIT:2> ) <EOL> for i , foo_i in enumerate ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) : <EOL> assert_true ( grid_search . grid_scores_ [ i ] [ <NUM_LIT:0> ] <EOL> == { '<STR_LIT>' : foo_i } ) <EOL> grid_search . score ( X , y ) <EOL> grid_search . predict_proba ( X ) <EOL> grid_search . decision_function ( X ) <EOL> grid_search . transform ( X ) <EOL> grid_search . scoring = '<STR_LIT>' <EOL> assert_raises ( ValueError , grid_search . fit , X , y ) <EOL> @ ignore_warnings <EOL> def test_grid_search_no_score ( ) : <EOL> clf = LinearSVC ( random_state = <NUM_LIT:0> ) <EOL> X , y = make_blobs ( random_state = <NUM_LIT:0> , centers = <NUM_LIT:2> ) <EOL> Cs = [ <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:10> ] <EOL> clf_no_score = LinearSVCNoScore ( random_state = <NUM_LIT:0> ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT:C>' : Cs } , scoring = '<STR_LIT>' ) <EOL> grid_search . fit ( X , y ) <EOL> grid_search_no_score = GridSearchCV ( clf_no_score , { '<STR_LIT:C>' : Cs } , <EOL> scoring = '<STR_LIT>' ) <EOL> grid_search_no_score . fit ( X , y ) <EOL> assert_equal ( grid_search_no_score . best_params_ , grid_search . best_params_ ) <EOL> assert_equal ( grid_search . score ( X , y ) , grid_search_no_score . score ( X , y ) ) <EOL> grid_search_no_score = GridSearchCV ( clf_no_score , { '<STR_LIT:C>' : Cs } ) <EOL> assert_raise_message ( TypeError , "<STR_LIT>" , grid_search_no_score . fit , <EOL> [ [ <NUM_LIT:1> ] ] ) <EOL> def test_grid_search_score_method ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:100> , n_classes = <NUM_LIT:2> , flip_y = <NUM_LIT> , <EOL> random_state = <NUM_LIT:0> ) <EOL> clf = LinearSVC ( random_state = <NUM_LIT:0> ) <EOL> grid = { '<STR_LIT:C>' : [ <NUM_LIT> ] } <EOL> search_no_scoring = GridSearchCV ( clf , grid , scoring = None ) . fit ( X , y ) <EOL> search_accuracy = GridSearchCV ( clf , grid , scoring = '<STR_LIT>' ) . fit ( X , y ) <EOL> search_no_score_method_auc = GridSearchCV ( LinearSVCNoScore ( ) , grid , <EOL> scoring = '<STR_LIT>' ) . fit ( X , y ) <EOL> search_auc = GridSearchCV ( clf , grid , scoring = '<STR_LIT>' ) . fit ( X , y ) <EOL> score_no_scoring = assert_no_warnings ( search_no_scoring . score , X , y ) <EOL> score_accuracy = assert_warns ( ChangedBehaviorWarning , <EOL> search_accuracy . score , X , y ) <EOL> score_no_score_auc = assert_no_warnings ( search_no_score_method_auc . score , <EOL> X , y ) <EOL> score_auc = assert_warns ( ChangedBehaviorWarning , <EOL> search_auc . score , X , y ) <EOL> assert_true ( score_auc < <NUM_LIT:1.0> ) <EOL> assert_true ( score_accuracy < <NUM_LIT:1.0> ) <EOL> assert_not_equal ( score_auc , score_accuracy ) <EOL> assert_almost_equal ( score_accuracy , score_no_scoring ) <EOL> assert_almost_equal ( score_auc , score_no_score_auc ) <EOL> def test_grid_search_labels ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X , y = make_classification ( n_samples = <NUM_LIT:15> , n_classes = <NUM_LIT:2> , random_state = <NUM_LIT:0> ) <EOL> labels = rng . randint ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:15> ) <EOL> clf = LinearSVC ( random_state = <NUM_LIT:0> ) <EOL> grid = { '<STR_LIT:C>' : [ <NUM_LIT:1> ] } <EOL> label_cvs = [ LeaveOneLabelOut ( ) , LeavePLabelOut ( <NUM_LIT:2> ) , LabelKFold ( ) , <EOL> LabelShuffleSplit ( ) ] <EOL> for cv in label_cvs : <EOL> gs = GridSearchCV ( clf , grid , cv = cv ) <EOL> assert_raise_message ( ValueError , <EOL> "<STR_LIT>" , <EOL> gs . fit , X , y ) <EOL> gs . fit ( X , y , labels ) <EOL> non_label_cvs = [ StratifiedKFold ( ) , StratifiedShuffleSplit ( ) ] <EOL> for cv in non_label_cvs : <EOL> gs = GridSearchCV ( clf , grid , cv = cv ) <EOL> gs . fit ( X , y ) <EOL> def test_trivial_grid_scores ( ) : <EOL> clf = MockClassifier ( ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> ] } ) <EOL> grid_search . fit ( X , y ) <EOL> assert_true ( hasattr ( grid_search , "<STR_LIT>" ) ) <EOL> random_search = RandomizedSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:0> ] } , n_iter = <NUM_LIT:1> ) <EOL> random_search . fit ( X , y ) <EOL> assert_true ( hasattr ( random_search , "<STR_LIT>" ) ) <EOL> def test_no_refit ( ) : <EOL> clf = MockClassifier ( ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , refit = False ) <EOL> grid_search . fit ( X , y ) <EOL> assert_true ( hasattr ( grid_search , "<STR_LIT>" ) ) <EOL> def test_grid_search_error ( ) : <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> clf = LinearSVC ( ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } ) <EOL> assert_raises ( ValueError , cv . fit , X_ [ : <NUM_LIT> ] , y_ ) <EOL> def test_grid_search_iid ( ) : <EOL> X , y = make_blobs ( centers = [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] , random_state = <NUM_LIT:0> , <EOL> cluster_std = <NUM_LIT:0.1> , shuffle = False , n_samples = <NUM_LIT> ) <EOL> mask = np . ones ( X . shape [ <NUM_LIT:0> ] , dtype = np . bool ) <EOL> mask [ np . where ( y == <NUM_LIT:1> ) [ <NUM_LIT:0> ] [ : : <NUM_LIT:2> ] ] = <NUM_LIT:0> <EOL> mask [ np . where ( y == <NUM_LIT:2> ) [ <NUM_LIT:0> ] [ : : <NUM_LIT:2> ] ] = <NUM_LIT:0> <EOL> svm = SVC ( kernel = '<STR_LIT>' ) <EOL> cv = [ [ mask , ~ mask ] , [ ~ mask , mask ] ] <EOL> grid_search = GridSearchCV ( svm , param_grid = { '<STR_LIT:C>' : [ <NUM_LIT:1> , <NUM_LIT:10> ] } , cv = cv ) <EOL> grid_search . fit ( X , y ) <EOL> first = grid_search . grid_scores_ [ <NUM_LIT:0> ] <EOL> assert_equal ( first . parameters [ '<STR_LIT:C>' ] , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( first . cv_validation_scores , [ <NUM_LIT:1> , <NUM_LIT:1.> / <NUM_LIT> ] ) <EOL> assert_almost_equal ( first . mean_validation_score , <EOL> <NUM_LIT:1> * <NUM_LIT:1.> / <NUM_LIT> + <NUM_LIT:1.> / <NUM_LIT> * <NUM_LIT> / <NUM_LIT> ) <EOL> grid_search = GridSearchCV ( svm , param_grid = { '<STR_LIT:C>' : [ <NUM_LIT:1> , <NUM_LIT:10> ] } , cv = cv , <EOL> iid = False ) <EOL> grid_search . fit ( X , y ) <EOL> first = grid_search . grid_scores_ [ <NUM_LIT:0> ] <EOL> assert_equal ( first . parameters [ '<STR_LIT:C>' ] , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( first . cv_validation_scores , [ <NUM_LIT:1> , <NUM_LIT:1.> / <NUM_LIT> ] ) <EOL> assert_almost_equal ( first . mean_validation_score , <EOL> np . mean ( first . cv_validation_scores ) ) <EOL> def test_grid_search_one_grid_point ( ) : <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> param_dict = { "<STR_LIT:C>" : [ <NUM_LIT:1.0> ] , "<STR_LIT>" : [ "<STR_LIT>" ] , "<STR_LIT>" : [ <NUM_LIT:0.1> ] } <EOL> clf = SVC ( ) <EOL> cv = GridSearchCV ( clf , param_dict ) <EOL> cv . fit ( X_ , y_ ) <EOL> clf = SVC ( C = <NUM_LIT:1.0> , kernel = "<STR_LIT>" , gamma = <NUM_LIT:0.1> ) <EOL> clf . fit ( X_ , y_ ) <EOL> assert_array_equal ( clf . dual_coef_ , cv . best_estimator_ . dual_coef_ ) <EOL> def test_grid_search_bad_param_grid ( ) : <EOL> param_dict = { "<STR_LIT:C>" : <NUM_LIT:1.0> } <EOL> clf = SVC ( ) <EOL> assert_raises ( ValueError , GridSearchCV , clf , param_dict ) <EOL> param_dict = { "<STR_LIT:C>" : [ ] } <EOL> clf = SVC ( ) <EOL> assert_raises ( ValueError , GridSearchCV , clf , param_dict ) <EOL> param_dict = { "<STR_LIT:C>" : np . ones ( <NUM_LIT:6> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> ) } <EOL> clf = SVC ( ) <EOL> assert_raises ( ValueError , GridSearchCV , clf , param_dict ) <EOL> def test_grid_search_sparse ( ) : <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> clf = LinearSVC ( ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } ) <EOL> cv . fit ( X_ [ : <NUM_LIT> ] , y_ [ : <NUM_LIT> ] ) <EOL> y_pred = cv . predict ( X_ [ <NUM_LIT> : ] ) <EOL> C = cv . best_estimator_ . C <EOL> X_ = sp . csr_matrix ( X_ ) <EOL> clf = LinearSVC ( ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } ) <EOL> cv . fit ( X_ [ : <NUM_LIT> ] . tocoo ( ) , y_ [ : <NUM_LIT> ] ) <EOL> y_pred2 = cv . predict ( X_ [ <NUM_LIT> : ] ) <EOL> C2 = cv . best_estimator_ . C <EOL> assert_true ( np . mean ( y_pred == y_pred2 ) >= <NUM_LIT> ) <EOL> assert_equal ( C , C2 ) <EOL> def test_grid_search_sparse_scoring ( ) : <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> clf = LinearSVC ( ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } , scoring = "<STR_LIT>" ) <EOL> cv . fit ( X_ [ : <NUM_LIT> ] , y_ [ : <NUM_LIT> ] ) <EOL> y_pred = cv . predict ( X_ [ <NUM_LIT> : ] ) <EOL> C = cv . best_estimator_ . C <EOL> X_ = sp . csr_matrix ( X_ ) <EOL> clf = LinearSVC ( ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } , scoring = "<STR_LIT>" ) <EOL> cv . fit ( X_ [ : <NUM_LIT> ] , y_ [ : <NUM_LIT> ] ) <EOL> y_pred2 = cv . predict ( X_ [ <NUM_LIT> : ] ) <EOL> C2 = cv . best_estimator_ . C <EOL> assert_array_equal ( y_pred , y_pred2 ) <EOL> assert_equal ( C , C2 ) <EOL> def f1_loss ( y_true_ , y_pred_ ) : <EOL> return - f1_score ( y_true_ , y_pred_ ) <EOL> F1Loss = make_scorer ( f1_loss , greater_is_better = False ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } , scoring = F1Loss ) <EOL> cv . fit ( X_ [ : <NUM_LIT> ] , y_ [ : <NUM_LIT> ] ) <EOL> y_pred3 = cv . predict ( X_ [ <NUM_LIT> : ] ) <EOL> C3 = cv . best_estimator_ . C <EOL> assert_equal ( C , C3 ) <EOL> assert_array_equal ( y_pred , y_pred3 ) <EOL> def test_grid_search_precomputed_kernel ( ) : <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> K_train = np . dot ( X_ [ : <NUM_LIT> ] , X_ [ : <NUM_LIT> ] . T ) <EOL> y_train = y_ [ : <NUM_LIT> ] <EOL> clf = SVC ( kernel = '<STR_LIT>' ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } ) <EOL> cv . fit ( K_train , y_train ) <EOL> assert_true ( cv . best_score_ >= <NUM_LIT:0> ) <EOL> K_test = np . dot ( X_ [ <NUM_LIT> : ] , X_ [ : <NUM_LIT> ] . T ) <EOL> y_test = y_ [ <NUM_LIT> : ] <EOL> y_pred = cv . predict ( K_test ) <EOL> assert_true ( np . mean ( y_pred == y_test ) >= <NUM_LIT:0> ) <EOL> assert_raises ( ValueError , cv . fit , K_train . tolist ( ) , y_train ) <EOL> def test_grid_search_precomputed_kernel_error_nonsquare ( ) : <EOL> K_train = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:20> ) ) <EOL> y_train = np . ones ( ( <NUM_LIT:10> , ) ) <EOL> clf = SVC ( kernel = '<STR_LIT>' ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } ) <EOL> assert_raises ( ValueError , cv . fit , K_train , y_train ) <EOL> def test_grid_search_precomputed_kernel_error_kernel_function ( ) : <EOL> X_ , y_ = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> kernel_function = lambda x1 , x2 : np . dot ( x1 , x2 . T ) <EOL> clf = SVC ( kernel = kernel_function ) <EOL> cv = GridSearchCV ( clf , { '<STR_LIT:C>' : [ <NUM_LIT:0.1> , <NUM_LIT:1.0> ] } ) <EOL> assert_raises ( ValueError , cv . fit , X_ , y_ ) <EOL> class BrokenClassifier ( BaseEstimator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parameter = None ) : <EOL> self . parameter = parameter <EOL> def fit ( self , X , y ) : <EOL> assert_true ( not hasattr ( self , '<STR_LIT>' ) ) <EOL> self . has_been_fit_ = True <EOL> def predict ( self , X ) : <EOL> return np . zeros ( X . shape [ <NUM_LIT:0> ] ) <EOL> @ ignore_warnings <EOL> def test_refit ( ) : <EOL> X = np . arange ( <NUM_LIT:100> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> y = np . array ( [ <NUM_LIT:0> ] * <NUM_LIT:5> + [ <NUM_LIT:1> ] * <NUM_LIT:5> ) <EOL> clf = GridSearchCV ( BrokenClassifier ( ) , [ { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> ] } ] , <EOL> scoring = "<STR_LIT>" , refit = True ) <EOL> clf . fit ( X , y ) <EOL> def test_gridsearch_nd ( ) : <EOL> X_4d = np . arange ( <NUM_LIT:10> * <NUM_LIT:5> * <NUM_LIT:3> * <NUM_LIT:2> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> y_3d = np . arange ( <NUM_LIT:10> * <NUM_LIT:7> * <NUM_LIT:11> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:7> , <NUM_LIT:11> ) <EOL> check_X = lambda x : x . shape [ <NUM_LIT:1> : ] == ( <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> check_y = lambda x : x . shape [ <NUM_LIT:1> : ] == ( <NUM_LIT:7> , <NUM_LIT:11> ) <EOL> clf = CheckingClassifier ( check_X = check_X , check_y = check_y ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } ) <EOL> grid_search . fit ( X_4d , y_3d ) . score ( X , y ) <EOL> assert_true ( hasattr ( grid_search , "<STR_LIT>" ) ) <EOL> def test_X_as_list ( ) : <EOL> X = np . arange ( <NUM_LIT:100> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> y = np . array ( [ <NUM_LIT:0> ] * <NUM_LIT:5> + [ <NUM_LIT:1> ] * <NUM_LIT:5> ) <EOL> clf = CheckingClassifier ( check_X = lambda x : isinstance ( x , list ) ) <EOL> cv = KFold ( n_folds = <NUM_LIT:3> ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , cv = cv ) <EOL> grid_search . fit ( X . tolist ( ) , y ) . score ( X , y ) <EOL> assert_true ( hasattr ( grid_search , "<STR_LIT>" ) ) <EOL> def test_y_as_list ( ) : <EOL> X = np . arange ( <NUM_LIT:100> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> y = np . array ( [ <NUM_LIT:0> ] * <NUM_LIT:5> + [ <NUM_LIT:1> ] * <NUM_LIT:5> ) <EOL> clf = CheckingClassifier ( check_y = lambda x : isinstance ( x , list ) ) <EOL> cv = KFold ( n_folds = <NUM_LIT:3> ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , cv = cv ) <EOL> grid_search . fit ( X , y . tolist ( ) ) . score ( X , y ) <EOL> assert_true ( hasattr ( grid_search , "<STR_LIT>" ) ) <EOL> @ ignore_warnings <EOL> def test_pandas_input ( ) : <EOL> types = [ ( MockDataFrame , MockDataFrame ) ] <EOL> try : <EOL> from pandas import Series , DataFrame <EOL> types . append ( ( DataFrame , Series ) ) <EOL> except ImportError : <EOL> pass <EOL> X = np . arange ( <NUM_LIT:100> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> y = np . array ( [ <NUM_LIT:0> ] * <NUM_LIT:5> + [ <NUM_LIT:1> ] * <NUM_LIT:5> ) <EOL> for InputFeatureType , TargetType in types : <EOL> X_df , y_ser = InputFeatureType ( X ) , TargetType ( y ) <EOL> check_df = lambda x : isinstance ( x , InputFeatureType ) <EOL> check_series = lambda x : isinstance ( x , TargetType ) <EOL> clf = CheckingClassifier ( check_X = check_df , check_y = check_series ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } ) <EOL> grid_search . fit ( X_df , y_ser ) . score ( X_df , y_ser ) <EOL> grid_search . predict ( X_df ) <EOL> assert_true ( hasattr ( grid_search , "<STR_LIT>" ) ) <EOL> def test_unsupervised_grid_search ( ) : <EOL> X , y = make_blobs ( random_state = <NUM_LIT:0> ) <EOL> km = KMeans ( random_state = <NUM_LIT:0> ) <EOL> grid_search = GridSearchCV ( km , param_grid = dict ( n_clusters = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) , <EOL> scoring = '<STR_LIT>' ) <EOL> grid_search . fit ( X , y ) <EOL> assert_equal ( grid_search . best_params_ [ "<STR_LIT>" ] , <NUM_LIT:3> ) <EOL> grid_search = GridSearchCV ( km , param_grid = dict ( n_clusters = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) ) <EOL> grid_search . fit ( X ) <EOL> assert_equal ( grid_search . best_params_ [ "<STR_LIT>" ] , <NUM_LIT:4> ) <EOL> def test_gridsearch_no_predict ( ) : <EOL> def custom_scoring ( estimator , X ) : <EOL> return <NUM_LIT> if estimator . bandwidth == <NUM_LIT> else <NUM_LIT:0> <EOL> X , _ = make_blobs ( cluster_std = <NUM_LIT> , random_state = <NUM_LIT:1> , <EOL> centers = [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> search = GridSearchCV ( KernelDensity ( ) , <EOL> param_grid = dict ( bandwidth = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) , <EOL> scoring = custom_scoring ) <EOL> search . fit ( X ) <EOL> assert_equal ( search . best_params_ [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> assert_equal ( search . best_score_ , <NUM_LIT> ) <EOL> def test_param_sampler ( ) : <EOL> param_distributions = { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT:C>" : uniform ( <NUM_LIT:0> , <NUM_LIT:1> ) } <EOL> sampler = ParameterSampler ( param_distributions = param_distributions , <EOL> n_iter = <NUM_LIT:10> , random_state = <NUM_LIT:0> ) <EOL> samples = [ x for x in sampler ] <EOL> assert_equal ( len ( samples ) , <NUM_LIT:10> ) <EOL> for sample in samples : <EOL> assert_true ( sample [ "<STR_LIT>" ] in [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert_true ( <NUM_LIT:0> <= sample [ "<STR_LIT:C>" ] <= <NUM_LIT:1> ) <EOL> param_distributions = { "<STR_LIT:C>" : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:10> ] } <EOL> sampler = ParameterSampler ( param_distributions = param_distributions , <EOL> n_iter = <NUM_LIT:3> , random_state = <NUM_LIT:0> ) <EOL> assert_equal ( [ x for x in sampler ] , [ x for x in sampler ] ) <EOL> if sp_version >= ( <NUM_LIT:0> , <NUM_LIT:16> ) : <EOL> param_distributions = { "<STR_LIT:C>" : uniform ( <NUM_LIT:0> , <NUM_LIT:1> ) } <EOL> sampler = ParameterSampler ( param_distributions = param_distributions , <EOL> n_iter = <NUM_LIT:10> , random_state = <NUM_LIT:0> ) <EOL> assert_equal ( [ x for x in sampler ] , [ x for x in sampler ] ) <EOL> def test_randomized_search_grid_scores ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:100> , n_informative = <NUM_LIT:3> , <EOL> random_state = <NUM_LIT:0> ) <EOL> params = dict ( C = expon ( scale = <NUM_LIT:10> ) , <EOL> gamma = expon ( scale = <NUM_LIT:0.1> ) ) <EOL> n_cv_iter = <NUM_LIT:3> <EOL> n_search_iter = <NUM_LIT:30> <EOL> search = RandomizedSearchCV ( SVC ( ) , n_iter = n_search_iter , cv = n_cv_iter , <EOL> param_distributions = params , iid = False ) <EOL> search . fit ( X , y ) <EOL> assert_equal ( len ( search . grid_scores_ ) , n_search_iter ) <EOL> for cv_score in search . grid_scores_ : <EOL> assert_equal ( len ( cv_score . cv_validation_scores ) , n_cv_iter ) <EOL> assert_almost_equal ( np . mean ( cv_score . cv_validation_scores ) , <EOL> cv_score . mean_validation_score ) <EOL> assert_equal ( list ( sorted ( cv_score . parameters . keys ( ) ) ) , <EOL> list ( sorted ( params . keys ( ) ) ) ) <EOL> sorted_grid_scores = list ( sorted ( search . grid_scores_ , <EOL> key = lambda x : x . mean_validation_score ) ) <EOL> best_score = sorted_grid_scores [ - <NUM_LIT:1> ] . mean_validation_score <EOL> assert_equal ( search . best_score_ , best_score ) <EOL> tied_best_params = [ s . parameters for s in sorted_grid_scores <EOL> if s . mean_validation_score == best_score ] <EOL> assert_true ( search . best_params_ in tied_best_params , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> search . best_params_ , tied_best_params ) ) <EOL> def test_grid_search_score_consistency ( ) : <EOL> clf = LinearSVC ( random_state = <NUM_LIT:0> ) <EOL> X , y = make_blobs ( random_state = <NUM_LIT:0> , centers = <NUM_LIT:2> ) <EOL> Cs = [ <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:10> ] <EOL> for score in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT:C>' : Cs } , scoring = score ) <EOL> grid_search . fit ( X , y ) <EOL> cv = StratifiedKFold ( n_folds = <NUM_LIT:3> ) <EOL> for C , scores in zip ( Cs , grid_search . grid_scores_ ) : <EOL> clf . set_params ( C = C ) <EOL> scores = scores [ <NUM_LIT:2> ] <EOL> i = <NUM_LIT:0> <EOL> for train , test in cv . split ( X , y ) : <EOL> clf . fit ( X [ train ] , y [ train ] ) <EOL> if score == "<STR_LIT>" : <EOL> correct_score = f1_score ( y [ test ] , clf . predict ( X [ test ] ) ) <EOL> elif score == "<STR_LIT>" : <EOL> dec = clf . decision_function ( X [ test ] ) <EOL> correct_score = roc_auc_score ( y [ test ] , dec ) <EOL> assert_almost_equal ( correct_score , scores [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> def test_pickle ( ) : <EOL> clf = MockClassifier ( ) <EOL> grid_search = GridSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , refit = True ) <EOL> grid_search . fit ( X , y ) <EOL> pickle . dumps ( grid_search ) <EOL> random_search = RandomizedSearchCV ( clf , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , <EOL> refit = True , n_iter = <NUM_LIT:3> ) <EOL> random_search . fit ( X , y ) <EOL> pickle . dumps ( random_search ) <EOL> def test_grid_search_with_multioutput_data ( ) : <EOL> X , y = make_multilabel_classification ( return_indicator = True , <EOL> random_state = <NUM_LIT:0> ) <EOL> est_parameters = { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] } <EOL> cv = KFold ( random_state = <NUM_LIT:0> ) <EOL> estimators = [ DecisionTreeRegressor ( random_state = <NUM_LIT:0> ) , <EOL> DecisionTreeClassifier ( random_state = <NUM_LIT:0> ) ] <EOL> for est in estimators : <EOL> grid_search = GridSearchCV ( est , est_parameters , cv = cv ) <EOL> grid_search . fit ( X , y ) <EOL> for parameters , _ , cv_validation_scores in grid_search . grid_scores_ : <EOL> est . set_params ( ** parameters ) <EOL> for i , ( train , test ) in enumerate ( cv . split ( X , y ) ) : <EOL> est . fit ( X [ train ] , y [ train ] ) <EOL> correct_score = est . score ( X [ test ] , y [ test ] ) <EOL> assert_almost_equal ( correct_score , <EOL> cv_validation_scores [ i ] ) <EOL> for est in estimators : <EOL> random_search = RandomizedSearchCV ( est , est_parameters , <EOL> cv = cv , n_iter = <NUM_LIT:3> ) <EOL> random_search . fit ( X , y ) <EOL> for parameters , _ , cv_validation_scores in random_search . grid_scores_ : <EOL> est . set_params ( ** parameters ) <EOL> for i , ( train , test ) in enumerate ( cv . split ( X , y ) ) : <EOL> est . fit ( X [ train ] , y [ train ] ) <EOL> correct_score = est . score ( X [ test ] , y [ test ] ) <EOL> assert_almost_equal ( correct_score , <EOL> cv_validation_scores [ i ] ) <EOL> def test_predict_proba_disabled ( ) : <EOL> X = np . arange ( <NUM_LIT:20> ) . reshape ( <NUM_LIT:5> , - <NUM_LIT:1> ) <EOL> y = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> clf = SVC ( probability = False ) <EOL> gs = GridSearchCV ( clf , { } , cv = <NUM_LIT:2> ) . fit ( X , y ) <EOL> assert_false ( hasattr ( gs , "<STR_LIT>" ) ) <EOL> def test_grid_search_allows_nans ( ) : <EOL> X = np . arange ( <NUM_LIT:20> , dtype = np . float64 ) . reshape ( <NUM_LIT:5> , - <NUM_LIT:1> ) <EOL> X [ <NUM_LIT:2> , : ] = np . nan <EOL> y = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> p = Pipeline ( [ <EOL> ( '<STR_LIT>' , Imputer ( strategy = '<STR_LIT>' , missing_values = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , MockClassifier ( ) ) , <EOL> ] ) <EOL> GridSearchCV ( p , { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , cv = <NUM_LIT:2> ) . fit ( X , y ) <EOL> class FailingClassifier ( BaseEstimator ) : <EOL> """<STR_LIT>""" <EOL> FAILING_PARAMETER = <NUM_LIT:2> <EOL> def __init__ ( self , parameter = None ) : <EOL> self . parameter = parameter <EOL> def fit ( self , X , y = None ) : <EOL> if self . parameter == FailingClassifier . FAILING_PARAMETER : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def predict ( self , X ) : <EOL> return np . zeros ( X . shape [ <NUM_LIT:0> ] ) <EOL> def test_grid_search_failing_classifier ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:10> , random_state = <NUM_LIT:0> ) <EOL> clf = FailingClassifier ( ) <EOL> gs = GridSearchCV ( clf , [ { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] } ] , scoring = '<STR_LIT>' , <EOL> refit = False , error_score = <NUM_LIT:0.0> ) <EOL> assert_warns ( FitFailedWarning , gs . fit , X , y ) <EOL> assert all ( np . all ( this_point . cv_validation_scores == <NUM_LIT:0.0> ) <EOL> for this_point in gs . grid_scores_ <EOL> if this_point . parameters [ '<STR_LIT>' ] == <EOL> FailingClassifier . FAILING_PARAMETER ) <EOL> gs = GridSearchCV ( clf , [ { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] } ] , scoring = '<STR_LIT>' , <EOL> refit = False , error_score = float ( '<STR_LIT>' ) ) <EOL> assert_warns ( FitFailedWarning , gs . fit , X , y ) <EOL> assert all ( np . all ( np . isnan ( this_point . cv_validation_scores ) ) <EOL> for this_point in gs . grid_scores_ <EOL> if this_point . parameters [ '<STR_LIT>' ] == <EOL> FailingClassifier . FAILING_PARAMETER ) <EOL> def test_grid_search_failing_classifier_raise ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:10> , random_state = <NUM_LIT:0> ) <EOL> clf = FailingClassifier ( ) <EOL> gs = GridSearchCV ( clf , [ { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] } ] , scoring = '<STR_LIT>' , <EOL> refit = False , error_score = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , gs . fit , X , y ) <EOL> def test_parameters_sampler_replacement ( ) : <EOL> params = { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> ] , '<STR_LIT>' : [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] } <EOL> sampler = ParameterSampler ( params , n_iter = <NUM_LIT:7> ) <EOL> assert_raises ( ValueError , list , sampler ) <EOL> sampler = ParameterSampler ( params , n_iter = <NUM_LIT:6> ) <EOL> samples = list ( sampler ) <EOL> assert_equal ( len ( samples ) , <NUM_LIT:6> ) <EOL> for values in ParameterGrid ( params ) : <EOL> assert_true ( values in samples ) <EOL> params = { '<STR_LIT:a>' : range ( <NUM_LIT:10> ) , '<STR_LIT:b>' : range ( <NUM_LIT:10> ) , '<STR_LIT:c>' : range ( <NUM_LIT:10> ) } <EOL> sampler = ParameterSampler ( params , n_iter = <NUM_LIT> , random_state = <NUM_LIT> ) <EOL> samples = list ( sampler ) <EOL> assert_equal ( len ( samples ) , <NUM_LIT> ) <EOL> hashable_samples = [ "<STR_LIT>" % ( p [ '<STR_LIT:a>' ] , p [ '<STR_LIT:b>' ] , p [ '<STR_LIT:c>' ] ) <EOL> for p in samples ] <EOL> assert_equal ( len ( set ( hashable_samples ) ) , <NUM_LIT> ) <EOL> params_distribution = { '<STR_LIT>' : bernoulli ( <NUM_LIT> ) , '<STR_LIT>' : [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] } <EOL> sampler = ParameterSampler ( params_distribution , n_iter = <NUM_LIT:7> ) <EOL> samples = list ( sampler ) <EOL> assert_equal ( len ( samples ) , <NUM_LIT:7> ) </s>
