8000 Merge pull request #5413 from amueller/cleanup_tests · scikit-learn/scikit-learn@3301893 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3301893

Browse files
committed
Merge pull request #5413 from amueller/cleanup_tests
Cleanup tests
2 parents 2d97609 + c66689c commit 3301893

File tree

8 files changed

+48
-42
lines changed

8 files changed

+48
-42
lines changed

sklearn/cluster/k_means_.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from ..utils.random import choice
3232
from ..externals.joblib import Parallel
3333
from ..externals.joblib import delayed
34+
from ..externals.six import string_types
3435

3536
from . import _k_means
3637

@@ -269,7 +270,7 @@ def k_means(X, n_clusters, init='k-means++', precompute_distances='auto',
269270

270271
if max_iter <= 0:
271272
raise ValueError('Number of iterations should be a positive number,'
272-
' got %d instead' % max_iter)
273+
' got %d instead' % max_iter)
273274

274275
best_inertia = np.infty
275276
X = as_float_array(X, copy=copy_x)
@@ -634,10 +635,10 @@ def _init_centroids(X, k, init, random_state=None, x_squared_norms=None,
634635
raise ValueError(
635636
"n_samples=%d should be larger than k=%d" % (n_samples, k))
636637

637-
if init == 'k-means++':
638+
if isinstance(init, string_types) and init == 'k-means++':
638639
centers = _k_init(X, k, random_state=random_state,
639640
x_squared_norms=x_squared_norms)
640-
elif init == 'random':
641+
elif isinstance(init, string_types) and init == 'random':
641642
seeds = random_state.permutation(n_samples)[:k]
642643
centers = X[seeds]
643644
elif hasattr(init, '__array__'):

sklearn/linear_model/base.py

Lines changed: 7 additions & 7 deletions
-
### should be squashed into its respective objects.
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
from ..utils.seq_dataset import ArrayDataset, CSRDataset
3535

3636

37-
###
38-
### TODO: intercept for all models
39-
### We should define a common function to center data instead of
40-
### repeating the same code inside each fit method.
37+
#
38+
# TODO: intercept for all models
39+
# We should define a common function to center data instead of
40+
# repeating the same code inside each fit method.
4141

42-
### TODO: bayesian_ridge_regression and bayesian_regression_ard
43
42+
# TODO: bayesian_ridge_regression and bayesian_regression_ard
43+
# should be squashed into its respective objects.
4444

4545
SPARSE_INTERCEPT_DECAY = 0.01
4646
# For sparse data intercept updates are scaled by this decay factor to avoid
@@ -474,7 +474,7 @@ def _pre_fit(X, y, Xy, precompute, normalize, fit_intercept, copy):
474474
Xy = None
475475

476476
# precompute if n_samples > n_features
477-
if precompute == 'auto':
477+
if isinstance(precompute, six.string_types) and precompute == 'auto':
478478
precompute = (n_samples > n_features)
479479

480480
if precompute is True:

sklearn/linear_model/coordinate_descent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ def fit(self, X, y, check_input=True):
644644
"well. You are advised to use the LinearRegression "
645645
"estimator", stacklevel=2)
646646

647-
if self.precompute == 'auto':
647+
if (isinstance(self.precompute, six.string_types)
648+
and self.precompute == 'auto'):
648649
warnings.warn("Setting precompute to 'auto', was found to be "
649650
"slower even when n_samples > n_features. Hence "
650651
"it will be removed in 0.18.",

sklearn/linear_model/least_angle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from ..utils import ConvergenceWarning
2727
from ..externals.joblib import Parallel, delayed
2828
from ..externals.six.moves import xrange
29+
from ..externals.six import string_types
2930

30 F438 31
import scipy
3132
solve_triangular_args = {}
@@ -179,7 +180,7 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
179180
# speeds up the calculation of the (partial) Gram matrix
180181
# and allows to easily swap columns
181182
X = X.copy('F')
182-
elif Gram == 'auto':
183+
elif isinstance(Gram, string_types) and Gram == 'auto':
183184
Gram = None
184185
if X.shape[0] > X.shape[1]:
185186
Gram = np.dot(X.T, X)

sklearn/metrics/regression.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from ..utils.validation import check_array, check_consistent_length
2828
from ..utils.validation import column_or_1d
29+
from ..externals.six import string_types
2930

3031
import warnings
3132

@@ -162,11 +163,12 @@ def mean_absolute_error(y_true, y_pred,
162163
y_true, y_pred, multioutput)
163164
output_errors = np.average(np.abs(y_pred - y_true),
164165
weights=sample_weight, axis=0)
165-
if multioutput == 'raw_values':
166-
return output_errors
167-
elif multioutput == 'uniform_average':
168-
# pass None as weights to np.average: uniform mean
169-
multioutput = None
166+
if isinstance(multioutput, string_types):
167+
if multioutput == 'raw_values':
168+
return output_errors
169+
elif multioutput == 'uniform_average':
170+
# pass None as weights to np.average: uniform mean
171+
multioutput = None
170172

171173
return np.average(output_errors, weights=multioutput)
172174

@@ -229,11 +231,12 @@ def mean_squared_error(y_true, y_pred,
229231
y_true, y_pred, multioutput)
230232
output_errors = np.average((y_true - y_pred) ** 2, axis=0,
231233
weights=sample_weight)
232-
if multioutput == 'raw_values':
233-
return output_errors
234-
elif multioutput == 'uniform_average':
235-
# pass None as weights to np.average: uniform mean
236-
multioutput = None
234+
if isinstance(multioutput, string_types):
235+
if multioutput == 'raw_values':
236+
return output_errors
237+
elif multioutput == 'uniform_average':
238+
# pass None as weights to np.average: uniform mean
239+
multioutput = None
237240

238241
return np.average(output_errors, weights=multioutput)
239242

@@ -464,20 +467,21 @@ def r2_score(y_true, y_pred,
464467
"to 'uniform_average' in 0.18.",
465468
DeprecationWarning)
466469
multioutput = 'variance_weighted'
467-
if multioutput == 'raw_values':
468-
# return scores individually
469-
return output_scores
470-
elif multioutput == 'uniform_average':
471-
# passing None as weights results is uniform mean
472-
avg_weights = None
473-
elif multioutput == 'variance_weighted':
474-
avg_weights = denominator
475-
# avoid fail on constant y or one-element arrays
476-
if not np.any(nonzero_denominator):
477-
if not np.any(nonzero_numerator):
478-
return 1.0
479-
else:
480-
return 0.0
470+
if isinstance(multioutput, string_types):
471+
if multioutput == 'raw_values':
472+
# return scores individually
473+
return output_scores
474+
elif multioutput == 'uniform_average':
475+
# passing None as weights results is uniform mean
476+
avg_weights = None
477+
elif multioutput == 'variance_weighted':
478+
avg_weights = denominator
479+
# avoid fail on constant y or one-element arrays
480+
if not np.any(nonzero_denominator):
481+
if not np.any(nonzero_numerator):
482+
return 1.0
483+
else:
484+
return 0.0
481485
else:
482486
avg_weights = multioutput
483487

sklearn/mixture/tests/test_gmm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def test_sample_gaussian():
5454
from sklearn.mixture import sample_gaussian
5555
x = sample_gaussian([0, 0], [[4, 3], [1, .1]],
5656
covariance_type='full', random_state=42)
57-
print(x)
5857
assert_true(np.isfinite(x).all())
5958

6059

sklearn/preprocessing/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ def _transform_selected(X, transform, selected="all", copy=True):
15531553
-------
15541554
X : array or sparse matrix, shape=(n_samples, n_features_new)
15551555
"""
1556-
if selected == "all":
1556+
if isinstance(selected, six.string_types) and selected == "all":
15571557
return transform(X)
15581558

15591559
X = check_array(X, accept_sparse='csc', copy=copy, dtype=FLOAT_DTYPES)

sklearn/utils/estimator_checks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
3939
from sklearn.random_projection import BaseRandomProjection
4040
from sklearn.feature_selection import SelectKBest
41-
from sklearn.svm.base import BaseLibSVM, BaseSVC
41+
from sklearn.svm.base import BaseLibSVM
4242
from sklearn.pipeline import make_pipeline
4343
from sklearn.decomposition import NMF, ProjectedGradientNMF
4444
from sklearn.utils.validation import DataConversionWarning
@@ -479,7 +479,7 @@ def check_fit1d_1sample(name, Estimator):
479479

480480
try:
481481
estimator.fit(X, y)
482-
except ValueError :
482+
except ValueError:
483483
pass
484484

485485

@@ -1158,7 +1158,6 @@ def check_regressors_train(name, Regressor):
11581158
# and furthermore assumes the presence of outliers, hence
11591159
# skipped
11601160
if name not in ('PLSCanonical', 'CCA', 'RANSACRegressor'):
1161-
print(regressor)
11621161
assert_greater(regressor.score(X, y_), 0.5)
11631162

11641163

@@ -1501,11 +1500,12 @@ def fit(self, X, y):
15011500
assert_true(all(item in deep_params.items() for item in
15021501
shallow_params.items()))
15031502

1503+
15041504
def check_classifiers_regression_target(name, Estimator):
15051505
# Check if classifier throws an exception when fed regression targets
15061506

15071507
boston = load_boston()
15081508
X, y = boston.data, boston.target
15091509
e = Estimator()
15101510
msg = 'Unknown label type: '
1511-
assert_raises_regex(ValueError, msg, e.fit, X, y)
1511+
assert_raises_regex(ValueError, msg, e.fit, X, y)

0 commit comments

Comments
 (0)
0