@@ -125,7 +125,9 @@ def _is_training_data(self, X):
125125# check_consistent_length
126126X = np .ones ((10 , 2 ))
127127X_sparse = coo_matrix (X )
128- y = np .arange (10 ) // 2
128+ y = np .array ([0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 ])
129+ # The number of samples per class needs to be > n_folds, for StratifiedKFold(3)
130+ y2 = np .array ([1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 ])
129131
130132
131133def test_cross_val_score ():
@@ -134,38 +136,38 @@ def test_cross_val_score():
134136 for a in range (- 10 , 10 ):
135137 clf .a = a
136138 # Smoke test
137- scores = cross_val_score (clf , X , y )
138- assert_array_equal (scores , clf .score (X , y ))
139+ scores = cross_val_score (clf , X , y2 )
140+ assert_array_equal (scores , clf .score (X , y2 ))
139141
140142 # test with multioutput y
141- scores = cross_val_score (clf , X_sparse , X )
142- assert_array_equal (scores , clf .score (X_sparse , X ))
143+ multioutput_y = np .column_stack ([y2 , y2 [::- 1 ]])
144+ scores = cross_val_score (clf , X_sparse , multioutput_y )
145+ assert_array_equal (scores , clf .score (X_sparse , multioutput_y ))
143146
144- scores = cross_val_score (clf , X_sparse , y )
145- assert_array_equal (scores , clf .score (X_sparse , y ))
147+ scores = cross_val_score (clf , X_sparse , y2 )
148+ assert_array_equal (scores , clf .score (X_sparse , y2 ))
146149
147150 # test with multioutput y
148- scores = cross_val_score (clf , X_sparse , X )
149- assert_array_equal (scores , clf .score (X_sparse , X ))
151+ scores = cross_val_score (clf , X_sparse , multioutput_y )
152+ assert_array_equal (scores , clf .score (X_sparse , multioutput_y ))
150153
151154 # test with X and y as list
152155 list_check = lambda x : isinstance (x , list )
153156 clf = CheckingClassifier (check_X = list_check )
154- scores = cross_val_score (clf , X .tolist (), y .tolist ())
157+ scores = cross_val_score (clf , X .tolist (), y2 .tolist ())
155158
156159 clf = CheckingClassifier (check_y = list_check )
157- scores = cross_val_score (clf , X , y .tolist ())
160+ scores = cross_val_score (clf , X , y2 .tolist ())
158161
159- assert_raises (ValueError , cross_val_score , clf , X , y ,
160- scoring = "sklearn" )
162+ assert_raises (ValueError , cross_val_score , clf , X , y2 , scoring = "sklearn" )
161163
162164 # test with 3d X and
163165 X_3d = X [:, :, np .newaxis ]
164166 clf = MockClassifier (allow_nd = True )
165- scores = cross_val_score (clf , X_3d , y )
167+ scores = cross_val_score (clf , X_3d , y2 )
166168
167169 clf = MockClassifier (allow_nd = False )
168- assert_raises (ValueError , cross_val_score , clf , X_3d , y )
170+ assert_raises (ValueError , cross_val_score , clf , X_3d , y2 )
169171
170172
171173def test_cross_val_score_predict_labels ():
@@ -197,7 +199,8 @@ def test_cross_val_score_pandas():
197199 pass
198200 for TargetType , InputFeatureType in types :
199201 # X dataframe, y series
200- X_df , y_ser = InputFeatureType (X ), TargetType (y )
202+ # 3 fold cross val is used so we need atleast 3 samples per class
203+ X_df , y_ser = InputFeatureType (X ), TargetType (y2 )
201204 check_df = lambda x : isinstance (x , InputFeatureType )
202205 check_series = lambda x : isinstance (x , TargetType )
203206 clf = CheckingClassifier (check_X = check_df , check_y = check_series )
@@ -476,21 +479,27 @@ def split(self, X, y=None, labels=None):
476479
477480
478481def test_cross_val_predict_input_types ():
479- clf = Ridge ()
482+ iris = load_iris ()
483+ X , y = iris .data , iris .target
484+ X_sparse = coo_matrix (X )
485+ multioutput_y = np .column_stack ([y , y [::- 1 ]])
486+
487+ clf = Ridge (fit_intercept = False , random_state = 0 )
488+ # 3 fold cv is used --> atleast 3 samples per class
480489 # Smoke test
481490 predictions = cross_val_predict (clf , X , y )
482- assert_equal (predictions .shape , (10 ,))
491+ assert_equal (predictions .shape , (150 ,))
483492
484493 # test with multioutput y
485- predictions = cross_val_predict (clf , X_sparse , X )
486- assert_equal (predictions .shape , (10 , 2 ))
494+ predictions = cross_val_predict (clf , X_sparse , multioutput_y )
495+ assert_equal (predictions .shape , (150 , 2 ))
487496
488497 predictions = cross_val_predict (clf , X_sparse , y )
489- assert_array_equal (predictions .shape , (10 ,))
498+ assert_array_equal (predictions .shape , (150 ,))
490499
491500 # test with multioutput y
492- predictions = cross_val_predict (clf , X_sparse , X )
493- assert_array_equal (predictions .shape , (10 , 2 ))
501+ predictions = cross_val_predict (clf , X_sparse , multioutput_y )
502+ assert_array_equal (predictions .shape , (150 , 2 ))
494503
495504 # test with X and y as list
496505 list_check = lambda x : isinstance (x , list )
@@ -505,7 +514,7 @@ def test_cross_val_predict_input_types():
505514 check_3d = lambda x : x .ndim == 3
506515 clf = CheckingClassifier (check_X = check_3d )
507516 predictions = cross_val_predict (clf , X_3d , y )
508- assert_array_equal (predictions .shape , (10 ,))
517+ assert_array_equal (predictions .shape , (150 ,))
509518
510519
511520def test_cross_val_predict_pandas ():
@@ -518,7 +527,7 @@ def test_cross_val_predict_pandas():
518527 pass
519528 for TargetType , InputFeatureType in types :
520529 # X dataframe, y series
521- X_df , y_ser = InputFeatureType (X ), TargetType (y )
530+ X_df , y_ser = InputFeatureType (X ), TargetType (y2 )
522531 check_df = lambda x : isinstance (x , InputFeatureType )
523532 check_series = lambda x : isinstance (x , TargetType )
524533 clf = CheckingClassifier (check_X = check_df , check_y = check_series )
0 commit comments