Skip to content

Commit 3cb250b

Browse files
committed
Update translation: lectures/python_by_example.md
1 parent 4d9ab6e commit 3cb250b

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

lectures/python_by_example.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ translation:
5353

5454
پیش از شروع این درس، باید جلسه ی قبل را مطالعه کرده باشید.
5555

56-
5756
## هدف:رسم یک فرآیند نویز سفید
5857

5958
فرض کنید می‌خواهیم فرآیند نویز سفید $\epsilon_0, \epsilon_1, \ldots, \epsilon_T$ را شبیه‌سازی و رسم کنیم، که در آن هر نقطه $\epsilon_t$ مستقل و نرمال استاندارد است.
@@ -77,7 +76,8 @@ translation:
7776
import numpy as np
7877
import matplotlib.pyplot as plt
7978
80-
ϵ_values = np.random.randn(100)
79+
rng = np.random.default_rng()
80+
ϵ_values = rng.standard_normal(100)
8181
plt.plot(ϵ_values)
8282
plt.show()
8383
```
@@ -111,7 +111,6 @@ np.sqrt(4)
111111
np.log(4)
112112
```
113113

114-
115114
#### چرا در برنامه نویسی پایتون، از Importهای متعددی استفاده می شود؟
116115

117116
برنامه های پایتون معمولا به چندین دستور `import` نیاز دارند.
@@ -120,7 +119,6 @@ np.log(4)
120119

121120
وقتی بخواهید کارهای جالب تر و پیشرفته تری با پایتون انجام دهید، تقریبا همیشه باید قابلیت های اضافی را با استفاده از `import` به برنامه اضافه کنید.
122121

123-
124122
#### پکیج ها(Packages)
125123

126124
```{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

@@ -191,7 +189,7 @@ sqrt(4)
191189
بیاید به کدی که نویز سفید رسم می کند برگردیم. سه خط بعد از دستور Import به این صورت هستند:
192190

193191
```{code-cell} ipython
194-
ϵ_values = np.random.randn(100)
192+
ϵ_values = rng.standard_normal(100)
195193
plt.plot(ϵ_values)
196194
plt.show()
197195
```
@@ -217,7 +215,7 @@ ts_length = 100
217215
ϵ_values = [] # empty list
218216
219217
for i in range(ts_length):
220-
e = np.random.randn()
218+
e = rng.standard_normal()
221219
ϵ_values.append(e)
222220
223221
plt.plot(ϵ_values)
@@ -313,7 +311,7 @@ x[1] # second element of x
313311

314312
```{code-cell} python3
315313
for i in range(ts_length):
316-
e = np.random.randn()
314+
e = rng.standard_normal()
317315
ϵ_values.append(e)
318316
```
319317

@@ -346,7 +344,6 @@ for variable_name in sequence:
346344

347345
* برای هر عنصر از دنباله `sequence`، نام متغیر `variable_name` را به آن عنصر متصل (blind) می کند و سپس بلوک کد را اجرا می کند.
348346

349-
350347
### یادداشتی درباره تورفتگی (Indentation)
351348

352349
```{index} single: Python; Indentation
@@ -393,7 +390,7 @@ ts_length = 100
393390
ϵ_values = []
394391
i = 0
395392
while i < ts_length:
396-
e = np.random.randn()
393+
e = rng.standard_normal()
397394
ϵ_values.append(e)
398395
i = i + 1
399396
plt.plot(ϵ_values)
@@ -495,9 +492,10 @@ import matplotlib.pyplot as plt
495492
T = 200
496493
x = np.empty(T+1)
497494
x[0] = 0
495+
rng = np.random.default_rng()
498496
499497
for t in range(T):
500-
x[t+1] = α * x[t] + np.random.randn()
498+
x[t+1] = α * x[t] + rng.standard_normal()
501499
502500
plt.plot(x)
503501
plt.show()
@@ -536,11 +534,12 @@ plt.show()
536534
α_values = [0.0, 0.8, 0.98]
537535
T = 200
538536
x = np.empty(T+1)
537+
rng = np.random.default_rng()
539538
540539
for α in α_values:
541540
x[0] = 0
542541
for t in range(T):
543-
x[t+1] = α * x[t] + np.random.randn()
542+
x[t+1] = α * x[t] + rng.standard_normal()
544543
plt.plot(x, label=f'$\\alpha = {α}$')
545544
546545
plt.legend()
@@ -588,9 +587,10 @@ $$
588587
T = 200
589588
x = np.empty(T+1)
590589
x[0] = 0
590+
rng = np.random.default_rng()
591591
592592
for t in range(T):
593-
x[t+1] = α * np.abs(x[t]) + np.random.randn()
593+
x[t+1] = α * np.abs(x[t]) + rng.standard_normal()
594594
595595
plt.plot(x)
596596
plt.show()
@@ -639,13 +639,14 @@ for x in numbers:
639639
T = 200
640640
x = np.empty(T+1)
641641
x[0] = 0
642+
rng = np.random.default_rng()
642643
643644
for t in range(T):
644645
if x[t] < 0:
645646
abs_x = - x[t]
646647
else:
647648
abs_x = x[t]
648-
x[t+1] = α * abs_x + np.random.randn()
649+
x[t+1] = α * abs_x + rng.standard_normal()
649650
650651
plt.plot(x)
651652
plt.show()
@@ -658,10 +659,11 @@ plt.show()
658659
T = 200
659660
x = np.empty(T+1)
660661
x[0] = 0
662+
rng = np.random.default_rng()
661663
662664
for t in range(T):
663665
abs_x = - x[t] if x[t] < 0 else x[t]
664-
x[t+1] = α * abs_x + np.random.randn()
666+
x[t+1] = α * abs_x + rng.standard_normal()
665667
666668
plt.plot(x)
667669
plt.show()
@@ -721,12 +723,13 @@ import numpy as np
721723

722724
```{code-cell} python3
723725
n = 1000000 # sample size for Monte Carlo simulation
726+
rng = np.random.default_rng()
724727
725728
count = 0
726729
for i in range(n):
727730
728731
# drawing random positions on the square
729-
u, v = np.random.uniform(), np.random.uniform()
732+
u, v = rng.uniform(), rng.uniform()
730733
731734
# check whether the point falls within the boundary
732735
# of the unit circle centred at (0.5,0.5)
@@ -743,4 +746,4 @@ print(area_estimate * 4) # dividing by radius**2
743746
```
744747

745748
```{solution-end}
746-
```
749+
```

0 commit comments

Comments
 (0)