Skip to content

Commit ee7700a

Browse files
authored
🌐 [translation-sync] [python_by_example.md] Update np.random → Generator API (#59)
* Update translation: lectures/python_by_example.md * Update translation: .translate/state/python_by_example.md.yml
1 parent 86a5c13 commit ee7700a

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
source-sha: 1a87942398e15e03539083cc944a78653c532607
2-
synced-at: "2026-03-20"
1+
source-sha: 3241a07f00b1fb4d2bebc6989d1f480847e08e31
2+
synced-at: "2026-04-25"
33
model: claude-sonnet-4-6
4-
mode: NEW
4+
mode: UPDATE
55
section-count: 6
6-
tool-version: 0.13.0
6+
tool-version: 0.14.1

lectures/python_by_example.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ translation:
5555

5656
在开始本讲座之前,您应该已经阅读过关于 Python 入门的{doc}`讲座 <getting_started>`
5757

58-
5958
## 任务:绘制白噪声过程
6059

6160
假设我们想要模拟并绘制白噪声过程 $\epsilon_0, \epsilon_1, \ldots, \epsilon_T$,其中每个抽取值 $\epsilon_t$ 是独立的标准正态分布。
@@ -84,7 +83,8 @@ FONTPATH = "_fonts/SourceHanSerifSC-SemiBold.otf" # i18n
8483
mpl.font_manager.fontManager.addfont(FONTPATH) # i18n
8584
mpl.rcParams['font.family'] = ['Source Han Serif SC'] # i18n
8685
87-
ϵ_values = np.random.randn(100)
86+
rng = np.random.default_rng()
87+
ϵ_values = rng.standard_normal(100)
8888
plt.plot(ϵ_values)
8989
plt.show()
9090
```
@@ -115,7 +115,6 @@ np.sqrt(4)
115115
np.log(4)
116116
```
117117

118-
119118
#### 为什么需要这么多导入语句?
120119

121120
Python 程序通常需要多条导入语句。
@@ -124,7 +123,6 @@ Python 程序通常需要多条导入语句。
124123

125124
当您想用 Python 做一些有趣的事情时,几乎总是需要导入额外的功能。
126125

127-
128126
####
129127

130128
```{index} single: Python; Packages
@@ -155,7 +153,7 @@ print(np.__file__)
155153
```{index} single: Python; Subpackages
156154
```
157155

158-
考虑这行代码 `ϵ_values = np.random.randn(100)`
156+
考虑这行代码 `rng = np.random.default_rng()`
159157

160158
这里 `np` 指的是 NumPy 包,而 `random` 是 NumPy 的一个**子包**
161159

@@ -194,7 +192,7 @@ sqrt(4)
194192
回到我们绘制白噪声的程序,导入语句之后的剩余三行是
195193

196194
```{code-cell} ipython
197-
ϵ_values = np.random.randn(100)
195+
ϵ_values = rng.standard_normal(100)
198196
plt.plot(ϵ_values)
199197
plt.show()
200198
```
@@ -223,7 +221,7 @@ ts_length = 100
223221
ϵ_values = [] # 空列表
224222
225223
for i in range(ts_length):
226-
e = np.random.randn()
224+
e = rng.standard_normal()
227225
ϵ_values.append(e)
228226
229227
plt.plot(ϵ_values)
@@ -312,7 +310,7 @@ x[1] # x 的第二个元素
312310

313311
```{code-cell} python3
314312
for i in range(ts_length):
315-
e = np.random.randn()
313+
e = rng.standard_normal()
316314
ϵ_values.append(e)
317315
```
318316

@@ -345,7 +343,6 @@ Python 解释器执行以下操作:
345343

346344
* 对于 `sequence` 的每个元素,它将名称 `variable_name` "绑定"到该元素,然后执行代码块。
347345

348-
349346
### 关于缩进的说明
350347

351348
```{index} single: Python; Indentation
@@ -387,7 +384,7 @@ ts_length = 100
387384
ϵ_values = []
388385
i = 0
389386
while i < ts_length:
390-
e = np.random.randn()
387+
e = rng.standard_normal()
391388
ϵ_values.append(e)
392389
i = i + 1
393390
plt.plot(ϵ_values)
@@ -487,9 +484,10 @@ import matplotlib.pyplot as plt
487484
T = 200
488485
x = np.empty(T+1)
489486
x[0] = 0
487+
rng = np.random.default_rng()
490488
491489
for t in range(T):
492-
x[t+1] = α * x[t] + np.random.randn()
490+
x[t+1] = α * x[t] + rng.standard_normal()
493491
494492
plt.plot(x)
495493
plt.show()
@@ -527,11 +525,12 @@ plt.show()
527525
α_values = [0.0, 0.8, 0.98]
528526
T = 200
529527
x = np.empty(T+1)
528+
rng = np.random.default_rng()
530529
531530
for α in α_values:
532531
x[0] = 0
533532
for t in range(T):
534-
x[t+1] = α * x[t] + np.random.randn()
533+
x[t+1] = α * x[t] + rng.standard_normal()
535534
plt.plot(x, label=f'$\\alpha = {α}$')
536535
537536
plt.legend()
@@ -579,9 +578,10 @@ $$
579578
T = 200
580579
x = np.empty(T+1)
581580
x[0] = 0
581+
rng = np.random.default_rng()
582582
583583
for t in range(T):
584-
x[t+1] = α * np.abs(x[t]) + np.random.randn()
584+
x[t+1] = α * np.abs(x[t]) + rng.standard_normal()
585585
586586
plt.plot(x)
587587
plt.show()
@@ -631,13 +631,14 @@ for x in numbers:
631631
T = 200
632632
x = np.empty(T+1)
633633
x[0] = 0
634+
rng = np.random.default_rng()
634635
635636
for t in range(T):
636637
if x[t] < 0:
637638
abs_x = - x[t]
638639
else:
639640
abs_x = x[t]
640-
x[t+1] = α * abs_x + np.random.randn()
641+
x[t+1] = α * abs_x + rng.standard_normal()
641642
642643
plt.plot(x)
643644
plt.show()
@@ -650,10 +651,11 @@ plt.show()
650651
T = 200
651652
x = np.empty(T+1)
652653
x[0] = 0
654+
rng = np.random.default_rng()
653655
654656
for t in range(T):
655657
abs_x = - x[t] if x[t] < 0 else x[t]
656-
x[t+1] = α * abs_x + np.random.randn()
658+
x[t+1] = α * abs_x + rng.standard_normal()
657659
658660
plt.plot(x)
659661
plt.show()
@@ -670,7 +672,7 @@ plt.show()
670672

671673
这是一个需要一些思考和规划的较难练习。
672674

673-
任务是使用[蒙特卡洛](https://en.wikipedia.org/wiki/Monte_Carlo_method)方法计算 $\pi$ 的近似值。
675+
任务是使用 [蒙特卡洛](https://en.wikipedia.org/wiki/Monte_Carlo_method) 方法计算 $\pi$ 的近似值。
674676

675677
除以下内容外不使用其他导入语句
676678

@@ -710,12 +712,13 @@ import numpy as np
710712

711713
```{code-cell} python3
712714
n = 1000000 # 蒙特卡洛模拟的样本量
715+
rng = np.random.default_rng()
713716
714717
count = 0
715718
for i in range(n):
716719
717720
# 在正方形上随机抽取位置
718-
u, v = np.random.uniform(), np.random.uniform()
721+
u, v = rng.uniform(), rng.uniform()
719722
720723
# 检查该点是否落在以 (0.5,0.5) 为圆心的
721724
# 单位圆的边界内
@@ -732,4 +735,4 @@ print(area_estimate * 4) # 除以半径的平方
732735
```
733736

734737
```{solution-end}
735-
```
738+
```

0 commit comments

Comments
 (0)