Skip to content

Commit ae76a6b

Browse files
committed
Fix numpy 2 doctests
1 parent 6dba100 commit ae76a6b

File tree

15 files changed

+100
-104
lines changed

15 files changed

+100
-104
lines changed

advanced/advanced_numpy/index.rst

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ Casting
312312
>>> y + 1
313313
array([2, 3, 4, 5], dtype=int8)
314314
>>> y + 256
315-
array([257, 258, 259, 260], dtype=int16)
315+
Traceback (most recent call last):
316+
File "<stdin>", line 1, in <module>
317+
OverflowError: Python integer 256 out of bounds for int8
316318
>>> y + 256.0
317319
array([257., 258., 259., 260.])
318320
>>> y + np.array([256], dtype=np.int32)
@@ -507,9 +509,9 @@ Main point
507509
(3, 1)
508510
>>> byte_offset = 3 * 1 + 1 * 2 # to find x[1, 2]
509511
>>> x.flat[byte_offset]
510-
6
512+
np.int8(6)
511513
>>> x[1, 2]
512-
6
514+
np.int8(6)
513515
514516
simple, **flexible**
515517

@@ -1343,18 +1345,12 @@ Array siblings: :class:`chararray`, :class:`maskedarray`
13431345
:class:`chararray`: vectorized string operations
13441346
--------------------------------------------------
13451347

1346-
>>> x = np.array(['a', ' bbb', ' ccc']).view(np.chararray)
1347-
>>> x.lstrip(' ')
1348-
chararray(['a', 'bbb', 'ccc'],
1349-
dtype='...')
1348+
>>> x = np.char.asarray(['a', ' bbb', ' ccc'])
1349+
>>> x
1350+
chararray(['a', ' bbb', ' ccc'], dtype='<U5')
13501351
>>> x.upper()
1351-
chararray(['A', ' BBB', ' CCC'],
1352-
dtype='...')
1352+
chararray(['A', ' BBB', ' CCC'], dtype='<U5')
13531353

1354-
.. note::
1355-
1356-
``.view()`` has a second meaning: it can make an ndarray an instance
1357-
of a specialized ndarray subclass
13581354

13591355
:class:`masked_array` missing data
13601356
------------------------------------
@@ -1376,9 +1372,9 @@ One way to describe this is to create a masked array::
13761372
Masked mean ignores masked data::
13771373

13781374
>>> mx.mean()
1379-
2.75
1375+
np.float64(2.75)
13801376
>>> np.mean(mx)
1381-
2.75
1377+
np.float64(2.75)
13821378

13831379
.. warning:: Not all NumPy functions respect masks, for instance
13841380
``np.dot``, so check the return types.
@@ -1588,7 +1584,7 @@ Good bug report
15881584
3. Version of NumPy/SciPy
15891585

15901586
>>> print(np.__version__)
1591-
1...
1587+
2...
15921588

15931589
**Check that the following is what you expect**
15941590

advanced/image_processing/index.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Increase contrast by setting min and max values::
137137
<matplotlib.image.AxesImage object at 0x...>
138138
>>> # Remove axes and ticks
139139
>>> plt.axis('off')
140-
(-0.5, 1023.5, 767.5, -0.5)
140+
(np.float64(-0.5), np.float64(1023.5), np.float64(767.5), np.float64(-0.5))
141141

142142
Draw contour lines::
143143

@@ -190,7 +190,7 @@ Images are arrays: use the whole ``numpy`` machinery.
190190

191191
>>> face = sp.datasets.face(gray=True)
192192
>>> face[0, 40]
193-
127
193+
np.uint8(127)
194194
>>> # Slicing
195195
>>> face[10:13, 20:23]
196196
array([[141, 153, 145],
@@ -222,9 +222,9 @@ Statistical information
222222

223223
>>> face = sp.datasets.face(gray=True)
224224
>>> face.mean()
225-
113.48026784261067
225+
np.float64(113.48026784261067)
226226
>>> face.max(), face.min()
227-
(250, 0)
227+
(np.uint8(250), np.uint8(0))
228228

229229

230230
``np.histogram``
@@ -653,9 +653,9 @@ Use mathematical morphology to clean up the result::
653653
>>> eroded_tmp = sp.ndimage.binary_erosion(tmp)
654654
>>> reconstruct_final = np.logical_not(sp.ndimage.binary_propagation(eroded_tmp, mask=tmp))
655655
>>> np.abs(mask - close_img).mean()
656-
0.00640699...
656+
np.float64(0.00640699...)
657657
>>> np.abs(mask - reconstruct_final).mean()
658-
0.00082232...
658+
np.float64(0.00082232...)
659659

660660
.. topic:: **Exercise**
661661
:class: green

advanced/mathematical_optimization/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ Brent's method to find the minimum of a function:
180180
True
181181
>>> x_min = result.x
182182
>>> x_min
183-
0.50...
183+
np.float64(0.50...)
184184
>>> x_min - 0.5
185-
5.8...e-09
185+
np.float64(5.8...e-09)
186186

187187

188188
.. |1d_optim_1| image:: auto_examples/images/sphx_glr_plot_1d_optim_001.png
@@ -824,7 +824,7 @@ handy.
824824
given, and a gradient computed numerically:
825825

826826
>>> sp.optimize.check_grad(f, jacobian, [2, -1])
827-
2.384185791015625e-07
827+
np.float64(2.384185791015625e-07)
828828

829829
See also :func:`scipy.optimize.approx_fprime` to find your errors.
830830

@@ -897,7 +897,7 @@ if we compute the norm ourselves and use a good generic optimizer
897897
... return np.sum(f(x)**2)
898898
>>> result = sp.optimize.minimize(g, x0, method="BFGS")
899899
>>> result.fun
900-
2.6940...e-11
900+
np.float64(2.6940...e-11)
901901

902902
BFGS needs more function calls, and gives a less precise result.
903903

advanced/scipy_sparse/dia_array.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ Examples
6363
>>> mtx.offsets
6464
array([ 0, -1, 2], dtype=int32)
6565
>>> print(mtx)
66-
(0, 0) 1
67-
(1, 1) 2
68-
(2, 2) 3
69-
(3, 3) 4
70-
(1, 0) 5
71-
(2, 1) 6
72-
(3, 2) 7
73-
(0, 2) 11
74-
(1, 3) 12
66+
(np.int32(0), np.int32(0)) 1
67+
(np.int32(1), np.int32(1)) 2
68+
(np.int32(2), np.int32(2)) 3
69+
(np.int32(3), np.int32(3)) 4
70+
(np.int32(1), np.int32(0)) 5
71+
(np.int32(2), np.int32(1)) 6
72+
(np.int32(3), np.int32(2)) 7
73+
(np.int32(0), np.int32(2)) 11
74+
(np.int32(1), np.int32(3)) 12
7575
>>> mtx.toarray()
7676
array([[ 1, 0, 11, 0],
7777
[ 5, 2, 0, 12],

advanced/scipy_sparse/dok_array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Examples
4646
* slicing and indexing::
4747

4848
>>> mtx[1, 1]
49-
0.0
49+
np.float64(0.0)
5050
>>> mtx[[1], 1:3]
5151
<1x2 sparse array of type '<... 'numpy.float64'>'
5252
with 1 stored elements in Dictionary Of Keys format>

intro/numpy/advanced_operations.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ For example, :math:`3x^2 + 2x - 1`::
2525

2626
>>> p = np.poly1d([3, 2, -1])
2727
>>> p(0)
28-
-1
28+
np.int64(-1)
2929
>>> p.roots
3030
array([-1. , 0.33333333])
3131
>>> p.order
@@ -60,7 +60,7 @@ e.g. the Chebyshev basis.
6060

6161
>>> p = np.polynomial.Polynomial([-1, 2, 3]) # coefs in different order!
6262
>>> p(0)
63-
-1.0
63+
np.float64(-1.0)
6464
>>> p.roots()
6565
array([-1. , 0.33333333])
6666
>>> p.degree() # In general polynomials do not always expose 'order'

intro/numpy/array_object.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ other Python sequences (e.g. lists):
463463
>>> a
464464
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
465465
>>> a[0], a[2], a[-1]
466-
(0, 2, 9)
466+
(np.int64(0), np.int64(2), np.int64(9))
467467

468468
.. warning::
469469

@@ -487,7 +487,7 @@ For multidimensional arrays, indices are tuples of integers:
487487
[0, 1, 0],
488488
[0, 0, 2]])
489489
>>> a[1, 1]
490-
1
490+
np.int64(1)
491491
>>> a[2, 1] = 10 # third line, second column
492492
>>> a
493493
array([[ 0, 0, 0],

intro/numpy/elaborate_arrays.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ Floating-point numbers:
9797
::
9898

9999
>>> np.finfo(np.float32).eps
100-
1.1920929e-07
100+
np.float32(1.1920929e-07)
101101
>>> np.finfo(np.float64).eps
102-
2.2204460492503131e-16
102+
np.float64(2.220446049250313e-16)
103103

104104
>>> np.float32(1e-8) + np.float32(1) == 1
105-
True
105+
np.True_
106106
>>> np.float64(1e-8) + np.float64(1) == 1
107-
False
107+
np.False_
108108

109109
Complex floating-point numbers:
110110

@@ -173,11 +173,11 @@ Field access works by indexing with field names::
173173
>>> samples['value']
174174
array([0.37, 0.11, 0.13, 0.37, 0.11, 0.13])
175175
>>> samples[0]
176-
(b'ALFA', 1., 0.37)
176+
np.void((b'ALFA', 1.0, 0.37), dtype=[('sensor_code', 'S4'), ('position', '<f8'), ('value', '<f8')])
177177

178178
>>> samples[0]['sensor_code'] = 'TAU'
179179
>>> samples[0]
180-
(b'TAU', 1., 0.37)
180+
np.void((b'TAU', 1.0, 0.37), dtype=[('sensor_code', 'S4'), ('position', '<f8'), ('value', '<f8')])
181181

182182
Multiple fields at once::
183183

intro/numpy/operations.rst

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ Computing sums
208208

209209
>>> x = np.array([1, 2, 3, 4])
210210
>>> np.sum(x)
211-
10
211+
np.int64(10)
212212
>>> x.sum()
213-
10
213+
np.int64(10)
214214

215215
.. image:: images/reductions.png
216216
:align: right
@@ -226,11 +226,11 @@ Sum by rows and by columns:
226226
>>> x.sum(axis=0) # columns (first dimension)
227227
array([3, 3])
228228
>>> x[:, 0].sum(), x[:, 1].sum()
229-
(3, 3)
229+
(np.int64(3), np.int64(3))
230230
>>> x.sum(axis=1) # rows (second dimension)
231231
array([2, 4])
232232
>>> x[0, :].sum(), x[1, :].sum()
233-
(2, 4)
233+
(np.int64(2), np.int64(4))
234234

235235
.. tip::
236236

@@ -241,9 +241,9 @@ Sum by rows and by columns:
241241
>>> rng = np.random.default_rng(27446968)
242242
>>> x = rng.random((2, 2, 2))
243243
>>> x.sum(axis=2)[0, 1]
244-
0.73415...
244+
np.float64(0.73415...)
245245
>>> x[0, 1, :].sum()
246-
0.73415...
246+
np.float64(0.73415...)
247247

248248
Other reductions
249249
................
@@ -256,23 +256,23 @@ Other reductions
256256

257257
>>> x = np.array([1, 3, 2])
258258
>>> x.min()
259-
1
259+
np.int64(1)
260260
>>> x.max()
261-
3
261+
np.int64(3)
262262

263263
>>> x.argmin() # index of minimum
264-
0
264+
np.int64(0)
265265
>>> x.argmax() # index of maximum
266-
1
266+
np.int64(1)
267267

268268
**Logical operations:**
269269

270270
.. sourcecode:: pycon
271271

272272
>>> np.all([True, True, False])
273-
False
273+
np.False_
274274
>>> np.any([True, True, False])
275-
True
275+
np.True_
276276

277277
.. note::
278278

@@ -282,15 +282,15 @@ Other reductions
282282

283283
>>> a = np.zeros((100, 100))
284284
>>> np.any(a != 0)
285-
False
285+
np.False_
286286
>>> np.all(a == a)
287-
True
287+
np.True_
288288

289289
>>> a = np.array([1, 2, 3, 2])
290290
>>> b = np.array([2, 2, 3, 2])
291291
>>> c = np.array([6, 4, 4, 5])
292292
>>> ((a <= b) & (b <= c)).all()
293-
True
293+
np.True_
294294

295295
**Statistics:**
296296

@@ -299,14 +299,14 @@ Other reductions
299299
>>> x = np.array([1, 2, 3, 1])
300300
>>> y = np.array([[1, 2, 3], [5, 6, 1]])
301301
>>> x.mean()
302-
1.75
302+
np.float64(1.75)
303303
>>> np.median(x)
304-
1.5
304+
np.float64(1.5)
305305
>>> np.median(y, axis=-1) # last axis
306306
array([2., 5.])
307307

308308
>>> x.std() # full population standard dev.
309-
0.82915619758884995
309+
np.float64(0.82915619758884995)
310310

311311

312312
... and many more (best to learn as you go).
@@ -709,20 +709,20 @@ Dimension shuffling
709709
>>> a.shape
710710
(4, 3, 2)
711711
>>> a[0, 2, 1]
712-
5
712+
np.int64(5)
713713
>>> b = a.transpose(1, 2, 0)
714714
>>> b.shape
715715
(3, 2, 4)
716716
>>> b[2, 1, 0]
717-
5
717+
np.int64(5)
718718

719719
Also creates a view:
720720

721721
.. sourcecode:: pycon
722722

723723
>>> b[2, 1, 0] = -1
724724
>>> a[0, 2, 1]
725-
-1
725+
np.int64(-1)
726726

727727
Resizing
728728
........
@@ -817,7 +817,7 @@ Finding minima and maxima:
817817
>>> j_max = np.argmax(a)
818818
>>> j_min = np.argmin(a)
819819
>>> j_max, j_min
820-
(0, 2)
820+
(np.int64(0), np.int64(2))
821821

822822

823823
.. XXX: need a frame for summaries

intro/scipy/image_processing/image_processing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Changing orientation, resolution, .. ::
4040
<matplotlib.image.AxesImage object at 0x...>
4141

4242
>>> plt.axis('off')
43-
(-0.5, 1023.5, 767.5, -0.5)
43+
(np.float64(-0.5), np.float64(1023.5), np.float64(767.5), np.float64(-0.5))
4444

4545
>>> # etc.
4646

0 commit comments

Comments
 (0)