GitHub

Original file line numberDiff line numberDiff line change

@@ -1,6 +1,6 @@

1-

source-sha: 1b356ab5e60d8f6939446fd5f47c243593a2abd3

2-

synced-at: "2026-05-04"

3-

model: claude-sonnet-4-6

1+

source-sha: aacff3815993e8bea8db7b63df72a0a038e220c1

2+

synced-at: "2026-08-05"

3+

model: claude-sonnet-5

44

mode: UPDATE

55

section-count: 7

6-

tool-version: 0.14.1

6+

tool-version: 0.25.0

Original file line numberDiff line numberDiff line change

@@ -1,6 +1,6 @@

1-

source-sha: a2b929f15e703b6942e8b80a29011c51f234b1e0

2-

synced-at: "2026-05-13"

3-

model: claude-sonnet-4-6

1+

source-sha: aacff3815993e8bea8db7b63df72a0a038e220c1

2+

synced-at: "2026-08-05"

3+

model: claude-sonnet-5

44

mode: UPDATE

55

section-count: 8

6-

tool-version: 0.15.0

6+

tool-version: 0.25.0

Original file line numberDiff line numberDiff line change

@@ -1,6 +1,6 @@

1-

source-sha: 811accdd4ed8803df3a7123ada3b560bc3110712

2-

synced-at: "2026-06-19"

3-

model: claude-sonnet-4-6

1+

source-sha: aacff3815993e8bea8db7b63df72a0a038e220c1

2+

synced-at: "2026-08-05"

3+

model: claude-sonnet-5

44

mode: UPDATE

55

section-count: 5

6-

tool-version: 0.15.0

6+

tool-version: 0.25.0

Original file line numberDiff line numberDiff line change

@@ -1,6 +1,6 @@

1-

source-sha: 3241a07f00b1fb4d2bebc6989d1f480847e08e31

2-

synced-at: "2026-04-25"

3-

model: claude-sonnet-4-6

1+

source-sha: aacff3815993e8bea8db7b63df72a0a038e220c1

2+

synced-at: "2026-08-05"

3+

model: claude-sonnet-5

44

mode: UPDATE

55

section-count: 6

6-

tool-version: 0.14.1

6+

tool-version: 0.25.0

Original file line numberDiff line numberDiff line change

@@ -292,7 +292,6 @@ quad(lambda x: x**3, 0, 2)

292292
293293

## 应用

294294
295-
296295

### 随机抽取

297296
298297

再次考虑 {doc}`前一讲 <python_by_example>` 中的以下代码

@@ -465,7 +464,7 @@ def x(t):

465464
466465

* 帧是存储给定函数调用的局部变量的地方

467466

* 栈是用于处理函数调用的内存

468-

* 一个先进后出(FILO)队列

467+

* 一种后进先出(LIFO)数据结构

469468
470469

这个例子有些刻意,因为第一个(迭代)解通常比递归解更受欢迎。

471470
Original file line numberDiff line numberDiff line change

@@ -1263,7 +1263,7 @@ class DiscreteRV:

12631263
12641264

然而,这里有一个问题。

12651265
1266-

假设在创建 `discreteRV` 实例后修改了 `q`,例如

1266+

假设在创建 `DiscreteRV` 实例后修改了 `q`,例如

12671267
12681268

```{code-cell} python3

12691269

q = (0.1, 0.9)

@@ -1394,7 +1394,7 @@ F.plot(ax)

13941394
13951395

在本练习中,尝试使用 `for` 循环来复现以下广播操作的结果。

13961396
1397-

**第一部分**:尝试使用 `for` 循环复现以下简单示例,并将你的结果与下面的广播操作进行比较。

1397+

**第 1 部分**:尝试使用 `for` 循环复现以下简单示例,并将你的结果与下面的广播操作进行比较。

13981398
13991399

```{code-cell} python3

14001400

@@ -1413,7 +1413,7 @@ tags: [hide-output]

14131413

print(A)

14141414

```

14151415
1416-

**第二部分**:继续复现以下广播操作的结果。同时,比较广播和你实现的 `for` 循环的速度。

1416+

**第 2 部分**:继续复现以下广播操作的结果。同时,比较广播和你实现的 `for` 循环的速度。

14171417
14181418

对于本练习的这一部分,你可以使用 `quantecon` 库中的 `tic`/`toc` 函数来计时执行。

14191419

@@ -1453,7 +1453,7 @@ print(B)

14531453

:class: dropdown

14541454

```

14551455
1456-

**第一部分解答**

1456+

**第 1 部分解答**

14571457
14581458

```{code-cell} python3

14591459

rng = np.random.default_rng(123)

@@ -1483,7 +1483,7 @@ print(np.array_equal(A, C))

14831483

```

14841484
14851485
1486-

**第二部分解答**

1486+

**第 2 部分解答**

14871487
14881488

```{code-cell} python3

14891489
Original file line numberDiff line numberDiff line change

@@ -391,7 +391,7 @@ df.apply(update_row, axis=1)

391391
392392

```{code-cell} ipython3

393393

# 将所有小数四舍五入到2位小数

394-

df.map(lambda x : round(x,2) if type(x)!=str else x)

394+

df.map(lambda x : round(x,2) if not isinstance(x, str) else x)

395395

```

396396
397397

**应用:缺失值插补**

@@ -414,8 +414,8 @@ df

414414

```{code-cell} ipython3

415415

# 将所有NaN值替换为0

416416

def replace_nan(x):

417-

if type(x)!=str:

418-

return 0 if np.isnan(x) else x

417+

if not isinstance(x, str):

418+

return 0 if pd.isna(x) else x

419419

else:

420420

return x

421421

@@ -547,7 +547,7 @@ r = requests.get('https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e

547547

* 切换到另一台机器

548548

* 通过阅读[文档](https://requests.readthedocs.io/en/latest/)解决代理问题

549549
550-

假设一切正常,你现在可以使用调用 `requests.get('https://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv')` 返回的 `source` 对象继续操作

550+

假设一切正常,你现在可以根据调用 `requests.get(url)` 返回的数据来构建 `source` 对象

551551
552552

```{code-cell} ipython3

553553

url = 'https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12&tts=12&width=1318&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes&show_tooltip=yes&id=UNRATE&scale=left&cosd=1948-01-01&coed=2024-06-01&line_color=%234572a7&link_values=false&line_style=solid&mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Monthly&fam=avg&fgst=lin&fgsnd=2020-02-01&line_index=1&transformation=lin&vintage_date=2024-07-29&revision_date=2024-07-29&nd=1948-01-01'

Original file line numberDiff line numberDiff line change

@@ -391,9 +391,9 @@ plt.plot(ϵ_values)

391391

plt.show()

392392

```

393393
394-

while 循环将持续执行由缩进分隔的代码块,直到条件(```i < ts_length```)被满足。

394+

while 循环将持续执行由缩进分隔的代码块,只要条件(`i < ts_length`)被满足。

395395
396-

在本例中,程序将持续向列表 ```ϵ_values``` 添加值,直到 ```i``` 等于 ```ts_length```

396+

在本例中,程序将持续向列表 `ϵ_values` 添加值,直到 `i` 等于 `ts_length`

397397
398398

```{code-cell} python3

399399

i == ts_length # while 循环的终止条件

Read the original on github.com ↗