11_Numpy_Matplotlib-reseni
11_Numpy_Matplotlib-reseni
January 6, 2021
1 NumPy
• de facto standard pro numerické výpočty v Pythonu
• velké množství dalších modulů postavených nad NumPy (SciPy, scikit-learn, pandas, …)
[6]: a.dtype
[6]: dtype('int64')
[15]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
[16]: np.random.sample(10)
1
[16]: array([0.79001315, 0.65122915, 0.55360169, 0.56902461, 0.95363964,
0.66821891, 0.68745137, 0.82941886, 0.10186436, 0.69028409])
[18]: len(a)
[18]: 10
[19]: (0, 9)
Operace se provádějí nad celým polem, není nutné používat for cyklus.
[21]: a = list(range(10))
a
[21]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[22]: [x + 1 for x in a]
[23]: b = np.arange(10)
b
[24]: b + 1
[25]: b ** 2
2
1.3 Vícerozměrná pole
[26]: a = np.arange(25)
a
[27]: a.shape
[27]: (25,)
[28]: b = a.reshape(5, 5)
b
[29]: b.shape
[29]: (5, 5)
[176]: b[0]
[176]: array([91, 83, 1, 10, 20, 73, 48, 84, 57, 63])
[34]: b[0, 3]
[34]: 3
[36]: b[0, :]
[37]: b[:, 2]
[38]: b[3:6, 2]
[39]: np.zeros(10)
[39]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
3
[41]: np.zeros((3, 3), dtype=int)
[45]: a[:, 1] = 4
a
[46]: a.T
[48]: np.eye(4)
4
array([[ 3, 1, 2],
[ 3, 7, 5],
[ 6, 7, 11]])
d)
array([3, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8])
Všechny zmíněné funkce mají parametr axis, který určuje, zda provést funkci přes řádky nebo
sloupce.
[61]: np.sum(a, axis=0)
5
je ekvivalentní: [ ] [ ] [ ]
1 1 x 1
· =
2 −1 y 2
[67]: x = np.linalg.solve(A, b)
x
[68]: A @ x
[71]: np.linalg.inv(A) @ b
6
[84]: plt.grid()
plt.plot(xs, np.sin(xs), '-o', color='red')
7
[90]: plt.grid()
plt.xlim(-1, 11)
plt.ylim(-2, 2)
plt.title('Goniometrické funkce')
plt.plot(xs, np.sin(xs), label='$y = \sin{x}$')
plt.plot(xs, np.cos(xs), label='$y = \cos{x}$')
plt.legend()
plt.savefig('image.png')
8
1.7.2 Histogram
[104]: (array([10., 1., 4., 5., 4., 5., 4., 7., 7., 7., 4., 7., 8.,
6., 4., 3., 2., 4., 5., 3.]),
array([0.01256698, 0.06128679, 0.1100066 , 0.15872641, 0.20744622,
0.25616603, 0.30488584, 0.35360565, 0.40232546, 0.45104528,
0.49976509, 0.5484849 , 0.59720471, 0.64592452, 0.69464433,
0.74336414, 0.79208395, 0.84080376, 0.88952357, 0.93824339,
0.9869632 ]),
<BarContainer object of 20 artists>)
9
1.7.3 Scatter plot
[113]: xs = np.random.sample(50)
ys = np.random.sample(50)
sizes = np.random.randint(200, size=50)
colors = np.random.randint(3, size=50)
[109]: colors
[109]: array([1, 2, 0, 2, 0, 0, 0, 2, 1, 1, 0, 2, 0, 1, 2, 0, 2, 0, 1, 1, 1, 2,
1, 1, 2, 0, 0, 2, 1, 1, 2, 2, 0, 2, 0, 1, 0, 0, 1, 0, 2, 1, 0, 2,
0, 0, 2, 2, 2, 1])
10
1.8 Otázka: Jsou tyto příkady ekvivalentní?
xs, ys = np.random.sample(10), np.random.sample(10)
1)
for x, y in zip(xs, ys):
plt.scatter(x, y)
2)
plt.scatter(xs, ys)
[119]: xs, ys = np.random.sample(10), np.random.sample(10)
11
[124]: plt.scatter(xs, ys, color='red')
12
1.9 NumPy - masky
[125]: a = np.random.randint(100, size=16).reshape(4, 4)
a
[129]: a[mask]
[129]: array([41, 75, 46, 46, 45, 62, 37, 70, 51, 95, 85, 97, 82])
[130]: array([41, 75, 46, 46, 45, 62, 37, 70, 51, 95, 85, 97, 82])
[131]: a[~mask]
[132]: xs = np.arange(100)
ys = np.random.sample(100)
plt.scatter(xs, ys)
13
[135]: mask = xs < 50
plt.scatter(xs[mask], ys[mask])
plt.scatter(xs[~mask], ys[~mask])
14
[136]: for threshold in np.linspace(0, 1, 6):
mask = (ys > threshold) & (ys < threshold + 0.2)
plt.scatter(xs[mask], ys[mask])
[154]: xs = np.random.sample(100000) * 2 - 1
ys = np.random.sample(100000) * 2 - 1
15
Jednotkový kruh je množina bodů, pro které platí x2 + y 2 ≤ 1.
[155]: mask = xs ** 2 + ys ** 2 <= 1
16
počet bodů (2r)2 4r2 4
=
˜ = =
počet bodů v kruhu πr 2 πr 2 π
[157]: 3.14304
17
[158]: a = np.random.randint(100, size=50).reshape(5, 10)
a
[158]: array([[91, 83, 1, 10, 20, 73, 48, 84, 57, 63],
[ 5, 22, 26, 3, 55, 31, 32, 31, 36, 85],
[ 6, 85, 85, 74, 35, 67, 19, 91, 16, 85],
[92, 92, 17, 57, 30, 39, 85, 26, 74, 84],
[80, 52, 60, 95, 48, 78, 96, 21, 59, 99]])
[164]: array([[91, 83, 1, 10, 20, 73, 48, 84, 57, 63],
[ 5, 22, 26, 3, 55, 31, 32, 31, 36, 85],
[ 6, 85, 85, 74, 35, 67, 19, 91, 16, 85],
[92, 92, 17, 57, 30, 39, 85, 26, 74, 84],
[80, 52, 60, 95, 48, 78, 96, 21, 59, 99]])
[165]: np.save('data.npy', a)
[167]: b = np.load('data.npy')
b
[167]: array([[91, 83, 1, 10, 20, 73, 48, 84, 57, 63],
[ 5, 22, 26, 3, 55, 31, 32, 31, 36, 85],
[ 6, 85, 85, 74, 35, 67, 19, 91, 16, 85],
[92, 92, 17, 57, 30, 39, 85, 26, 74, 84],
[80, 52, 60, 95, 48, 78, 96, 21, 59, 99]])
235 µs ± 1.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
[169]: %%timeit
np.arange(1000) ** 2
2.31 µs ± 22.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
18
1.11.1 Součet dvou seznamů/polí
191 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
[172]: %%timeit
c_py = []
for x, y in zip(a_py, b_py):
c_py.append(x + y)
143 ms ± 583 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
[174]: %%timeit
c_py = [x + y for x, y in zip(a_py, b_py)]
117 ms ± 763 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
NumPy
[175]: %%timeit
c_np = a_np + b_np
920 µs ± 5.48 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
19