Skip to content

Commit ba7c55c

Browse files
authored
Merge pull request numpy#26606 from bmwoodruff/update-randn-to-standard_normal
DOC: Update randn() to use rng.standard_normal()
2 parents 0516b05 + ab770f6 commit ba7c55c

File tree

9 files changed

+48
-30
lines changed

9 files changed

+48
-30
lines changed

numpy/_core/shape_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ def stack(arrays, axis=0, out=None, *, dtype=None, casting="same_kind"):
417417
418418
Examples
419419
--------
420-
>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
420+
>>> rng = np.random.default_rng()
421+
>>> arrays = [rng.normal(size=(3,4)) for _ in range(10)]
421422
>>> np.stack(arrays, axis=0).shape
422423
(10, 3, 4)
423424

numpy/lib/_function_base_impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ def flip(m, axis=None):
323323
[7, 6]],
324324
[[1, 0],
325325
[3, 2]]])
326-
>>> A = np.random.randn(3,4,5)
326+
>>> rng = np.random.default_rng()
327+
>>> A = rng.normal(size=(3,4,5))
327328
>>> np.all(np.flip(A,2) == A[:,:,::-1,...])
328329
True
329330
"""

numpy/lib/_histograms_impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,8 @@ def histogramdd(sample, bins=10, range=None, density=None, weights=None):
972972
973973
Examples
974974
--------
975-
>>> r = np.random.randn(100,3)
975+
>>> rng = np.random.default_rng()
976+
>>> r = rng.normal(size=(100,3))
976977
>>> H, edges = np.histogramdd(r, bins = (5, 8, 4))
977978
>>> H.shape, edges[0].size, edges[1].size, edges[2].size
978979
((5, 8, 4), 6, 9, 5)

numpy/lib/_twodim_base_impl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def fliplr(m):
8989
[0., 2., 0.],
9090
[3., 0., 0.]])
9191
92-
>>> A = np.random.randn(2,3,5)
92+
>>> rng = np.random.default_rng()
93+
>>> A = rng.normal(size=(2,3,5))
9394
>>> np.all(np.fliplr(A) == A[:,::-1,...])
9495
True
9596
@@ -142,7 +143,8 @@ def flipud(m):
142143
[0., 2., 0.],
143144
[1., 0., 0.]])
144145
145-
>>> A = np.random.randn(2,3,5)
146+
>>> rng = np.random.default_rng()
147+
>>> A = rng.normal(size=(2,3,5))
146148
>>> np.all(np.flipud(A) == A[::-1,...])
147149
True
148150

numpy/linalg/_linalg.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def tensorsolve(a, b, axes=None):
274274
--------
275275
>>> a = np.eye(2*3*4)
276276
>>> a.shape = (2*3, 4, 2, 3, 4)
277-
>>> b = np.random.randn(2*3, 4)
277+
>>> rng = np.random.default_rng()
278+
>>> b = rng.normal(size=(2*3, 4))
278279
>>> x = np.linalg.tensorsolve(a, b)
279280
>>> x.shape
280281
(2, 3, 4)
@@ -456,7 +457,8 @@ def tensorinv(a, ind=2):
456457
>>> ainv = np.linalg.tensorinv(a, ind=2)
457458
>>> ainv.shape
458459
(8, 3, 4, 6)
459-
>>> b = np.random.randn(4, 6)
460+
>>> rng = np.random.default_rng()
461+
>>> b = rng.normal(size=(4, 6))
460462
>>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b))
461463
True
462464
@@ -465,7 +467,8 @@ def tensorinv(a, ind=2):
465467
>>> ainv = np.linalg.tensorinv(a, ind=1)
466468
>>> ainv.shape
467469
(8, 3, 24)
468-
>>> b = np.random.randn(24)
470+
>>> rng = np.random.default_rng()
471+
>>> b = rng.normal(size=24)
469472
>>> np.allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b))
470473
True
471474
@@ -978,7 +981,8 @@ def qr(a, mode='reduced'):
978981
979982
Examples
980983
--------
981-
>>> a = np.random.randn(9, 6)
984+
>>> rng = np.random.default_rng()
985+
>>> a = rng.normal(size=(9, 6))
982986
>>> Q, R = np.linalg.qr(a)
983987
>>> np.allclose(a, np.dot(Q, R)) # a does equal QR
984988
True
@@ -1703,8 +1707,10 @@ def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
17031707
17041708
Examples
17051709
--------
1706-
>>> a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6)
1707-
>>> b = np.random.randn(2, 7, 8, 3) + 1j*np.random.randn(2, 7, 8, 3)
1710+
>>> rng = np.random.default_rng()
1711+
>>> a = rng.normal(size=(9, 6)) + 1j*rng.normal(size=(9, 6))
1712+
>>> b = rng.normal(size=(2, 7, 8, 3)) + 1j*rng.normal(size=(2, 7, 8, 3))
1713+
17081714
17091715
Reconstruction based on full SVD, 2D case:
17101716
@@ -2181,7 +2187,8 @@ def pinv(a, rcond=None, hermitian=False, *, rtol=_NoValue):
21812187
The following example checks that ``a * a+ * a == a`` and
21822188
``a+ * a * a+ == a+``:
21832189
2184-
>>> a = np.random.randn(9, 6)
2190+
>>> rng = np.random.default_rng()
2191+
>>> a = rng.normal(size=(9, 6))
21852192
>>> B = np.linalg.pinv(a)
21862193
>>> np.allclose(a, np.dot(a, np.dot(B, a)))
21872194
True

numpy/polynomial/hermite.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,10 +1460,11 @@ def hermfit(x, y, deg, rcond=None, full=False, w=None):
14601460
--------
14611461
>>> from numpy.polynomial.hermite import hermfit, hermval
14621462
>>> x = np.linspace(-10, 10)
1463-
>>> err = np.random.randn(len(x))/10
1463+
>>> rng = np.random.default_rng()
1464+
>>> err = rng.normal(scale=1./10, size=len(x))
14641465
>>> y = hermval(x, [1, 2, 3]) + err
14651466
>>> hermfit(x, y, 2)
1466-
array([1.0218, 1.9986, 2.9999]) # may vary
1467+
array([1.02294967, 2.00016403, 2.99994614]) # may vary
14671468
14681469
"""
14691470
return pu._fit(hermvander, x, y, deg, rcond, full, w)

numpy/polynomial/hermite_e.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,11 +1387,11 @@ def hermefit(x, y, deg, rcond=None, full=False, w=None):
13871387
--------
13881388
>>> from numpy.polynomial.hermite_e import hermefit, hermeval
13891389
>>> x = np.linspace(-10, 10)
1390-
>>> np.random.seed(123)
1391-
>>> err = np.random.randn(len(x))/10
1390+
>>> rng = np.random.default_rng()
1391+
>>> err = rng.normal(scale=1./10, size=len(x))
13921392
>>> y = hermeval(x, [1, 2, 3]) + err
13931393
>>> hermefit(x, y, 2)
1394-
array([ 1.01690445, 1.99951418, 2.99948696]) # may vary
1394+
array([1.02284196, 2.00032805, 2.99978457]) # may vary
13951395
13961396
"""
13971397
return pu._fit(hermevander, x, y, deg, rcond, full, w)

numpy/polynomial/laguerre.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,10 +1441,11 @@ def lagfit(x, y, deg, rcond=None, full=False, w=None):
14411441
--------
14421442
>>> from numpy.polynomial.laguerre import lagfit, lagval
14431443
>>> x = np.linspace(0, 10)
1444-
>>> err = np.random.randn(len(x))/10
1444+
>>> rng = np.random.default_rng()
1445+
>>> err = rng.normal(scale=1./10, size=len(x))
14451446
>>> y = lagval(x, [1, 2, 3]) + err
14461447
>>> lagfit(x, y, 2)
1447-
array([ 0.96971004, 2.00193749, 3.00288744]) # may vary
1448+
array([1.00578369, 1.99417356, 2.99827656]) # may vary
14481449
14491450
"""
14501451
return pu._fit(lagvander, x, y, deg, rcond, full, w)

numpy/polynomial/polynomial.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,27 +1441,31 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None):
14411441
14421442
Examples
14431443
--------
1444-
>>> np.random.seed(123)
14451444
>>> from numpy.polynomial import polynomial as P
14461445
>>> x = np.linspace(-1,1,51) # x "data": [-1, -0.96, ..., 0.96, 1]
1447-
>>> y = x**3 - x + np.random.randn(len(x)) # x^3 - x + Gaussian noise
1446+
>>> rng = np.random.default_rng()
1447+
>>> err = rng.normal(size=len(x))
1448+
>>> y = x**3 - x + err # x^3 - x + Gaussian noise
14481449
>>> c, stats = P.polyfit(x,y,3,full=True)
1449-
>>> np.random.seed(123)
1450-
>>> c # c[0], c[2] should be approx. 0, c[1] approx. -1, c[3] approx. 1
1451-
array([ 0.01909725, -1.30598256, -0.00577963, 1.02644286]) # may vary
1450+
>>> c # c[0], c[1] approx. -1, c[2] should be approx. 0, c[3] approx. 1
1451+
array([ 0.23111996, -1.02785049, -0.2241444 , 1.08405657]) # may vary
14521452
>>> stats # note the large SSR, explaining the rather poor results
1453-
[array([ 38.06116253]), 4, array([ 1.38446749, 1.32119158, 0.50443316, # may vary
1454-
0.28853036]), 1.1324274851176597e-014]
1453+
[array([48.312088]), # may vary
1454+
4,
1455+
array([1.38446749, 1.32119158, 0.50443316, 0.28853036]),
1456+
1.1324274851176597e-14]
14551457
14561458
Same thing without the added noise
14571459
14581460
>>> y = x**3 - x
14591461
>>> c, stats = P.polyfit(x,y,3,full=True)
1460-
>>> c # c[0], c[2] should be "very close to 0", c[1] ~= -1, c[3] ~= 1
1461-
array([-6.36925336e-18, -1.00000000e+00, -4.08053781e-16, 1.00000000e+00])
1462+
>>> c # c[0], c[1] ~= -1, c[2] should be "very close to 0", c[3] ~= 1
1463+
array([-6.73496154e-17, -1.00000000e+00, 0.00000000e+00, 1.00000000e+00])
14621464
>>> stats # note the minuscule SSR
1463-
[array([ 7.46346754e-31]), 4, array([ 1.38446749, 1.32119158, # may vary
1464-
0.50443316, 0.28853036]), 1.1324274851176597e-014]
1465+
[array([8.79579319e-31]),
1466+
4,
1467+
array([1.38446749, 1.32119158, 0.50443316, 0.28853036]),
1468+
1.1324274851176597e-14]
14651469
14661470
"""
14671471
return pu._fit(polyvander, x, y, deg, rcond, full, w)

0 commit comments

Comments
 (0)