You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lectures/python_by_example.md
+20-17Lines changed: 20 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,6 @@ translation:
53
53
54
54
پیش از شروع این درس، باید جلسه ی قبل را مطالعه کرده باشید.
55
55
56
-
57
56
## هدف:رسم یک فرآیند نویز سفید
58
57
59
58
فرض کنید میخواهیم فرآیند نویز سفید $\epsilon_0, \epsilon_1, \ldots, \epsilon_T$ را شبیهسازی و رسم کنیم، که در آن هر نقطه $\epsilon_t$ مستقل و نرمال استاندارد است.
@@ -77,7 +76,8 @@ translation:
77
76
import numpy as np
78
77
import matplotlib.pyplot as plt
79
78
80
-
ϵ_values = np.random.randn(100)
79
+
rng = np.random.default_rng()
80
+
ϵ_values = rng.standard_normal(100)
81
81
plt.plot(ϵ_values)
82
82
plt.show()
83
83
```
@@ -111,7 +111,6 @@ np.sqrt(4)
111
111
np.log(4)
112
112
```
113
113
114
-
115
114
#### چرا در برنامه نویسی پایتون، از Importهای متعددی استفاده می شود؟
116
115
117
116
برنامه های پایتون معمولا به چندین دستور `import` نیاز دارند.
@@ -120,7 +119,6 @@ np.log(4)
120
119
121
120
وقتی بخواهید کارهای جالب تر و پیشرفته تری با پایتون انجام دهید، تقریبا همیشه باید قابلیت های اضافی را با استفاده از `import` به برنامه اضافه کنید.
122
121
123
-
124
122
#### پکیج ها(Packages)
125
123
126
124
```{index} single: Python; Packages
@@ -155,7 +153,7 @@ print(np.__file__)
155
153
```{index} single: Python; Subpackages
156
154
```
157
155
158
-
به خط حاوی `ϵ_values = np.random.randn(100)` در کد توجه کنید.
156
+
به خط حاوی `rng = np.random.default_rng()` در کد توجه کنید.
159
157
160
158
در این دستور، `np` به پکیج NumPY اشاره دارد، و `random` یک زیرپکیج از NumPY است.
161
159
@@ -191,7 +189,7 @@ sqrt(4)
191
189
بیاید به کدی که نویز سفید رسم می کند برگردیم. سه خط بعد از دستور Import به این صورت هستند:
192
190
193
191
```{code-cell} ipython
194
-
ϵ_values = np.random.randn(100)
192
+
ϵ_values = rng.standard_normal(100)
195
193
plt.plot(ϵ_values)
196
194
plt.show()
197
195
```
@@ -217,7 +215,7 @@ ts_length = 100
217
215
ϵ_values = [] # empty list
218
216
219
217
for i in range(ts_length):
220
-
e = np.random.randn()
218
+
e = rng.standard_normal()
221
219
ϵ_values.append(e)
222
220
223
221
plt.plot(ϵ_values)
@@ -313,7 +311,7 @@ x[1] # second element of x
313
311
314
312
```{code-cell} python3
315
313
for i in range(ts_length):
316
-
e = np.random.randn()
314
+
e = rng.standard_normal()
317
315
ϵ_values.append(e)
318
316
```
319
317
@@ -346,7 +344,6 @@ for variable_name in sequence:
346
344
347
345
* برای هر عنصر از دنباله `sequence`، نام متغیر `variable_name` را به آن عنصر متصل (blind) می کند و سپس بلوک کد را اجرا می کند.
348
346
349
-
350
347
### یادداشتی درباره تورفتگی (Indentation)
351
348
352
349
```{index} single: Python; Indentation
@@ -393,7 +390,7 @@ ts_length = 100
393
390
ϵ_values = []
394
391
i = 0
395
392
while i < ts_length:
396
-
e = np.random.randn()
393
+
e = rng.standard_normal()
397
394
ϵ_values.append(e)
398
395
i = i + 1
399
396
plt.plot(ϵ_values)
@@ -495,9 +492,10 @@ import matplotlib.pyplot as plt
495
492
T = 200
496
493
x = np.empty(T+1)
497
494
x[0] = 0
495
+
rng = np.random.default_rng()
498
496
499
497
for t in range(T):
500
-
x[t+1] = α * x[t] + np.random.randn()
498
+
x[t+1] = α * x[t] + rng.standard_normal()
501
499
502
500
plt.plot(x)
503
501
plt.show()
@@ -536,11 +534,12 @@ plt.show()
536
534
α_values = [0.0, 0.8, 0.98]
537
535
T = 200
538
536
x = np.empty(T+1)
537
+
rng = np.random.default_rng()
539
538
540
539
for α in α_values:
541
540
x[0] = 0
542
541
for t in range(T):
543
-
x[t+1] = α * x[t] + np.random.randn()
542
+
x[t+1] = α * x[t] + rng.standard_normal()
544
543
plt.plot(x, label=f'$\\alpha = {α}$')
545
544
546
545
plt.legend()
@@ -588,9 +587,10 @@ $$
588
587
T = 200
589
588
x = np.empty(T+1)
590
589
x[0] = 0
590
+
rng = np.random.default_rng()
591
591
592
592
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()
594
594
595
595
plt.plot(x)
596
596
plt.show()
@@ -639,13 +639,14 @@ for x in numbers:
639
639
T = 200
640
640
x = np.empty(T+1)
641
641
x[0] = 0
642
+
rng = np.random.default_rng()
642
643
643
644
for t in range(T):
644
645
if x[t] < 0:
645
646
abs_x = - x[t]
646
647
else:
647
648
abs_x = x[t]
648
-
x[t+1] = α * abs_x + np.random.randn()
649
+
x[t+1] = α * abs_x + rng.standard_normal()
649
650
650
651
plt.plot(x)
651
652
plt.show()
@@ -658,10 +659,11 @@ plt.show()
658
659
T = 200
659
660
x = np.empty(T+1)
660
661
x[0] = 0
662
+
rng = np.random.default_rng()
661
663
662
664
for t in range(T):
663
665
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()
665
667
666
668
plt.plot(x)
667
669
plt.show()
@@ -721,12 +723,13 @@ import numpy as np
721
723
722
724
```{code-cell} python3
723
725
n = 1000000 # sample size for Monte Carlo simulation
726
+
rng = np.random.default_rng()
724
727
725
728
count = 0
726
729
for i in range(n):
727
730
728
731
# drawing random positions on the square
729
-
u, v = np.random.uniform(), np.random.uniform()
732
+
u, v = rng.uniform(), rng.uniform()
730
733
731
734
# check whether the point falls within the boundary
0 commit comments