Skip to content

Commit c01ed96

Browse files
justinvflarsmans
authored andcommitted
PY3 xrange, np.divide, string.uppsercase, None comparison
Fixes scikit-learn#2108.
1 parent fc7aeee commit c01ed96

File tree

7 files changed

+31
-19
lines changed

7 files changed

+31
-19
lines changed

sklearn/decomposition/fastica_.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from ..base import BaseEstimator, TransformerMixin
1616
from ..externals import six
17+
from ..externals.six import moves
1718
from ..utils import array2d, as_float_array, check_random_state
1819

1920
__all__ = ['fastica', 'FastICA']
@@ -63,7 +64,7 @@ def _ica_def(X, tol, g, fun_args, max_iter, w_init):
6364
w = w_init[j, :].copy()
6465
w /= np.sqrt((w ** 2).sum())
6566

66-
for _ in xrange(max_iter):
67+
for _ in moves.xrange(max_iter):
6768
wtx = np.dot(w.T, X)
6869
gwtx, g_wtx = g(wtx, fun_args)
6970

@@ -93,7 +94,7 @@ def _ica_par(X, tol, g, fun_args, max_iter, w_init):
9394

9495
W = _sym_decorrelation(w_init)
9596

96-
for _ in xrange(max_iter):
97+
for _ in moves.xrange(max_iter):
9798
wtx = np.dot(W, X)
9899
gwtx, g_wtx = g(wtx, fun_args)
99100

sklearn/decomposition/tests/test_fastica.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from sklearn.decomposition import FastICA, fastica, PCA
1717
from sklearn.decomposition.fastica_ import _gs_decorrelation
18+
from sklearn.externals.six import moves
1819

1920

2021
def center_and_norm(x, axis=-1):
@@ -124,7 +125,7 @@ def g_test(x):
124125
ica = FastICA(fun=fn, algorithm=algo, random_state=0)
125126
assert_raises(ValueError, ica.fit, m.T)
126127

127-
assert_raises(TypeError, FastICA(fun=xrange(10)).fit, m.T)
128+
assert_raises(TypeError, FastICA(fun=moves.xrange(10)).fit, m.T)
128129

129130

130131
def test_fastica_nowhiten():

sklearn/feature_extraction/text.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,12 @@ def __init__(self, input='content', charset='utf-8',
594594
if max_df < 0 or min_df < 0:
595595
raise ValueError("negative value for max_df of min_df")
596596
self.max_features = max_features
597-
if not any((
598-
isinstance(max_features, numbers.Integral),
599-
max_features is None,
600-
max_features > 0)):
601-
raise ValueError(
602-
"max_features is neither a positive integer nor None")
597+
if max_features is not None:
598+
if (not isinstance(max_features, numbers.Integral) or
599+
max_features <= 0):
600+
raise ValueError(
601+
"max_features=%r, neither a positive integer nor None"
602+
% max_features)
603603
self.ngram_range = ngram_range
604604
if vocabulary is not None:
605605
if not isinstance(vocabulary, Mapping):

sklearn/feature_selection/tests/test_base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import string
2-
31
import numpy as np
42
from scipy import sparse as sp
53

@@ -34,7 +32,7 @@ def _get_support_mask(self):
3432
Xinv = X.copy()
3533
Xinv[:, 1::2] = 0
3634
y = [0, 1]
37-
feature_names = list(string.uppercase[:10])
35+
feature_names = list('ABCDEFGHIJ')
3836
feature_names_t = feature_names[::2]
3937
feature_names_inv = np.array(feature_names)
4038
feature_names_inv[1::2] = ''

sklearn/metrics/tests/test_metrics.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ def test(y_true, y_pred):
519519
assert_array_almost_equal(mcc, 0.57, decimal=2)
520520

521521
test(y_true, y_pred)
522-
test(map(str, y_true), map(str, y_pred))
522+
test([str(y) for y in y_true],
523+
[str(y) for y in y_pred])
523524

524525

525526
def test_matthews_corrcoef_nan():
@@ -639,7 +640,9 @@ def test(y_true, y_pred, string_type=False):
639640
[4, 24, 3]])
640641

641642
test(y_true, y_pred)
642-
test(map(str, y_true), map(str, y_pred), string_type=True)
643+
test(list(str(y) for y in y_true),
644+
list(str(y) for y in y_pred),
645+
string_type=True)
643646

644647

645648
def test_confusion_matrix_multiclass_subset_labels():
@@ -820,7 +823,7 @@ def test_losses():
820823
assert_equal(accuracy_score(y_true, y_pred),
821824
1 - zero_one_loss(y_true, y_pred))
822825

823-
with warnings.catch_warnings(True):
826+
with warnings.catch_warnings(record=True):
824827
# Throw deprecated warning
825828
assert_equal(zero_one_score(y_true, y_pred),
826829
1 - zero_one_loss(y_true, y_pred))
@@ -1001,7 +1004,7 @@ def test_hinge_loss_binary():
10011004
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
10021005
assert_equal(hinge_loss(y_true, pred_decision), 1.2 / 4)
10031006

1004-
with warnings.catch_warnings(True):
1007+
with warnings.catch_warnings(record=True):
10051008
# Test deprecated pos_label
10061009
assert_equal(
10071010
hinge_loss(-y_true, pred_decision),
@@ -1011,7 +1014,7 @@ def test_hinge_loss_binary():
10111014
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
10121015

10131016
assert_equal(hinge_loss(y_true, pred_decision), 1.2 / 4)
1014-
with warnings.catch_warnings(True):
1017+
with warnings.catch_warnings(record=True):
10151018
# Test deprecated pos_label
10161019
assert_equal(hinge_loss(y_true, pred_decision, pos_label=2,
10171020
neg_label=0), 1.2 / 4)

sklearn/utils/fixes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,13 @@ def safe_copy(X):
213213
safe_copy = np.copy
214214

215215
try:
216-
np.divide(1, 1, dtype=np.float)
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: '
220+
'https://github.com/numpy/numpy/issues/3484')
217221
divide = np.divide
222+
218223
except TypeError:
219224
# Compat for old versions of np.divide that do not provide support for
220225
# the dtype args

sklearn/utils/tests/test_fixes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from nose.tools import assert_equal
88
from numpy.testing import assert_array_equal
99

10-
from ..fixes import _in1d, _copysign
10+
from ..fixes import _in1d, _copysign, divide
1111

1212

1313
def test_in1d():
@@ -16,6 +16,10 @@ def test_in1d():
1616
assert_equal(_in1d(a, b).sum(), 5)
1717

1818

19+
def test_divide():
20+
assert_equal(divide(.6, 1), .600000000000)
21+
22+
1923
def test_copysign():
2024
a = np.array([-1, 1, -1])
2125
b = np.array([1, -1, 1])

0 commit comments

Comments
 (0)