Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0315318

Browse files
committed
TST/FIX Make model selection tests warning free!
- Use data that will converge for the multioutput case - Use atleast 3 samples per class to conform to 3fold cv - Add the elided ignore warnings line - Use the iris dataset to prevent non-convergence of sag solver
1 parent 04ca448 commit 0315318

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

sklearn/model_selection/tests/test_search.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ def test_grid_search_labels():
246246

247247
non_label_cvs = [StratifiedKFold(), StratifiedShuffleSplit()]
248248
for cv in non_label_cvs:
249-
print(cv)
250249
gs = GridSearchCV(clf, grid, cv=cv)
251250
# Should not raise an error
252251
gs.fit(X, y)

sklearn/model_selection/tests/test_split.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def test_kfold_valueerrors():
216216
# though all the classes are not necessarily represented at on each
217217
# side of the split at each split
218218
with warnings.catch_warnings():
219+
warnings.simplefilter("ignore")
219220
check_cv_coverage(skf_3, X2, y, labels=None, expected_n_iter=3)
220221

221222
# Error when number of folds is <= 1

sklearn/model_selection/tests/test_validation.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def _is_training_data(self, X):
126126
X = np.ones((10, 2))
127127
X_sparse = coo_matrix(X)
128128
y = np.arange(10) // 2
129+
y2 = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 3]) // 2
129130

130131

131132
def test_cross_val_score():
@@ -134,38 +135,38 @@ def test_cross_val_score():
134135
for a in range(-10, 10):
135136
clf.a = a
136137
# 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))
139140

140141
# 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))
143145

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))
146148

147149
# 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))
150152

151153
# test with X and y as list
152154
list_check = lambda x: isinstance(x, list)
153155
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())
155157

156158
clf = CheckingClassifier(check_y=list_check)
157-
scores = cross_val_score(clf, X, y.tolist())
159+
scores = cross_val_score(clf, X, y2.tolist())
158160

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")
161162

162163
# test with 3d X and
163164
X_3d = X[:, :, np.newaxis]
164165
clf = MockClassifier(allow_nd=True)
165-
scores = cross_val_score(clf, X_3d, y)
166+
scores = cross_val_score(clf, X_3d, y2)
166167

167168
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)
169170

170171

171172
def test_cross_val_score_predict_labels():
@@ -197,7 +198,8 @@ def test_cross_val_score_pandas():
197198
pass
198199
for TargetType, InputFeatureType in types:
199200
# 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)
201203
check_df = lambda x: isinstance(x, InputFeatureType)
202204
check_series = lambda x: isinstance(x, TargetType)
203205
clf = CheckingClassifier(check_X=check_df, check_y=check_series)
@@ -476,21 +478,27 @@ def split(self, X, y=None, labels=None):
476478

477479

478480
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
480488
# Smoke test
481489
predictions = cross_val_predict(clf, X, y)
482-
assert_equal(predictions.shape, (10,))
490+
assert_equal(predictions.shape, (150,))
483491

484492
# 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))
487495

488496
predictions = cross_val_predict(clf, X_sparse, y)
489-
assert_array_equal(predictions.shape, (10,))
497+
assert_array_equal(predictions.shape, (150,))
490498

491499
# 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))
494502

495503
# test with X and y as list
496504
list_check = lambda x: isinstance(x, list)
@@ -505,7 +513,7 @@ def test_cross_val_predict_input_types():
505513
check_3d = lambda x: x.ndim == 3
506514
clf = CheckingClassifier(check_X=check_3d)
507515
predictions = cross_val_predict(clf, X_3d, y)
508-
assert_array_equal(predictions.shape, (10,))
516+
assert_array_equal(predictions.shape, (150,))
509517

510518

511519
def test_cross_val_predict_pandas():
@@ -518,7 +526,7 @@ def test_cross_val_predict_pandas():
518526
pass
519527
for TargetType, InputFeatureType in types:
520528
# X dataframe, y series
521-
X_df, y_ser = InputFeatureType(X), TargetType(y)
529+
X_df, y_ser = InputFeatureType(X), TargetType(y2)
522530
check_df = lambda x: isinstance(x, InputFeatureType)
523531
check_series = lambda x: isinstance(x, TargetType)
524532
clf = CheckingClassifier(check_X=check_df, check_y=check_series)

0 commit comments

Comments
 (0)