8000 TST + PY3 various fixes · scikit-learn/scikit-learn@d420aaf · GitHub
[go: up one dir, main page]

Skip to content

Commit d420aaf

Browse files
justinvflarsmans
authored andcommitted
TST + PY3 various fixes
* Little hack in fixes.py that can be removed when NumPy's numpy/numpy#3484 is fixed. * Little cleanup to the fixes implementation. * Change the numpy workaround and added a test. Fixed some xrange usage. * Fix the weight boosting test. * None is not comparable in python3 * Fix a test that depended on dictionary iteration order. * Python3 removed execfile * More test fixes. Fixes #2122.
1 parent 163a68e commit d420aaf

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

sklearn/datasets/tests/test_svmlight_format.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
import tempfile
88

9+
from sklearn.externals.six import b
910
from sklearn.utils.testing import assert_equal
1011
from sklearn.utils.testing import assert_array_equal
1112
from sklearn.utils.testing import assert_array_almost_equal
@@ -259,11 +260,11 @@ def test_dump_concise():
259260
dump_svmlight_file(X, y, f)
260261
f.seek(0)
261262
# make sure it's using the most concise format possible
262-
assert_equal(f.readline(), "1 0:1 1:2.1 2:3.01 3:1.000000000000001 4:1\n")
263-
assert_equal(f.readline(), "2.1 0:1000000000 1:2e+18 2:3e+27\n")
264-
assert_equal(f.readline(), "3.01 \n")
265-
assert_equal(f.readline(), "1.000000000000001 \n")
266-
assert_equal(f.readline(), "1 \n")
263+
assert_equal(f.readline(), b("1 0:1 1:2.1 2:3.01 3:1.000000000000001 4:1\n"))
264+
assert_equal(f.readline(), b("2.1 0:1000000000 1:2e+18 2:3e+27\n"))
265+
assert_equal(f.readline(), b("3.01 \n"))
266+
assert_equal(f.readline(), b("1.000000000000001 \n"))
267+
assert_equal(f.readline(), b("1 \n"))
267268
f.seek(0)
268269
# make sure it's correct too :)
269270
X2, y2 = load_svmlight_file(f)

sklearn/decomposition/pca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def fit(self, X, y=None, homoscedastic=True):
327327
Xr = X - self.mean_
328328
Xr -= np.dot(np.dot(Xr, self.components_.T), self.components_)
329329
n_samples = X.shape[0]
330-
if n_features <= self.n_components:
330+
if self.n_components is None or n_features <= self.n_components:
331331
delta = np.zeros(n_features)
332332
elif homoscedastic:
333333
delta = ((Xr ** 2).sum() * np.ones(n_features)

sklearn/ensemble/tests/test_weight_boosting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_classification_toy():
4545
clf = AdaBoostClassifier(algorithm=alg)
4646
clf.fit(X, y_class)
4747
assert_array_equal(clf.predict(T), y_t_class)
48-
assert_array_equal(np.unique(y_t_class), clf.classes_)
48+
assert_array_equal(np.unique(np.asarray(y_t_class)), clf.classes_)
4949
assert_equal(clf.predict_proba(T).shape, (len(T), 2))
5050
assert_equal(clf.decision_function(T).shape, (len(T),))
5151

sklearn/tests/test_common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
from scipy import sparse
2020

21+
from sklearn.externals.six import PY3
2122
from sklearn.utils.testing import assert_raises
2223
from sklearn.utils.testing import assert_equal
2324
from sklearn.utils.testing import assert_true
@@ -110,7 +111,6 @@ def test_all_estimators():
110111
else:
111112
assert_equal(params[arg], default)
112113

113-
114114
def test_estimators_sparse_data():
115115
# All estimators should either deal with sparse data, or raise an
116116
# intelligible error message
@@ -830,7 +830,10 @@ def test_configure():
830830
# The configuration spits out warnings when not finding
831831
# Blas/Atlas development headers
832832
warnings.simplefilter('ignore', UserWarning)
833-
execfile('setup.py', dict(__name__='__main__'))
833+
if PY3:
834+
exec(open('setup.py').read(), dict(__name__='__main__'))
835+
else:
836+
execfile('setup.py', dict(__name__='__main__'))
834837
finally:
835838
sys.argv = old_argv
836839
os.chdir(cwd)

sklearn/tests/test_grid_search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ def test_parameter_grid():
125125
# loop to assert we can iterate over the grid multiple times
126126
for i in xrange(2):
127127
# tuple + chain transforms {"a": 1, "b": 2} to ("a", 1, "b", 2)
128-
points = set(tuple(chain(*p.items())) for p in grid2)
128+
points = set(tuple(chain(*(sorted(p.items())))) for p in grid2)
129129
assert_equal(points,
130-
set(("foo", x, "bar", y)
131-
for x, y in product(params2["foo"], params2["bar"])))
130+
set(("bar", x, "foo", y)
131+
for x, y in product(params2["bar"], params2["foo"])))
132132

133133
# Special case: empty grid (useful to get default estimator settings)
134134
empty = ParameterGrid({})

sklearn/utils/fixes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,9 @@ def safe_copy(X):
213213
safe_copy = np.copy
214214

215215
try:
216-
if not np.allclose(np.divide(.4, 1),
217-
np.divide(.4, 1, dtype=np.float),
218-
.4):
219-
raise TypeError('Divide not working with dtype kwarg: '
216+
if (not np.allclose(np.divide(.4, 1), np.divide(.4, 1, dtype=np.float))
217+
or not np.allclose(np.divide(.4, 1), .4)):
218+
raise TypeError('Divide not working with dtype: '
220219
'https://github.com/numpy/numpy/issues/3484')
221220
divide = np.divide
222221

0 commit comments

Comments
 (0)
0