Skip to content

Commit e2a42a8

Browse files
EmilyXinyiogrisel
andauthored
update the doctests to be compatible with numpy>=2 (scikit-learn#29613)
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
1 parent 3a11129 commit e2a42a8

28 files changed

+147
-143
lines changed

sklearn/cluster/_mean_shift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def estimate_bandwidth(X, *, quantile=0.3, n_samples=None, random_state=0, n_job
8282
>>> X = np.array([[1, 1], [2, 1], [1, 0],
8383
... [4, 7], [3, 5], [3, 6]])
8484
>>> estimate_bandwidth(X, quantile=0.5)
85-
1.61...
85+
np.float64(1.61...)
8686
"""
8787
X = check_array(X)
8888

sklearn/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ def pytest_collection_modifyitems(config, items):
209209
)
210210
skip_doctests = True
211211

212-
if np_base_version >= parse_version("2"):
212+
if np_base_version < parse_version("2"):
213+
# TODO: configure numpy to output scalar arrays as regular Python scalars
214+
# once possible to improve readability of the tests docstrings.
215+
# https://numpy.org/neps/nep-0051-scalar-representation.html#implementation
213216
reason = "Due to NEP 51 numpy scalar repr has changed in numpy 2"
214217
skip_doctests = True
215218

sklearn/covariance/_shrunk_covariance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def ledoit_wolf_shrinkage(X, assume_centered=False, block_size=1000):
335335
>>> X = rng.multivariate_normal(mean=[0, 0], cov=real_cov, size=50)
336336
>>> shrinkage_coefficient = ledoit_wolf_shrinkage(X)
337337
>>> shrinkage_coefficient
338-
0.23...
338+
np.float64(0.23...)
339339
"""
340340
X = check_array(X)
341341
# for only one feature, the result is the same whatever the shrinkage
@@ -452,7 +452,7 @@ def ledoit_wolf(X, *, assume_centered=False, block_size=1000):
452452
array([[0.44..., 0.16...],
453453
[0.16..., 0.80...]])
454454
>>> shrinkage
455-
0.23...
455+
np.float64(0.23...)
456456
"""
457457
estimator = LedoitWolf(
458458
assume_centered=assume_centered,
@@ -672,7 +672,7 @@ def oas(X, *, assume_centered=False):
672672
array([[0.7533..., 0.2763...],
673673
[0.2763..., 0.3964...]])
674674
>>> shrinkage
675-
0.0195...
675+
np.float64(0.0195...)
676676
"""
677677
estimator = OAS(
678678
assume_centered=assume_centered,
@@ -778,7 +778,7 @@ class OAS(EmpiricalCovariance):
778778
array([[ 1.7833..., -1.2431... ],
779779
[-1.2431..., 3.3889...]])
780780
>>> oas.shrinkage_
781-
0.0195...
781+
np.float64(0.0195...)
782782
"""
783783

784784
@_fit_context(prefer_skip_nested_validation=True)

sklearn/datasets/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def load_wine(*, return_X_y=False, as_frame=False):
571571
>>> data.target[[10, 80, 140]]
572572
array([0, 1, 2])
573573
>>> list(data.target_names)
574-
['class_0', 'class_1', 'class_2']
574+
[np.str_('class_0'), np.str_('class_1'), np.str_('class_2')]
575575
"""
576576

577577
data, target, target_names, fdescr = load_csv_data(
@@ -705,7 +705,7 @@ def load_iris(*, return_X_y=False, as_frame=False):
705705
>>> data.target[[10, 25, 50]]
706706
array([0, 0, 1])
707707
>>> list(data.target_names)
708-
['setosa', 'versicolor', 'virginica']
708+
[np.str_('setosa'), np.str_('versicolor'), np.str_('virginica')]
709709
710710
See :ref:`sphx_glr_auto_examples_datasets_plot_iris_dataset.py` for a more
711711
detailed example of how to work with the iris dataset.
@@ -833,7 +833,7 @@ def load_breast_cancer(*, return_X_y=False, as_frame=False):
833833
>>> data.target[[10, 50, 85]]
834834
array([0, 1, 0])
835835
>>> list(data.target_names)
836-
['malignant', 'benign']
836+
[np.str_('malignant'), np.str_('benign')]
837837
"""
838838
data_file_name = "breast_cancer.csv"
839839
data, target, target_names, fdescr = load_csv_data(

sklearn/datasets/_samples_generator.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def make_classification(
204204
>>> y.shape
205205
(100,)
206206
>>> list(y[:5])
207-
[0, 0, 1, 1, 0]
207+
[np.int64(0), np.int64(0), np.int64(1), np.int64(1), np.int64(0)]
208208
"""
209209
generator = check_random_state(random_state)
210210

@@ -556,7 +556,8 @@ def make_hastie_10_2(n_samples=12000, *, random_state=None):
556556
>>> y.shape
557557
(24000,)
558558
>>> list(y[:5])
559-
[-1.0, 1.0, -1.0, 1.0, -1.0]
559+
[np.float64(-1.0), np.float64(1.0), np.float64(-1.0), np.float64(1.0),
560+
np.float64(-1.0)]
560561
"""
561562
rs = check_random_state(random_state)
562563

@@ -796,7 +797,7 @@ def make_circles(
796797
>>> y.shape
797798
(100,)
798799
>>> list(y[:5])
799-
[1, 1, 1, 0, 0]
800+
[np.int64(1), np.int64(1), np.int64(1), np.int64(0), np.int64(0)]
800801
"""
801802
if isinstance(n_samples, numbers.Integral):
802803
n_samples_out = n_samples // 2
@@ -1168,7 +1169,7 @@ def make_friedman1(n_samples=100, n_features=10, *, noise=0.0, random_state=None
11681169
>>> y.shape
11691170
(100,)
11701171
>>> list(y[:3])
1171-
[16.8..., 5.8..., 9.4...]
1172+
[np.float64(16.8...), np.float64(5.8...), np.float64(9.4...)]
11721173
"""
11731174
generator = check_random_state(random_state)
11741175

@@ -1250,7 +1251,7 @@ def make_friedman2(n_samples=100, *, noise=0.0, random_state=None):
12501251
>>> y.shape
12511252
(100,)
12521253
>>> list(y[:3])
1253-
[1229.4..., 27.0..., 65.6...]
1254+
[np.float64(1229.4...), np.float64(27.0...), np.float64(65.6...)]
12541255
"""
12551256
generator = check_random_state(random_state)
12561257

@@ -1334,7 +1335,7 @@ def make_friedman3(n_samples=100, *, noise=0.0, random_state=None):
13341335
>>> y.shape
13351336
(100,)
13361337
>>> list(y[:3])
1337-
[1.5..., 0.9..., 0.4...]
1338+
[np.float64(1.5...), np.float64(0.9...), np.float64(0.4...)]
13381339
"""
13391340
generator = check_random_state(random_state)
13401341

@@ -2049,7 +2050,7 @@ def make_gaussian_quantiles(
20492050
>>> y.shape
20502051
(100,)
20512052
>>> list(y[:5])
2052-
[2, 0, 1, 0, 2]
2053+
[np.int64(2), np.int64(0), np.int64(1), np.int64(0), np.int64(2)]
20532054
"""
20542055
if n_samples < n_classes:
20552056
raise ValueError("n_samples must be at least n_classes")

sklearn/decomposition/_dict_learning.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -847,15 +847,15 @@ def dict_learning_online(
847847
We can check the level of sparsity of `U`:
848848
849849
>>> np.mean(U == 0)
850-
0.53...
850+
np.float64(0.53...)
851851
852852
We can compare the average squared euclidean norm of the reconstruction
853853
error of the sparse coded signal relative to the squared euclidean norm of
854854
the original signal:
855855
856856
>>> X_hat = U @ V
857857
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
858-
0.05...
858+
np.float64(0.05...)
859859
"""
860860
# TODO(1.6): remove in 1.6
861861
if max_iter is None:
@@ -1049,15 +1049,15 @@ def dict_learning(
10491049
We can check the level of sparsity of `U`:
10501050
10511051
>>> np.mean(U == 0)
1052-
0.6...
1052+
np.float64(0.6...)
10531053
10541054
We can compare the average squared euclidean norm of the reconstruction
10551055
error of the sparse coded signal relative to the squared euclidean norm of
10561056
the original signal:
10571057
10581058
>>> X_hat = U @ V
10591059
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
1060-
0.01...
1060+
np.float64(0.01...)
10611061
"""
10621062
estimator = DictionaryLearning(
10631063
n_components=n_components,
@@ -1551,15 +1551,15 @@ class DictionaryLearning(_BaseSparseCoding, BaseEstimator):
15511551
We can check the level of sparsity of `X_transformed`:
15521552
15531553
>>> np.mean(X_transformed == 0)
1554-
0.52...
1554+
np.float64(0.52...)
15551555
15561556
We can compare the average squared euclidean norm of the reconstruction
15571557
error of the sparse coded signal relative to the squared euclidean norm of
15581558
the original signal:
15591559
15601560
>>> X_hat = X_transformed @ dict_learner.components_
15611561
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
1562-
0.05...
1562+
np.float64(0.05...)
15631563
"""
15641564

15651565
_parameter_constraints: dict = {
@@ -1914,15 +1914,15 @@ class MiniBatchDictionaryLearning(_BaseSparseCoding, BaseEstimator):
19141914
We can check the level of sparsity of `X_transformed`:
19151915
19161916
>>> np.mean(X_transformed == 0) > 0.5
1917-
True
1917+
np.True_
19181918
19191919
We can compare the average squared euclidean norm of the reconstruction
19201920
error of the sparse coded signal relative to the squared euclidean norm of
19211921
the original signal:
19221922
19231923
>>> X_hat = X_transformed @ dict_learner.components_
19241924
>>> np.mean(np.sum((X_hat - X) ** 2, axis=1) / np.sum(X ** 2, axis=1))
1925-
0.052...
1925+
np.float64(0.052...)
19261926
"""
19271927

19281928
_parameter_constraints: dict = {

sklearn/decomposition/_sparse_pca.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class SparsePCA(_BaseSparsePCA):
267267
(200, 5)
268268
>>> # most values in the components_ are zero (sparsity)
269269
>>> np.mean(transformer.components_ == 0)
270-
0.9666...
270+
np.float64(0.9666...)
271271
"""
272272

273273
_parameter_constraints: dict = {
@@ -473,7 +473,7 @@ class MiniBatchSparsePCA(_BaseSparsePCA):
473473
(200, 5)
474474
>>> # most values in the components_ are zero (sparsity)
475475
>>> np.mean(transformer.components_ == 0)
476-
0.9...
476+
np.float64(0.9...)
477477
"""
478478

479479
_parameter_constraints: dict = {

sklearn/feature_selection/_from_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class SelectFromModel(MetaEstimatorMixin, SelectorMixin, BaseEstimator):
219219
>>> selector.estimator_.coef_
220220
array([[-0.3252..., 0.8345..., 0.4976...]])
221221
>>> selector.threshold_
222-
0.55249...
222+
np.float64(0.55249...)
223223
>>> selector.get_support()
224224
array([False, True, False])
225225
>>> selector.transform(X)

sklearn/isotonic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def check_increasing(x, y):
6565
>>> from sklearn.isotonic import check_increasing
6666
>>> x, y = [1, 2, 3, 4, 5], [2, 4, 6, 8, 10]
6767
>>> check_increasing(x, y)
68-
True
68+
np.True_
6969
>>> y = [10, 8, 6, 4, 2]
7070
>>> check_increasing(x, y)
71-
False
71+
np.False_
7272
"""
7373

7474
# Calculate Spearman rho estimate and set return accordingly.

sklearn/linear_model/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ class LinearRegression(MultiOutputMixin, RegressorMixin, LinearModel):
544544
>>> reg.coef_
545545
array([1., 2.])
546546
>>> reg.intercept_
547-
3.0...
547+
np.float64(3.0...)
548548
>>> reg.predict(np.array([[3, 5]]))
549549
array([16.])
550550
"""

0 commit comments

Comments
 (0)