Skip to content

Commit 80222b9

Browse files
committed
Only run doctests on numpy 1.14.
Fix doctests due to numpy 1.14 formatting changes.
1 parent c61a694 commit 80222b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+344
-328
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ matrix:
4141
# This environment tests the newest supported Anaconda release (5.0.0)
4242
# It also runs tests requiring Pandas and PyAMG
4343
- env: DISTRIB="conda" PYTHON_VERSION="3.6.2" INSTALL_MKL="true"
44-
NUMPY_VERSION="1.13.1" SCIPY_VERSION="0.19.1" PANDAS_VERSION="0.20.3"
44+
NUMPY_VERSION="1.14.2" SCIPY_VERSION="1.0.0" PANDAS_VERSION="0.20.3"
4545
CYTHON_VERSION="0.26.1" PYAMG_VERSION="3.3.2" PILLOW_VERSION="4.3.0"
4646
COVERAGE=true
4747
CHECK_PYTEST_SOFT_DEPENDENCY="true" TEST_DOCSTRINGS="true"

conftest.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
# This file is here so that when running from the root folder
2-
# ./sklearn is added to sys.path by pytest.
3-
# See https://docs.pytest.org/en/latest/pythonpath.html for more details.
4-
# For example, this allows to build extensions in place and run pytest
5-
# doc/modules/clustering.rst and use sklearn from the local folder
6-
# rather than the one from site-packages.
7-
8-
# Set numpy array str/repr to legacy behaviour on numpy > 1.13 to make
9-
# the doctests pass
10-
import numpy as np
11-
try:
12-
np.set_printoptions(legacy='1.13')
13-
except TypeError:
14-
pass
1+
# Even if empty this file is useful so that when running from the root folder
2+
# ./sklearn is added to sys.path by pytest. See
3+
# https://docs.pytest.org/en/latest/pythonpath.html for more details. For
4+
# example, this allows to build extensions in place and run pytest
5+
# doc/modules/clustering.rst and use sklearn from the local folder rather than
6+
# the one from site-packages.
7+
8+
from distutils.version import LooseVersion
9+
10+
import pytest
11+
from _pytest.doctest import DoctestItem
12+
13+
14+
def pytest_collection_modifyitems(config, items):
15+
# numpy changed the str/repr formatting of numpy arrays in 1.14. We want to
16+
# run doctests only for numpy >= 1.14.
17+
skip_doctests = True
18+
try:
19+
import numpy as np
20+
if LooseVersion(np.__version__) >= LooseVersion('1.14'):
21+
skip_doctests = False
22+
except ImportError:
23+
pass
24+
25+
if skip_doctests:
26+
skip_marker = pytest.mark.skip(
27+
reason='doctests are only run for numpy >= 1.14')
28+
29+
for item in items:
30+
if isinstance(item, DoctestItem):
31+
item.add_marker(skip_marker)

doc/datasets/mldata.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ of size 28x28 pixels, labeled from 0 to 9::
3434
>>> mnist.target.shape
3535
(70000,)
3636
>>> np.unique(mnist.target)
37-
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
37+
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
3838

3939
After the first download, the dataset is cached locally in the path
4040
specified by the ``data_home`` keyword argument, which defaults to

doc/developers/utilities.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ For example::
7171
>>> random_state = 0
7272
>>> random_state = check_random_state(random_state)
7373
>>> random_state.rand(4)
74-
array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318])
74+
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
7575

7676
When developing your own scikit-learn compatible estimator, the following
7777
helpers are available.

doc/modules/compose.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ object::
190190
steps=[('reduce_dim', PCA(...)), ('clf', SVC(...))])
191191
>>> # The pca instance can be inspected directly
192192
>>> print(pca1.components_) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
193-
[[ -1.77484909e-19 ... 4.07058917e-18]]
193+
[[-1.77484909e-19 ... 4.07058917e-18]]
194194

195195
Enabling caching triggers a clone of the transformers before fitting.
196196
Therefore, the transformer instance given to the pipeline cannot be
@@ -212,7 +212,7 @@ object::
212212
steps=[('reduce_dim', PCA(...)), ('clf', SVC(...))])
213213
>>> print(cached_pipe.named_steps['reduce_dim'].components_)
214214
... # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
215-
[[ -1.77484909e-19 ... 4.07058917e-18]]
215+
[[-1.77484909e-19 ... 4.07058917e-18]]
216216
>>> # Remove the cache directory
217217
>>> rmtree(cachedir)
218218

doc/modules/cross_validation.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ time)::
106106
>>> clf = svm.SVC(kernel='linear', C=1)
107107
>>> scores = cross_val_score(clf, iris.data, iris.target, cv=5)
108108
>>> scores # doctest: +ELLIPSIS
109-
array([ 0.96..., 1. ..., 0.96..., 0.96..., 1. ])
109+
array([0.96..., 1. ..., 0.96..., 0.96..., 1. ])
110110

111111
The mean score and the 95\% confidence interval of the score estimate are hence
112112
given by::
@@ -122,7 +122,7 @@ scoring parameter::
122122
>>> scores = cross_val_score(
123123
... clf, iris.data, iris.target, cv=5, scoring='f1_macro')
124124
>>> scores # doctest: +ELLIPSIS
125-
array([ 0.96..., 1. ..., 0.96..., 0.96..., 1. ])
125+
array([0.96..., 1. ..., 0.96..., 0.96..., 1. ])
126126

127127
See :ref:`scoring_parameter` for details.
128128
In the case of the Iris dataset, the samples are balanced across target
@@ -141,7 +141,7 @@ validation iterator instead, for instance::
141141
>>> cv = ShuffleSplit(n_splits=3, test_size=0.3, random_state=0)
142142
>>> cross_val_score(clf, iris.data, iris.target, cv=cv)
143143
... # doctest: +ELLIPSIS
144-
array([ 0.97..., 0.97..., 1. ])
144+
array([0.97..., 0.97..., 1. ])
145145

146146

147147
.. topic:: Data transformation with held out data
@@ -168,7 +168,7 @@ validation iterator instead, for instance::
168168
>>> clf = make_pipeline(preprocessing.StandardScaler(), svm.SVC(C=1))
169169
>>> cross_val_score(clf, iris.data, iris.target, cv=cv)
170170
... # doctest: +ELLIPSIS
171-
array([ 0.97..., 0.93..., 0.95...])
171+
array([0.97..., 0.93..., 0.95...])
172172

173173
See :ref:`combining_estimators`.
174174

@@ -212,7 +212,7 @@ predefined scorer names::
212212
>>> sorted(scores.keys())
213213
['fit_time', 'score_time', 'test_precision_macro', 'test_recall_macro']
214214
>>> scores['test_recall_macro'] # doctest: +ELLIPSIS
215-
array([ 0.96..., 1. ..., 0.96..., 0.96..., 1. ])
215+
array([0.96..., 1. ..., 0.96..., 0.96..., 1. ])
216216

217217
Or as a dict mapping scorer name to a predefined or custom scoring function::
218218

@@ -225,7 +225,7 @@ Or as a dict mapping scorer name to a predefined or custom scoring function::
225225
['fit_time', 'score_time', 'test_prec_macro', 'test_rec_micro',
226226
'train_prec_macro', 'train_rec_micro']
227227
>>> scores['train_rec_micro'] # doctest: +ELLIPSIS
228-
array([ 0.97..., 0.97..., 0.99..., 0.98..., 0.98...])
228+
array([0.97..., 0.97..., 0.99..., 0.98..., 0.98...])
229229

230230
Here is an example of ``cross_validate`` using a single metric::
231231

doc/modules/ensemble.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ accessed via the ``feature_importances_`` property::
782782
>>> clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
783783
... max_depth=1, random_state=0).fit(X, y)
784784
>>> clf.feature_importances_ # doctest: +ELLIPSIS
785-
array([ 0.11, 0.1 , 0.11, ...
785+
array([0.11, 0.1 , 0.11, ...
786786

787787
.. topic:: Examples:
788788

doc/modules/feature_extraction.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ is a traditional numerical feature::
4949
>>> vec = DictVectorizer()
5050

5151
>>> vec.fit_transform(measurements).toarray()
52-
array([[ 1., 0., 0., 33.],
53-
[ 0., 1., 0., 12.],
54-
[ 0., 0., 1., 18.]])
52+
array([[ 1., 0., 0., 33.],
53+
[ 0., 1., 0., 12.],
54+
[ 0., 0., 1., 18.]])
5555

5656
>>> vec.get_feature_names()
5757
['city=Dubai', 'city=London', 'city=San Francisco', 'temperature']
@@ -89,7 +89,7 @@ suitable for feeding into a classifier (maybe after being piped into a
8989
<1x6 sparse matrix of type '<... 'numpy.float64'>'
9090
with 6 stored elements in Compressed Sparse ... format>
9191
>>> pos_vectorized.toarray()
92-
array([[ 1., 1., 1., 1., 1., 1.]])
92+
array([[1., 1., 1., 1., 1., 1.]])
9393
>>> vec.get_feature_names()
9494
['pos+1=PP', 'pos-1=NN', 'pos-2=DT', 'word+1=on', 'word-1=cat', 'word-2=the']
9595

@@ -463,12 +463,12 @@ content of the documents::
463463
with 9 stored elements in Compressed Sparse ... format>
464464

465465
>>> tfidf.toarray() # doctest: +ELLIPSIS
466-
array([[ 0.81940995, 0. , 0.57320793],
467-
[ 1. , 0. , 0. ],
468-
[ 1. , 0. , 0. ],
469-
[ 1. , 0. , 0. ],
470-
[ 0.47330339, 0.88089948, 0. ],
471-
[ 0.58149261, 0. , 0.81355169]])
466+
array([[0.81940995, 0. , 0.57320793],
467+
[1. , 0. , 0. ],
468+
[1. , 0. , 0. ],
469+
[1. , 0. , 0. ],
470+
[0.47330339, 0.88089948, 0. ],
471+
[0.58149261, 0. , 0.81355169]])
472472

473473
Each row is normalized to have unit Euclidean norm:
474474

@@ -523,19 +523,19 @@ And the L2-normalized tf-idf changes to
523523

524524
>>> transformer = TfidfTransformer()
525525
>>> transformer.fit_transform(counts).toarray()
526-
array([[ 0.85151335, 0. , 0.52433293],
527-
[ 1. , 0. , 0. ],
528-
[ 1. , 0. , 0. ],
529-
[ 1. , 0. , 0. ],
530-
[ 0.55422893, 0.83236428, 0. ],
531-
[ 0.63035731, 0. , 0.77630514]])
526+
array([[0.85151335, 0. , 0.52433293],
527+
[1. , 0. , 0. ],
528+
[1. , 0. , 0. ],
529+
[1. , 0. , 0. ],
530+
[0.55422893, 0.83236428, 0. ],
531+
[0.63035731, 0. , 0.77630514]])
532532

533533
The weights of each
534534
feature computed by the ``fit`` method call are stored in a model
535535
attribute::
536536

537537
>>> transformer.idf_ # doctest: +ELLIPSIS
538-
array([ 1. ..., 2.25..., 1.84...])
538+
array([1. ..., 2.25..., 1.84...])
539539

540540

541541

doc/modules/gaussian_process.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ kernel but with the hyperparameters set to ``theta``. An illustrative example:
413413
>>> from sklearn.gaussian_process.kernels import ConstantKernel, RBF
414414
>>> kernel = ConstantKernel(constant_value=1.0, constant_value_bounds=(0.0, 10.0)) * RBF(length_scale=0.5, length_scale_bounds=(0.0, 10.0)) + RBF(length_scale=2.0, length_scale_bounds=(0.0, 10.0))
415415
>>> for hyperparameter in kernel.hyperparameters: print(hyperparameter)
416-
Hyperparameter(name='k1__k1__constant_value', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False)
417-
Hyperparameter(name='k1__k2__length_scale', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False)
418-
Hyperparameter(name='k2__length_scale', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False)
416+
Hyperparameter(name='k1__k1__constant_value', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False)
417+
Hyperparameter(name='k1__k2__length_scale', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False)
418+
Hyperparameter(name='k2__length_scale', value_type='numeric', bounds=array([[ 0., 10.]]), n_elements=1, fixed=False)
419419
>>> params = kernel.get_params()
420420
>>> for key in sorted(params): print("%s : %s" % (key, params[key]))
421421
k1 : 1**2 * RBF(length_scale=0.5)
@@ -431,9 +431,9 @@ kernel but with the hyperparameters set to ``theta``. An illustrative example:
431431
>>> print(kernel.theta) # Note: log-transformed
432432
[ 0. -0.69314718 0.69314718]
433433
>>> print(kernel.bounds) # Note: log-transformed
434-
[[ -inf 2.30258509]
435-
[ -inf 2.30258509]
436-
[ -inf 2.30258509]]
434+
[[ -inf 2.30258509]
435+
[ -inf 2.30258509]
436+
[ -inf 2.30258509]]
437437

438438

439439
All Gaussian process kernels are interoperable with :mod:`sklearn.metrics.pairwise`

doc/modules/impute.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ that contain the missing values::
3131
SimpleImputer(axis=0, copy=True, missing_values='NaN', strategy='mean', verbose=0)
3232
>>> X = [[np.nan, 2], [6, np.nan], [7, 6]]
3333
>>> print(imp.transform(X)) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
34-
[[ 4. 2. ]
35-
[ 6. 3.666...]
36-
[ 7. 6. ]]
34+
[[4. 2. ]
35+
[6. 3.666...]
36+
[7. 6. ]]
3737

3838
The :class:`SimpleImputer` class also supports sparse matrices::
3939

@@ -44,13 +44,13 @@ The :class:`SimpleImputer` class also supports sparse matrices::
4444
SimpleImputer(axis=0, copy=True, missing_values=0, strategy='mean', verbose=0)
4545
>>> X_test = sp.csc_matrix([[0, 2], [6, 0], [7, 6]])
4646
>>> print(imp.transform(X_test)) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
47-
[[ 4. 2. ]
48-
[ 6. 3.666...]
49-
[ 7. 6. ]]
47+
[[4. 2. ]
48+
[6. 3.666...]
49+
[7. 6. ]]
5050

5151
Note that, here, missing values are encoded by 0 and are thus implicitly stored
5252
in the matrix. This format is thus suitable when there are many more missing
5353
values than observed values.
5454

5555
:class:`SimpleImputer` can be used in a Pipeline as a way to build a composite
56-
estimator that supports imputation. See :ref:`sphx_glr_auto_examples_plot_missing_values.py`.
56+
estimator that supports imputation. See :ref:`sphx_glr_auto_examples_plot_missing_values.py`.

0 commit comments

Comments
 (0)