8000 Merge pull request #5283 from TomDLT/remove_warnings · scikit-learn/scikit-learn@37bd275 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37bd275

Browse files
committed
Merge pull request #5283 from TomDLT/remove_warnings
[MRG] Remove some warnings from test suit
2 parents 9ea9b34 + fecd355 commit 37bd275

File tree

8 files changed

+51
-35
lines changed

8 files changed

+51
-35
lines changed

sklearn/decomposition/tests/test_kernel_pca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def histogram(x, y, **kwargs):
3939

4040
# non-regression test: previously, gamma would be 0 by default,
4141
# forcing all eigenvalues to 0 under the poly kernel
42-
assert_not_equal(X_fit_transformed, [])
42+
assert_not_equal(X_fit_transformed.size, 0)
4343

4444
# transform new data
4545
X_pred_transformed = kpca.transform(X_pred)

sklearn/metrics/ranking.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from ..utils.multiclass import type_of_target
2929
from ..utils.fixes import isclose
3030
from ..utils.fixes import bincount
31+
from ..utils.fixes import array_equal
3132
from ..utils.stats import rankdata
3233
from ..utils.sparsefuncs import count_nonzero
3334

@@ -295,11 +296,11 @@ def _binary_clf_curve(y_true, y_score, pos_label=None, sample_weight=None):
295296
# ensure binary classification if pos_label is not specified
296297
classes = np.unique(y_true)
297298
if (pos_label is None and
298-
not (np.all(classes == [0, 1]) or
299-
np.all(classes == [-1, 1]) or
300-
np.all(classes == [0]) or
301-
np.all(classes == [-1]) or
302-
np.all(classes == [1]))):
299+
not (array_equal(classes, [0, 1]) or
300+
array_equal(classes, [-1, 1]) or
301+
array_equal(classes, [0]) or
302+
array_equal(classes, [-1]) or
303+
array_equal(classes, [1]))):
303304
raise ValueError("Data is not binary and pos_label is not specified")
304305
elif pos_label is None:
305306
pos_label = 1.

sklearn/metrics/tests/test_classification.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ def test_precision_recall_f_binary_single_class():
161161

162162
@ignore_warnings
163163
def test_precision_recall_f_extra_labels():
164-
"""Test handling of explicit additional (not in input) labels to PRF
165-
"""
164+
# Test handling of explicit additional (not in input) labels to PRF
166165
y_true = [1, 3, 3, 2]
167166
y_pred = [1, 1, 3, 2]
168167
y_true_bin = label_binarize(y_true, classes=np.arange(5))
@@ -202,7 +201,7 @@ def test_precision_recall_f_extra_labels():
202201

203202
@ignore_warnings
204203
def test_precision_recall_f_ignored_labels():
205-
"""Test a subset of labels may be requested for PRF"""
204+
# Test a subset of labels may be requested for PRF
206205
y_true = [1, 1, 2, 3]
207206
y_pred = [1, 3, 3, 3]
208207
y_true_bin = label_binarize(y_true, classes=np.arange(5))
@@ -323,6 +322,7 @@ def test_cohen_kappa():
323322
assert_almost_equal(cohen_kappa_score(y1, y2), .8013, decimal=4)
324323

325324

325+
@ignore_warnings
326326
def test_matthews_corrcoef_nan():
327327
assert_equal(matthews_corrcoef([0], [1]), 0.0)
328328
assert_equal(matthews_corrcoef([0, 0], [0, 1]), 0.0)
@@ -647,8 +647,8 @@ def test_multilabel_hamming_loss():
647647
assert_equal(hamming_loss(y1, y2), 1 / 6)
648648
assert_equal(hamming_loss(y1, y1), 0)
649649
assert_equal(hamming_loss(y2, y2), 0)
650-
assert_equal(hamming_loss(y2, np.logical_not(y2)), 1)
651-
assert_equal(hamming_loss(y1, np.logical_not(y1)), 1)
650+
assert_equal(hamming_loss(y2, 1 - y2), 1)
651+
assert_equal(hamming_loss(y1, 1 - y1), 1)
652652
assert_equal(hamming_loss(y1, np.zeros(y1.shape)), 4 / 6)
653653
assert_equal(hamming_loss(y2, np.zeros(y1.shape)), 0.5)
654654

@@ -807,6 +807,7 @@ def test_precision_recall_f1_score_multilabel_2():
807807
0.1666, 2)
808808

809809

810+
@ignore_warnings
810811
def test_precision_recall_f1_score_with_an_empty_prediction():
811812
y_true = np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 1, 1, 0]])
812813
y_pred = np.array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 1, 1, 0]])
@@ -1142,11 +1143,11 @@ def test_hinge_loss_binary():
11421143

11431144
def test_hinge_loss_multiclass():
11441145
pred_decision = np.array([
1145-
[0.36, -0.17, -0.58, -0.99],
1146+
[+0.36, -0.17, -0.58, -0.99],
11461147
[-0.54, -0.37, -0.48, -0.58],
11471148
[-1.45, -0.58, -0.38, -0.17],
11481149
[-0.54, -0.38, -0.48, -0.58],
1149-
[-2.36, -0.79, -0.27, 0.24],
1150+
[-2.36, -0.79, -0.27, +0.24],
11501151
[-1.45, -0.58, -0.38, -0.17]
11511152
])
11521153
y_true = np.array([0, 1, 2, 1, 3, 2])
@@ -1167,10 +1168,10 @@ def test_hinge_loss_multiclass():
11671168
def test_hinge_loss_multiclass_missing_labels_with_labels_none():
11681169
y_true = np.array([0, 1, 2, 2])
11691170
pred_decision = np.array([
1170-
[1.27, 0.034, -0.68, -1.40],
1171+
[+1.27, 0.034, -0.68, -1.40],
11711172
[-1.45, -0.58, -0.38, -0.17],
1172-
[-2.36, -0.79, -0.27, 0.24],
1173-
[-2.36, -0.79, -0.27, 0.24]
1173+
[-2.36, -0.79, -0.27, +0.24],
1174+
[-2.36, -0.79, -0.27, +0.24]
11741175
])
11751176
error_message = ("Please include all labels in y_true "
11761177
"or pass labels as third argument")
@@ -1181,7 +1182,7 @@ def test_hinge_loss_multiclass_missing_labels_with_labels_none():
11811182

11821183
def test_hinge_loss_multiclass_with_missing_labels():
11831184
pred_decision = np.array([
1184-
[0.36, -0.17, -0.58, -0.99],
1185+
[+0.36, -0.17, -0.58, -0.99],
11851186
[-0.55, -0.38, -0.48, -0.58],
11861187
[-1.45, -0.58, 10000 -0.38, -0.17],
11871188
[-0.55, -0.38, -0.48, -0.58],
@@ -1209,12 +1210,12 @@ def test_hinge_loss_multiclass_invariance_lists():
12091210
y_true = ['blue', 'green', 'red',
12101211
'green', 'white', 'red']
12111212
pred_decision = [
1212-
[0.36, -0.17, -0.58, -0.99],
1213+
[+0.36, -0.17, -0.58, -0.99],
12131214
[-0.55, -0.38, -0.48, -0.58],
1214-
[-1.45, -0.58, -0.38, -0.17],
1215+
[-1.45, -0.58, -0.38, -0.17],
12151216
[-0.55, -0.38, -0.48, -0.58],
1216-
[-2.36, -0.79, -0.27, 0.24],
1217-
[-1.45, -0.58, -0.38, -0.17]]
1217+
[-2.36, -0.79, -0.27, +0.24],
1218+
[-1.45, -0.58, -0.38, -0.17]]
12181219
dummy_losses = np.array([
12191220
1 - pred_decision[0][0] + pred_decision[0][1],
12201221
1 - pred_decision[1][1] + pred_decision[1][2],

sklearn/metrics/tests/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"mean_squared_error": mean_squared_error,
9494
"median_absolute_error": median_absolute_error,
9595
"explained_variance_score": explained_variance_score,
96-
"r2_score": r2_score,
96+
"r2_score": partial(r2_score, multioutput='variance_weighted'),
9797
}
9898

9999
CLASSIFICATION_METRICS = {

sklearn/tests/test_cross_validation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def get_params(self, deep=False):
111111
W_sparse = coo_matrix((np.array([1]), (np.array([1]), np.array([0]))),
112112
shape=(10, 1))
113113
P_sparse = coo_matrix(np.eye(5))
114-
y = np.arange(10) // 2
114+
115+
# avoid StratifiedKFold's Warning about least populated class in y
116+
y = np.arange(10) % 3
115117

116118
##############################################################################
117119
# Tests
@@ -289,12 +291,10 @@ def test_shuffle_kfold():
289291

290292
all_folds = None
291293
for train, test in kf:
292-
sorted_array = np.arange(100)
293-
assert_true(np.any(sorted_array != ind[train]))
294-
sorted_array = np.arange(101, 200)
295-
assert_true(np.any(sorted_array != ind[train]))
296-
sorted_array = np.arange(201, 300)
297-
assert_true(np.any(sorted_array != ind[train]))
294+
assert_true(np.any(np.arange(100) != ind[test]))
295+
assert_true(np.any(np.arange(100, 200) != ind[test]))
296+
assert_true(np.any(np.arange(200, 300) != ind[test]))
297+
298298
if all_folds is None:
299299
all_folds = ind[test].copy()
300300
else:
@@ -396,6 +396,7 @@ def test_label_kfold():
396396
'Robert', 'Marion', 'David', 'Tony', 'Abel', 'Becky',
397397
'Madmood', 'Cary', 'Mary', 'Alexandre', 'David', 'Francis',
398398
'Barack', 'Abdoul', 'Rasha', 'Xi', 'Silvia']
399+
labels = np.asarray(labels, dtype=object)
399400

400401
n_labels = len(np.unique(labels))
401402
n_samples = len(labels)
@@ -415,7 +416,6 @@ def test_label_kfold():
415416
assert_equal(len(np.unique(folds[labels == label])), 1)
416417

417418
# Check that no label is on both sides of the split
418-
labels = np.asarray(labels, dtype=object)
419419
for train, test in cval.LabelKFold(labels, n_folds=n_folds):
420420
assert_equal(len(np.intersect1d(labels[train], labels[test])), 0)
421421

@@ -560,7 +560,7 @@ def test_label_shuffle_split():
560560

561561
for y in ys:
562562
n_iter = 6
563-
test_size = 1./3
563+
test_size = 1. / 3
564564
slo = cval.LabelShuffleSplit(y, n_iter, test_size=test_size,
565565
random_state=0)
566566

sklearn/tests/test_grid_search.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ def predict(self, X):
429429
return np.zeros(X.shape[0])
430430

431431

432+
@ignore_warnings
432433
def test_refit():
433434
# Regression test for bug in refitting
434435
# Simulates re-fitting a broken estimator; this used to break with

sklearn/utils/fixes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,17 @@ def makedirs(name, mode=0o777, exist_ok=False):
375375
if (not exist_ok or e.errno != errno.EEXIST
376376
or not os.path.isdir(name)):
377377
raise
378+
379+
380+
if np_version < (1, 8, 1):
381+
def array_equal(a1, a2):
382+
# copy-paste from numpy 1.8.1
383+
try:
384+
a1, a2 = np.asarray(a1), np.asarray(a2)
385+
except:
386+
return False
387+
if a1.shape != a2.shape:
388+
return False
389+
return bool(np.asarray(a1 == a2).all())
390+
else:
391+
from numpy import array_equal

sklearn/utils/multiclass.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
import numpy as np
2020

2121
from ..externals.six import string_types
22-
2322
from .validation import check_array
24-
2523
from ..utils.fixes import bincount
24+
from ..utils.fixes import array_equal
2625

2726

2827
def _unique_multiclass(y):
@@ -148,7 +147,7 @@ def is_multilabel(y):
148147
if issparse(y):
149148
if isinstance(y, (dok_matrix, lil_matrix)):
150149
y = y.tocsr()
151-
return (len(y.data) == 0 or np.ptp(y.data) == 0 and
150+
return (len(y.data) == 0 or np.unique(y.data).size == 1 and
152151
(y.dtype.kind in 'biu' or # bool, int, uint
153152
_is_integral_float(np.unique(y.data))))
154153
else:
@@ -283,7 +282,7 @@ def _check_partial_fit_first_call(clf, classes=None):
283282

284283
elif classes is not None:
285284
if getattr(clf, 'classes_', None) is not None:
286-
if not np.all(clf.classes_ == unique_labels(classes)):
285+
if not array_equal(clf.classes_, unique_labels(classes)):
287286
raise ValueError(
288287
"`classes=%r` is not the same as on last call "
289288
"to partial_fit, was: %r" % (classes, clf.classes_))

0 commit comments

Comments
 (0)
0