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

Skip to content

Commit 28eb1eb

Browse files
jnothmanogrisel
authored andcommitted
Assert or ignore all sequence of sequences deprecation warnings
1 parent 9cfa1f3 commit 28eb1eb

File tree

6 files changed

+56
-46
lines changed

6 files changed

+56
-46
lines changed

sklearn/datasets/tests/test_samples_generator.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from sklearn.utils.testing import assert_true
1414
from sklearn.utils.testing import assert_less
1515
from sklearn.utils.testing import assert_raises
16+
from sklearn.utils.testing import assert_warns
1617

1718
from sklearn.datasets import make_classification
1819
from sklearn.datasets import make_multilabel_classification
@@ -131,11 +132,11 @@ def test_make_classification_informative_features():
131132
n_clusters_per_class=2)
132133

133134

134-
def test_make_multilabel_classification():
135+
def test_make_multilabel_classification_return_sequences():
135136
for allow_unlabeled, min_length in zip((True, False), (0, 1)):
136-
X, Y = make_multilabel_classification(n_samples=100, n_features=20,
137-
n_classes=3, random_state=0,
138-
allow_unlabeled=allow_unlabeled)
137+
X, Y = assert_warns(DeprecationWarning, make_multilabel_classification,
138+
n_samples=100, n_features=20, n_classes=3,
139+
random_state=0, allow_unlabeled=allow_unlabeled)
139140
assert_equal(X.shape, (100, 20), "X shape mismatch")
140141
if not allow_unlabeled:
141142
assert_equal(max([max(y) for y in Y]), 2)

sklearn/metrics/tests/test_metrics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ def test_multilabel_representation_invariance():
16821682
metric.__module__ = 'tmp'
16831683
metric.__name__ = name
16841684
metric = ignore_warnings(metric)
1685-
measure = metric(y1, y2)
1685+
measure = assert_warns(DeprecationWarning, metric, y1, y2)
16861686

16871687
# Check representation invariance
16881688
assert_almost_equal(metric(y1_binary_indicator,
@@ -1894,7 +1894,8 @@ def test_normalize_option_multilabel_classification():
18941894
metrics = ALL_METRICS[name]
18951895

18961896
# List of list of labels
1897-
measure = metrics(y_true, y_pred, normalize=True)
1897+
measure = assert_warns(DeprecationWarning, metrics, y_true, y_pred,
1898+
normalize=True)
18981899
assert_greater(measure, 0,
18991900
msg="We failed to test correctly the normalize option")
19001901
assert_almost_equal(metrics(y_true, y_pred, normalize=False)

sklearn/preprocessing/tests/test_label.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from sklearn.utils.testing import assert_raises
77
from sklearn.utils.testing import assert_true
88
from sklearn.utils.testing import assert_false
9+
from sklearn.utils.testing import assert_warns
10+
from sklearn.utils.testing import ignore_warnings
911

1012
from sklearn.preprocessing.label import LabelBinarizer
1113
from sklearn.preprocessing.label import MultiLabelBinarizer
@@ -51,6 +53,7 @@ def test_label_binarizer():
5153
assert_array_equal(lb.inverse_transform(got), inp)
5254

5355

56+
@ignore_warnings
5457
def test_label_binarizer_column_y():
5558
# first for binary classification vs multi-label with 1 possible class
5659
# lists are multi-label, array is multi-class :-/
@@ -125,7 +128,7 @@ def test_label_binarizer_multilabel():
125128
indicator_mat = np.array([[0, 1, 1],
126129
[1, 0, 0],
127130
[1, 1, 0]])
128-
got = lb.fit_transform(inp)
131+
got = assert_warns(DeprecationWarning, lb.fit_transform, inp)
129132
assert_true(lb.multilabel_)
130133
assert_array_equal(indicator_mat, got)
131134
assert_equal(lb.inverse_transform(got), inp)
@@ -142,7 +145,7 @@ def test_label_binarizer_multilabel():
142145
[1, 0],
143146
[0, 1],
144147
[1, 1]])
145-
got = lb.fit_transform(inp)
148+
got = assert_warns(DeprecationWarning, lb.fit_transform, inp)
146149
assert_true(lb.multilabel_)
147150
assert_array_equal(expected, got)
148151
assert_equal([set(x) for x in lb.inverse_transform(got)],
@@ -155,7 +158,7 @@ def test_label_binarizer_errors():
155158
lb = LabelBinarizer().fit(one_class)
156159
assert_false(lb.multilabel_)
157160

158-
multi_label = [(2, 3), (0,), (0, 2)]
161+
multi_label = np.array([[0, 0, 1, 0], [1, 0, 1, 0]])
159162
assert_raises(ValueError, lb.transform, multi_label)
160163

161164
lb = LabelBinarizer()
@@ -229,7 +232,8 @@ def test_label_binarizer_multilabel_unlabeled():
229232
Y = np.array([[1, 1],
230233
[1, 0],
231234
[0, 0]])
232-
assert_array_equal(lb.fit_transform(y), Y)
235+
assert_array_equal(assert_warns(DeprecationWarning,
236+
lb.fit_transform, y), Y)
233237

234238

235239
def test_label_binarize_with_multilabel_indicator():

sklearn/tests/test_multiclass.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sklearn.utils.testing import assert_true
88
from sklearn.utils.testing import assert_false
99
from sklearn.utils.testing import assert_raises
10+
from sklearn.utils.testing import assert_warns
1011

1112
from sklearn.utils.testing import assert_greater
1213
from sklearn.multiclass import OneVsRestClassifier
@@ -86,7 +87,9 @@ def test_ovr_multilabel():
8687
LinearRegression(), Ridge(),
8788
ElasticNet(), Lasso(alpha=0.5)):
8889
# test input as lists of tuples
89-
clf = OneVsRestClassifier(base_clf).fit(X, y)
90+
clf = assert_warns(DeprecationWarning,
91+
OneVsRestClassifier(base_clf).fit,
92+
X, y)
9093
assert_equal(set(clf.classes_), classes)
9194
y_pred = clf.predict([[0, 4, 4]])[0]
9295
assert_equal(set(y_pred), set(["spam", "eggs"]))
@@ -115,6 +118,7 @@ def test_ovr_multilabel_dataset():
115118
n_labels=2,
116119
length=50,
117120
allow_unlabeled=au,
121+
return_indicator=True,
118122
random_state=0)
119123
X_train, Y_train = X[:80], Y[:80]
120124
X_test, Y_test = X[80:], Y[80:]
@@ -138,6 +142,7 @@ def test_ovr_multilabel_predict_proba():
138142
n_labels=3,
139143
length=50,
140144
allow_unlabeled=au,
145+
return_indicator=True,
141146
random_state=0)
142147
X_train, Y_train = X[:80], Y[:80]
143148
X_test, Y_test = X[80:], Y[80:]
@@ -157,8 +162,8 @@ def test_ovr_multilabel_predict_proba():
157162

158163
# predict assigns a label if the probability that the
159164
# sample has the label is greater than 0.5.
160-
pred = [tuple(l.nonzero()[0]) for l in (Y_proba > 0.5)]
161-
assert_equal(pred, Y_pred)
165+
pred = Y_proba > .5
166+
assert_array_equal(pred, Y_pred)
162167

163168

164169
def test_ovr_single_label_predict_proba():
@@ -189,12 +194,13 @@ def test_ovr_multilabel_decision_function():
189194
n_labels=3,
190195
length=50,
191196
allow_unlabeled=True,
197+
return_indicator=True,
192198
random_state=0)
193199
X_train, Y_train = X[:80], Y[:80]
194200
X_test, Y_test = X[80:], Y[80:]
195201
clf = OneVsRestClassifier(svm.SVC()).fit(X_train, Y_train)
196-
assert_array_equal((clf.decision_function(X_test) > 0).nonzero()[1],
197-
np.hstack(clf.predict(X_test)))
202+
assert_array_equal((clf.decision_function(X_test) > 0).astype(int),
203+
clf.predict(X_test))
198204

199205

200206
def test_ovr_single_label_decision_function():

sklearn/utils/multiclass.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ def unique_labels(*ys):
6868
array([1, 2, 3, 4])
6969
>>> unique_labels([1, 2, 10], [5, 11])
7070
array([ 1, 2, 5, 10, 11])
71-
>>> unique_labels(np.array([[0.0, 1.0], [1.0, 1.0]]), np.zeros((2, 2)))
72-
array([0, 1])
73-
>>> unique_labels([(1, 2), (3,)], [(1, 2), tuple()])
74-
array([1, 2, 3])
75-
7671
"""
7772
if not ys:
7873
raise ValueError('No argument has been passed.')
@@ -152,6 +147,8 @@ def is_label_indicator_matrix(y):
152147
def is_sequence_of_sequences(y):
153148
""" Check if ``y`` is in the sequence of sequences format (multilabel).
154149
150+
This format is DEPRECATED.
151+
155152
Parameters
156153
----------
157154
y : sequence or array.
@@ -160,22 +157,6 @@ def is_sequence_of_sequences(y):
160157
-------
161158
out : bool,
162159
Return ``True``, if ``y`` is a sequence of sequences else ``False``.
163-
164-
>>> import numpy as np
165-
>>> is_sequence_of_sequences([0, 1, 0, 1])
166-
False
167-
>>> is_sequence_of_sequences([[1], [0, 2], []])
168-
True
169-
>>> is_sequence_of_sequences(np.array([[1], [0, 2], []], dtype=object))
170-
True
171-
>>> is_sequence_of_sequences([(1,), (0, 2), ()])
172-
True
173-
>>> is_sequence_of_sequences(np.array([[1, 0], [0, 0]]))
174-
False
175-
>>> is_sequence_of_sequences(np.array([[1], [0], [0]]))
176-
False
177-
>>> is_sequence_of_sequences(np.array([[1, 0, 0]]))
178-
False
179160
"""
180161
# the explicit check for ndarray is for forward compatibility; future
181162
# versions of Numpy might want to register ndarray as a Sequence
@@ -213,8 +194,6 @@ def is_multilabel(y):
213194
>>> from sklearn.utils.multiclass import is_multilabel
214195
>>> is_multilabel([0, 1, 0, 1])
215196
False
216-
>>> is_multilabel([[1], [0, 2], []])
217-
True
218197
>>> is_multilabel(np.array([[1, 0], [0, 0]]))
219198
True
220199
>>> is_multilabel(np.array([[1], [0], [0]]))
@@ -273,10 +252,6 @@ def type_of_target(y):
273252
'multiclass-multioutput'
274253
>>> type_of_target(np.array([[1.5, 2.0], [3.0, 1.6]]))
275254
'continuous-multioutput'
276-
>>> type_of_target([['a', 'b'], ['c'], []])
277-
'multilabel-sequences'
278-
>>> type_of_target([[]])
279-
'multilabel-sequences'
280255
>>> type_of_target(np.array([[0, 1], [1, 1]]))
281256
'multilabel-indicator'
282257
"""

sklearn/utils/tests/test_multiclass.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from itertools import product
3+
from functools import partial
34
from sklearn.externals.six.moves import xrange
45
from sklearn.externals.six import iteritems
56

@@ -8,6 +9,8 @@
89
from sklearn.utils.testing import assert_true
910
from sklearn.utils.testing import assert_false
1011
from sklearn.utils.testing import assert_raises
12+
from sklearn.utils.testing import assert_warns
13+
from sklearn.utils.testing import ignore_warnings
1114

1215
from sklearn.utils.multiclass import unique_labels
1316
from sklearn.utils.multiclass import is_label_indicator_matrix
@@ -132,10 +135,15 @@ def test_unique_labels():
132135
assert_array_equal(unique_labels([4, 0, 2]), np.array([0, 2, 4]))
133136

134137
# Multilabels
135-
assert_array_equal(unique_labels([(0, 1, 2), (0,), tuple(), (2, 1)]),
138+
assert_array_equal(assert_warns(DeprecationWarning,
139+
unique_labels,
140+
[(0, 1, 2), (0,), tuple(), (2, 1)]),
136141
np.arange(3))
137-
assert_array_equal(unique_labels([[0, 1, 2], [0], list(), [2, 1]]),
142+
assert_array_equal(assert_warns(DeprecationWarning,
143+
unique_labels,
144+
[[0, 1, 2], [0], list(), [2, 1]]),
138145
np.arange(3))
146+
139147
assert_array_equal(unique_labels(np.array([[0, 0, 1],
140148
[1, 0, 1],
141149
[0, 0, 0]])),
@@ -160,9 +168,16 @@ def test_unique_labels():
160168
# Some tests with strings input
161169
assert_array_equal(unique_labels(["a", "b", "c"], ["d"]),
162170
["a", "b", "c", "d"])
163-
assert_array_equal(unique_labels([["a", "b"], ["c"]], [["d"]]),
171+
172+
assert_array_equal(assert_warns(DeprecationWarning, unique_labels,
173+
[["a", "b"], ["c"]], [["d"]]),
164174
["a", "b", "c", "d"])
165175

176+
177+
@ignore_warnings
178+
def test_unique_labels_non_specific():
179+
"""Test unique_labels with a variety of collected examples"""
180+
166181
# Smoke test for all supported format
167182
for format in ["binary", "multiclass", "multilabel-sequences",
168183
"multilabel-indicator"]:
@@ -178,6 +193,9 @@ def test_unique_labels():
178193
for example in EXAMPLES[y_type]:
179194
assert_raises(ValueError, unique_labels, example)
180195

196+
197+
@ignore_warnings
198+
def test_unique_labels_mixed_types():
181199
#Mix of multilabel-indicator and multilabel-sequences
182200
mix_multilabel_format = product(EXAMPLES["multilabel-indicator"],
183201
EXAMPLES["multilabel-sequences"])
@@ -207,6 +225,7 @@ def test_unique_labels():
207225
["0", "2"])
208226

209227

228+
@ignore_warnings
210229
def test_is_multilabel():
211230
for group, group_examples in iteritems(EXAMPLES):
212231
if group.startswith('multilabel'):
@@ -234,14 +253,18 @@ def test_is_sequence_of_sequences():
234253
for group, group_examples in iteritems(EXAMPLES):
235254
if group == 'multilabel-sequences':
236255
assert_, exp = assert_true, 'True'
256+
check = partial(assert_warns, DeprecationWarning,
257+
is_sequence_of_sequences)
237258
else:
238259
assert_, exp = assert_false, 'False'
260+
check = is_sequence_of_sequences
239261
for example in group_examples:
240-
assert_(is_sequence_of_sequences(example),
262+
assert_(check(example),
241263
msg='is_sequence_of_sequences(%r) should be %s'
242264
% (example, exp))
243265

244266

267+
@ignore_warnings
245268
def test_type_of_target():
246269
for group, group_examples in iteritems(EXAMPLES):
247270
for example in group_examples:

0 commit comments

Comments
 (0)