@@ -126,6 +126,7 @@ def _is_training_data(self, X):
126
126
X = np .ones ((10 , 2 ))
127
127
X_sparse = coo_matrix (X )
128
128
y = np .arange (10 ) // 2
129
+ y2 = np .array ([1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 ]) // 2
129
130
130
131
131
132
def test_cross_val_score ():
@@ -134,38 +135,38 @@ def test_cross_val_score():
134
135
for a in range (- 10 , 10 ):
135
136
clf .a = a
136
137
# Smoke test
137
- scores = cross_val_score (clf , X , y )
138
- assert_array_equal (scores , clf .score (X , y ))
138
+ scores = cross_val_score (clf , X , y2 )
139
+ assert_array_equal (scores , clf .score (X , y2 ))
139
140
140
141
# test with multioutput y
141
- scores = cross_val_score (clf , X_sparse , X )
142
- assert_array_equal (scores , clf .score (X_sparse , X ))
142
+ multioutput_y = np .column_stack ([y2 , y2 [::- 1 ]])
143
+ scores = cross_val_score (clf , X_sparse , multioutput_y )
144
+ assert_array_equal (scores , clf .score (X_sparse , multioutput_y ))
143
145
144
- scores = cross_val_score (clf , X_sparse , y )
145
- assert_array_equal (scores , clf .score (X_sparse , y ))
146
+ scores = cross_val_score (clf , X_sparse , y2 )
147
+ assert_array_equal (scores , clf .score (X_sparse , y2 ))
146
148
147
149
# test with multioutput y
148
- scores = cross_val_score (clf , X_sparse , X )
149
- assert_array_equal (scores , clf .score (X_sparse , X ))
150
+ scores = cross_val_score (clf , X_sparse , multioutput_y )
151
+ assert_array_equal (scores , clf .score (X_sparse , multioutput_y ))
150
152
151
153
# test with X and y as list
152
154
list_check = lambda x : isinstance (x , list )
153
155
clf = CheckingClassifier (check_X = list_check )
154
- scores = cross_val_score (clf , X .tolist (), y .tolist ())
156
+ scores = cross_val_score (clf , X .tolist (), y2 .tolist ())
155
157
156
158
clf = CheckingClassifier (check_y = list_check )
157
- scores = cross_val_score (clf , X , y .tolist ())
159
+ scores = cross_val_score (clf , X , y2 .tolist ())
158
160
159
- assert_raises (ValueError , cross_val_score , clf , X , y ,
160
- scoring = "sklearn" )
161
+ assert_raises (ValueError , cross_val_score , clf , X , y2 , scoring = "sklearn" )
161
162
162
163
# test with 3d X and
163
164
X_3d = X [:, :, np .newaxis ]
164
165
clf = MockClassifier (allow_nd = True )
165
- scores = cross_val_score (clf , X_3d , y )
166
+ scores = cross_val_score (clf , X_3d , y2 )
166
167
167
168
clf = MockClassifier (allow_nd = False )
168
- assert_raises (ValueError , cross_val_score , clf , X_3d , y )
169
+ assert_raises (ValueError , cross_val_score , clf , X_3d , y2 )
169
170
170
171
171
172
def test_cross_val_score_predict_labels ():
@@ -197,7 +198,8 @@ def test_cross_val_score_pandas():
197
198
pass
198
199
for TargetType , InputFeatureType in types :
199
200
# X dataframe, y series
200
- X_df , y_ser = InputFeatureType (X ), TargetType (y )
201
+ # 3 fold cross val is used so we need atleast 3 samples per class
202
+ X_df , y_ser = InputFeatureType (X ), TargetType (y2 )
201
203
check_df = lambda x : isinstance (x , InputFeatureType )
202
204
check_series = lambda x : isinstance (x , TargetType )
203
205
clf = CheckingClassifier (check_X = check_df , check_y = check_series )
@@ -476,21 +478,27 @@ def split(self, X, y=None, labels=None):
476
478
477
479
478
480
def test_cross_val_predict_input_types ():
479
- clf = Ridge ()
481
+ iris = load_iris ()
482
+ X , y = iris .data , iris .target
483
+ X_sparse = coo_matrix (X )
484
+ multioutput_y = np .column_stack ([y , y [::- 1 ]])
485
+
486
+ clf = Ridge (random_state = 0 )
487
+ # 3 fold cv is used --> atleast 3 samples per class
480
488
# Smoke test
481
489
predictions = cross_val_predict (clf , X , y )
482
- assert_equal (predictions .shape , (10 ,))
490
+ assert_equal (predictions .shape , (150 ,))
483
491
484
492
# test with multioutput y
485
- predictions = cross_val_predict (clf , X_sparse , X )
486
- assert_equal (predictions .shape , (10 , 2 ))
493
+ predictions = cross_val_predict (clf , X_sparse , multioutput_y )
494
+ assert_equal (predictions .shape , (150 , 2 ))
487
495
488
496
predictions = cross_val_predict (clf , X_sparse , y )
489
- assert_array_equal (predictions .shape , (10 ,))
497
+ assert_array_equal (predictions .shape , (150 ,))
490
498
491
499
# test with multioutput y
492
- predictions = cross_val_predict (clf , X_sparse , X )
493
- assert_array_equal (predictions .shape , (10 , 2 ))
500
+ predictions = cross_val_predict (clf , X_sparse , multioutput_y )
501
+ assert_array_equal (predictions .shape , (150 , 2 ))
494
502
495
503
# test with X and y as list
496
504
list_check = lambda x : isinstance (x , list )
@@ -505,7 +513,7 @@ def test_cross_val_predict_input_types():
505
513
check_3d = lambda x : x .ndim == 3
506
514
clf = CheckingClassifier (check_X = check_3d )
507
515
predictions = cross_val_predict (clf , X_3d , y )
508
- assert_array_equal (predictions .shape , (10 ,))
516
+ assert_array_equal (predictions .shape , (150 ,))
509
517
510
518
511
519
def test_cross_val_predict_pandas ():
@@ -518,7 +526,7 @@ def test_cross_val_predict_pandas():
518
526
pass
519
527
for TargetType , InputFeatureType in types :
520
528
# X dataframe, y series
521
- X_df , y_ser = InputFeatureType (X ), TargetType (y )
529
+ X_df , y_ser = InputFeatureType (X ), TargetType (y2 )
522
530
check_df = lambda x : isinstance (x , InputFeatureType )
523
531
check_series = lambda x : isinstance (x , TargetType )
524
532
clf = CheckingClassifier (check_X = check_df , check_y = check_series )
0 commit comments