Skip to content

Commit 8b44fa1

Browse files
committed
Merge pull request scikit-learn#4824 from amueller/testing_less_warnings
minor fixes to the tests, don't raise as many warnings in the test suite
2 parents bdd3cb7 + f0f174b commit 8b44fa1

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

sklearn/ensemble/tests/test_forest.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from sklearn.utils.testing import assert_greater_equal
2525
from sklearn.utils.testing import assert_raises
2626
from sklearn.utils.testing import assert_warns
27-
from sklearn.utils.testing import assert_warns_message
2827
from sklearn.utils.testing import ignore_warnings
2928

3029
from sklearn import datasets
@@ -415,10 +414,8 @@ def test_classes_shape():
415414

416415

417416
def test_random_trees_dense_type():
418-
'''
419-
Test that the `sparse_output` parameter of RandomTreesEmbedding
420-
works by returning a dense array.
421-
'''
417+
# Test that the `sparse_output` parameter of RandomTreesEmbedding
418+
# works by returning a dense array.
422419

423420
# Create the RTE with sparse=False
424421
hasher = RandomTreesEmbedding(n_estimators=10, sparse_output=False)
@@ -430,11 +427,8 @@ def test_random_trees_dense_type():
430427

431428

432429
def test_random_trees_dense_equal():
433-
'''
434-
Test that the `sparse_output` parameter of RandomTreesEmbedding
435-
works by returning the same array for both argument
436-
values.
437-
'''
430+
# Test that the `sparse_output` parameter of RandomTreesEmbedding
431+
# works by returning the same array for both argument values.
438432

439433
# Create the RTEs
440434
hasher_dense = RandomTreesEmbedding(n_estimators=10, sparse_output=False,
@@ -807,8 +801,7 @@ def check_class_weight_balanced_and_bootstrap_multi_output(name):
807801
clf = ForestClassifier(class_weight='balanced_subsample', random_state=0)
808802
clf.fit(X, _y)
809803
clf = ForestClassifier(class_weight='subsample', random_state=0)
810-
#assert_warns_message(DeprecationWarning, "balanced_subsample", clf.fit, X, _y)
811-
clf.fit(X, _y)
804+
ignore_warnings(clf.fit)(X, _y)
812805

813806

814807
def test_class_weight_balanced_and_bootstrap_multi_output():

sklearn/metrics/cluster/tests/test_supervised.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_non_consicutive_labels():
109109

110110
def uniform_labelings_scores(score_func, n_samples, k_range, n_runs=10,
111111
seed=42):
112-
"""Compute score for random uniform cluster labelings"""
112+
# Compute score for random uniform cluster labelings
113113
random_labels = np.random.RandomState(seed).random_integers
114114
scores = np.zeros((len(k_range), n_runs))
115115
for i, k in enumerate(k_range):
@@ -121,7 +121,7 @@ def uniform_labelings_scores(score_func, n_samples, k_range, n_runs=10,
121121

122122

123123
def test_adjustment_for_chance():
124-
"""Check that adjusted scores are almost zero on random labels"""
124+
# Check that adjusted scores are almost zero on random labels
125125
n_clusters_range = [2, 10, 50, 90]
126126
n_samples = 100
127127
n_runs = 10
@@ -134,7 +134,7 @@ def test_adjustment_for_chance():
134134

135135

136136
def test_adjusted_mutual_info_score():
137-
"""Compute the Adjusted Mutual Information and test against known values"""
137+
# Compute the Adjusted Mutual Information and test against known values
138138
labels_a = np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3])
139139
labels_b = np.array([1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 3, 1, 3, 3, 3, 2, 2])
140140
# Mutual information
@@ -177,7 +177,7 @@ def test_contingency_matrix():
177177

178178

179179
def test_exactly_zero_info_score():
180-
"""Check numerical stability when information is exactly zero"""
180+
# Check numerical stability when information is exactly zero
181181
for i in np.logspace(1, 4, 4).astype(np.int):
182182
labels_a, labels_b = np.ones(i, dtype=np.int),\
183183
np.arange(i, dtype=np.int)
@@ -188,7 +188,7 @@ def test_exactly_zero_info_score():
188188

189189

190190
def test_v_measure_and_mutual_information(seed=36):
191-
"""Check relation between v_measure, entropy and mutual information"""
191+
# Check relation between v_measure, entropy and mutual information
192192
for i in np.logspace(1, 4, 4).astype(np.int):
193193
random_state = np.random.RandomState(seed)
194194
labels_a, labels_b = random_state.random_integers(0, 10, i),\

sklearn/metrics/cluster/tests/test_unsupervised.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def test_silhouette():
12-
"""Tests the Silhouette Coefficient. """
12+
# Tests the Silhouette Coefficient.
1313
dataset = datasets.load_iris()
1414
X = dataset.data
1515
y = dataset.target
@@ -39,10 +39,8 @@ def test_silhouette():
3939

4040

4141
def test_no_nan():
42-
"""Assert Silhouette Coefficient != nan when there is 1 sample in a class.
43-
44-
This tests for the condition that caused issue 960.
45-
"""
42+
# Assert Silhouette Coefficient != nan when there is 1 sample in a class.
43+
# This tests for the condition that caused issue 960.
4644
# Note that there is only one sample in cluster 0. This used to cause the
4745
# silhouette_score to return nan (see bug #960).
4846
labels = np.array([1, 0, 1, 1, 1])
@@ -53,7 +51,7 @@ def test_no_nan():
5351

5452

5553
def test_correct_labelsize():
56-
"""Assert 1 < n_labels < n_samples"""
54+
# Assert 1 < n_labels < n_samples
5755
dataset = datasets.load_iris()
5856
X = dataset.data
5957

sklearn/metrics/cluster/unsupervised.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ def silhouette_samples(X, labels, metric='euclidean', **kwds):
161161
B = np.array([_nearest_cluster_distance(distances[i], labels, i)
162162
for i in range(n)])
163163
sil_samples = (B - A) / np.maximum(A, B)
164-
# nan values are for clusters of size 1, and should be 0
165-
return np.nan_to_num(sil_samples)
164+
return sil_samples
166165

167166

168167
def _intra_cluster_distance(distances_row, labels, i):
@@ -187,6 +186,9 @@ def _intra_cluster_distance(distances_row, labels, i):
187186
"""
188187
mask = labels == labels[i]
189188
mask[i] = False
189+
if not np.any(mask):
190+
# cluster of size 1
191+
return 0
190192
a = np.mean(distances_row[mask])
191193
return a
192194

sklearn/svm/tests/test_svm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from sklearn.utils.testing import assert_greater, assert_in, assert_less
2424
from sklearn.utils.testing import assert_raises_regexp, assert_warns
2525
from sklearn.utils.testing import assert_warns_message, assert_raise_message
26+
from sklearn.utils.testing import ignore_warnings
2627

2728
# toy sample
2829
X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]
@@ -793,6 +794,8 @@ def test_unfitted():
793794
clf.predict, X)
794795

795796

797+
# ignore convergence warnings from max_iter=1
798+
@ignore_warnings
796799
def test_consistent_proba():
797800
a = svm.SVC(probability=True, max_iter=1, random_state=0)
798801
proba_1 = a.fit(X, Y).predict_proba(X)

sklearn/utils/estimator_checks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ def _yield_clustering_checks(name, Clusterer):
147147

148148

149149
def _yield_all_checks(name, Estimator):
150-
#yield check_parameters_default_constructible, name, Estimator
151150
for check in _yield_non_meta_checks(name, Estimator):
152151
yield check
153152
if issubclass(Estimator, ClassifierMixin):
@@ -795,6 +794,7 @@ def check_estimators_fit_returns_self(name, Estimator):
795794
assert_true(estimator.fit(X, y) is estimator)
796795

797796

797+
@ignore_warnings
798798
def check_estimators_unfitted(name, Estimator):
799799
"""Check that predict raises an exception in an unfitted estimator.
800800

0 commit comments

Comments
 (0)