Skip to content

Commit e15b2ed

Browse files
authored
🌐 [translation-sync] [numpy.md] Update np.random → Generator API (#65)
* Update translation: lectures/numpy.md * Update translation: .translate/state/numpy.md.yml
1 parent a5f656a commit e15b2ed

2 files changed

Lines changed: 33 additions & 45 deletions

File tree

.translate/state/numpy.md.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
source-sha: cc9c3256dc35bd277cb25d0089f0a0452c0fa94e
2-
synced-at: "2026-03-20"
1+
source-sha: a2b929f15e703b6942e8b80a29011c51f234b1e0
2+
synced-at: "2026-05-13"
33
model: claude-sonnet-4-6
4-
mode: NEW
4+
mode: UPDATE
55
section-count: 8
6-
tool-version: 0.13.0
6+
tool-version: 0.15.0

lectures/numpy.md

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ z
240240

241241
要从包含数值数据的文本文件中读取数组数据,使用 `np.loadtxt`——详情请参阅[文档](https://numpy.org/doc/stable/reference/routines.io.html)
242242

243-
244-
245243
### 数组索引
246244

247245
```{index} single: NumPy; Arrays (Indexing)
@@ -401,7 +399,6 @@ z
401399
z.searchsorted(2.2)
402400
```
403401

404-
405402
## 算术运算
406403

407404
```{index} single: NumPy; Arithmetic Operations
@@ -450,7 +447,6 @@ A * B
450447
(numpy_matrix_multiplication)=
451448
特别地,`A * B` *不是*矩阵乘积,而是逐元素乘积。
452449

453-
454450
## 矩阵乘法
455451

456452
```{index} single: NumPy; Matrix Multiplication
@@ -852,8 +848,6 @@ ax.text(11, 7.0, '?', size=16, ha='center', va='center');
852848
- 通过*第二步*`b` 将被扩展为 `b -> (2, 2, 2)`
853849
- 我们可以看到经过前两步后它们仍然不匹配。因此,将引发 `ValueError`
854850

855-
856-
857851
## 可变性与数组复制
858852

859853
NumPy 数组是可变数据类型,类似于 Python 列表。
@@ -864,7 +858,6 @@ NumPy 数组是可变数据类型,类似于 Python 列表。
864858

865859
在本节中,我们回顾一些关键问题。
866860

867-
868861
### 可变性
869862

870863
我们在上面已经看到了可变性的例子。
@@ -884,7 +877,8 @@ a
884877
可变性导致以下行为(这可能会让 MATLAB 程序员感到震惊……)
885878

886879
```{code-cell} python3
887-
a = np.random.randn(3)
880+
rng = np.random.default_rng()
881+
a = rng.standard_normal(3)
888882
a
889883
```
890884

@@ -896,7 +890,7 @@ a
896890

897891
发生的情况是我们通过修改 `b` 改变了 `a`
898892

899-
名称 `b` 绑定到 `a`,成为该数组的另一个引用(Python 赋值模型在{doc}`课程后面 <python_advanced_features>`有更详细的描述)。
893+
名称 `b` 绑定到 `a`,成为该数组的另一个引用(Python 赋值模型在 {doc}`课程后面 <python_advanced_features>` 有更详细的描述)。
900894

901895
因此,它有同等权利对该数组进行更改。
902896

@@ -913,7 +907,7 @@ a
913907
这可以使用 `np.copy` 来完成
914908

915909
```{code-cell} python3
916-
a = np.random.randn(3)
910+
a = rng.standard_normal(3)
917911
a
918912
```
919913

@@ -935,14 +929,10 @@ a
935929

936930
注意对 `b` 的更改没有影响 `a`
937931

938-
939-
940-
941932
## 其他功能
942933

943934
让我们来看看 NumPy 的其他一些有用功能。
944935

945-
946936
### 通用函数
947937

948938
```{index} single: NumPy; Vectorized Functions
@@ -990,7 +980,7 @@ def f(x):
990980
NumPy 函数 `np.where` 提供了一个向量化的替代方案:
991981

992982
```{code-cell} python3
993-
x = np.random.randn(4)
983+
x = rng.standard_normal(4)
994984
x
995985
```
996986

@@ -1009,7 +999,6 @@ f(x) # 传递与前一个例子中相同的向量 x
1009999

10101000
(稍后我们将看到 JAX 有一个强大的 `np.vectorize` 版本,通常确实可以生成高效的代码。)
10111001

1012-
10131002
### 比较
10141003

10151004
```{index} single: NumPy; Comparisons
@@ -1066,11 +1055,11 @@ z[z > 3]
10661055

10671056
NumPy 通过其子包提供了一些与科学编程相关的附加功能。
10681057

1069-
我们已经看到了如何使用 np.random 生成随机变量
1058+
我们已经看到了如何使用 NumPy 的 [随机 `Generator`](https://numpy.org/doc/stable/reference/random/generator.html#random-generator) 生成随机变量
10701059

10711060
```{code-cell} python3
1072-
z = np.random.randn(10000) # 生成标准正态随机数
1073-
y = np.random.binomial(10, 0.5, size=1000) # 从 Bin(10, 0.5) 中抽取 1000 个样本
1061+
z = rng.standard_normal(10000) # 生成标准正态随机数
1062+
y = rng.binomial(10, 0.5, size=1000) # 从 Bin(10, 0.5) 中抽取 1000 个样本
10741063
y.mean()
10751064
```
10761065

@@ -1094,10 +1083,9 @@ np.linalg.inv(A) # 计算逆矩阵
10941083

10951084
这些功能的大部分也可在 [SciPy](https://scipy.org/) 中找到,SciPy 是建立在 NumPy 之上的模块集合。
10961085

1097-
我们将在{doc}`不久后 <scipy>`更详细地介绍 SciPy 版本。
1098-
1099-
有关 NumPy 中可用内容的完整列表,请参阅[此文档](https://numpy.org/doc/stable/reference/routines.html)
1086+
我们将在 {doc}`不久后 <scipy>` 更详细地介绍 SciPy 版本。
11001087

1088+
有关 NumPy 中可用内容的完整列表,请参阅 [此文档](https://numpy.org/doc/stable/reference/routines.html)
11011089

11021090
### 隐式多线程
11031091

@@ -1115,7 +1103,7 @@ NumPy 在其大部分编译代码中尝试实现多线程。
11151103
n = 20
11161104
m = 1000
11171105
for i in range(n):
1118-
X = np.random.randn(m, m)
1106+
X = rng.standard_normal((m, m))
11191107
λ = np.linalg.eigvals(X)
11201108
```
11211109

@@ -1129,7 +1117,6 @@ for i in range(n):
11291117

11301118
这是因为 NumPy 的 `eigvals` 例程巧妙地将任务分割并分发到不同的线程。
11311119

1132-
11331120
## 练习
11341121

11351122

@@ -1251,25 +1238,25 @@ def sample(q):
12511238

12521239
```{code-cell} python3
12531240
from numpy import cumsum
1254-
from numpy.random import uniform
12551241
12561242
class DiscreteRV:
12571243
"""
12581244
根据给定概率向量 q 生成离散随机变量的抽样数组。
12591245
"""
12601246
1261-
def __init__(self, q):
1247+
def __init__(self, q, seed=None):
12621248
"""
12631249
参数 q 是一个 NumPy 数组或类似数组,非负且和为 1
12641250
"""
12651251
self.q = q
12661252
self.Q = cumsum(q)
1253+
self.rng = np.random.default_rng(seed)
12671254
12681255
def draw(self, k=1):
12691256
"""
12701257
从 q 中返回 k 个样本。对于每次抽样,值 i 以概率 q[i] 返回。
12711258
"""
1272-
return self.Q.searchsorted(uniform(0, 1, size=k))
1259+
return self.Q.searchsorted(self.rng.uniform(0, 1, size=k))
12731260
```
12741261

12751262
逻辑不是很直观,但如果你慢慢阅读,就会理解。
@@ -1389,7 +1376,8 @@ class ECDF:
13891376

13901377
```{code-cell} python3
13911378
fig, ax = plt.subplots()
1392-
X = np.random.randn(1000)
1379+
rng = np.random.default_rng()
1380+
X = rng.standard_normal(1000)
13931381
F = ECDF(X)
13941382
F.plot(ax)
13951383
```
@@ -1410,9 +1398,9 @@ F.plot(ax)
14101398

14111399
```{code-cell} python3
14121400
1413-
np.random.seed(123)
1414-
x = np.random.randn(4, 4)
1415-
y = np.random.randn(4)
1401+
rng = np.random.default_rng(123)
1402+
x = rng.standard_normal((4, 4))
1403+
y = rng.standard_normal(4)
14161404
A = x / y
14171405
```
14181406

@@ -1440,9 +1428,9 @@ print(A)
14401428

14411429
```{code-cell} python3
14421430
1443-
np.random.seed(123)
1444-
x = np.random.randn(1000, 100, 100)
1445-
y = np.random.randn(100)
1431+
rng = np.random.default_rng(123)
1432+
x = rng.standard_normal((1000, 100, 100))
1433+
y = rng.standard_normal(100)
14461434
14471435
with qe.Timer("广播操作"):
14481436
B = x / y
@@ -1468,9 +1456,9 @@ print(B)
14681456
**第一部分解答**
14691457

14701458
```{code-cell} python3
1471-
np.random.seed(123)
1472-
x = np.random.randn(4, 4)
1473-
y = np.random.randn(4)
1459+
rng = np.random.default_rng(123)
1460+
x = rng.standard_normal((4, 4))
1461+
y = rng.standard_normal(4)
14741462
14751463
C = np.empty_like(x)
14761464
n = len(x)
@@ -1499,9 +1487,9 @@ print(np.array_equal(A, C))
14991487

15001488
```{code-cell} python3
15011489
1502-
np.random.seed(123)
1503-
x = np.random.randn(1000, 100, 100)
1504-
y = np.random.randn(100)
1490+
rng = np.random.default_rng(123)
1491+
x = rng.standard_normal((1000, 100, 100))
1492+
y = rng.standard_normal(100)
15051493
15061494
with qe.Timer("For 循环操作"):
15071495
D = np.empty_like(x)
@@ -1528,4 +1516,4 @@ print(np.array_equal(B, D))
15281516
```
15291517

15301518
```{solution-end}
1531-
```
1519+
```

0 commit comments

Comments
 (0)