1
+ {
2
+ "metadata" : {
3
+ "language_info" : {
4
+ "codemirror_mode" : {
5
+ "name" : " ipython" ,
6
+ "version" : 3
7
+ },
8
+ "file_extension" : " .py" ,
9
+ "mimetype" : " text/x-python" ,
10
+ "name" : " python" ,
11
+ "nbconvert_exporter" : " python" ,
12
+ "pygments_lexer" : " ipython3" ,
13
+ "version" : " 3.7.7-final"
14
+ },
15
+ "orig_nbformat" : 2 ,
16
+ "kernelspec" : {
17
+ "name" : " python3" ,
18
+ "display_name" : " Python 3.7.7 64-bit" ,
19
+ "metadata" : {
20
+ "interpreter" : {
21
+ "hash" : " 1005f71a2f2c9f7da6acb75e9bc0247674183f6cab85bd2a339bd069fc0cd207"
22
+ }
23
+ }
24
+ }
25
+ },
26
+ "nbformat" : 4 ,
27
+ "nbformat_minor" : 2 ,
28
+ "cells" : [
29
+ {
30
+ "source" : [
31
+ " # Numpy Basic Operations"
32
+ ],
33
+ "cell_type" : " markdown" ,
34
+ "metadata" : {}
35
+ },
36
+ {
37
+ "cell_type" : " code" ,
38
+ "execution_count" : 3 ,
39
+ "metadata" : {},
40
+ "outputs" : [],
41
+ "source" : [
42
+ " import numpy as np"
43
+ ]
44
+ },
45
+ {
46
+ "source" : [
47
+ " ## Artithemetics\n " ,
48
+ " \n " ,
49
+ " Arithmetic operators on arrays apply elementwise."
50
+ ],
51
+ "cell_type" : " markdown" ,
52
+ "metadata" : {}
53
+ },
54
+ {
55
+ "cell_type" : " code" ,
56
+ "execution_count" : 27 ,
57
+ "metadata" : {},
58
+ "outputs" : [
59
+ {
60
+ "output_type" : " stream" ,
61
+ "name" : " stdout" ,
62
+ "text" : [
63
+ " a = [0 1 2 3 4]\n b = [1 1 1 1 1]\n a+b = [1 2 3 4 5]\n a-b = [-1 0 1 2 3]\n a^2 = [ 0 1 4 9 16]\n a*10 = [ 0 10 20 30 40]\n a>2 = [False False False True True]\n "
64
+ ]
65
+ }
66
+ ],
67
+ "source" : [
68
+ " a = np.arange(5)\n " ,
69
+ " b = np.ones(5, dtype=int)\n " ,
70
+ " \n " ,
71
+ " print(f\" a = {a}\" , f\" b = {b}\" , sep=\"\\ n\" )\n " ,
72
+ " print(f\" a+b = {a+b}\" )\n " ,
73
+ " print(f\" a-b = {a-b}\" )\n " ,
74
+ " print(f\" a^2 = {a**2}\" )\n " ,
75
+ " print(f\" a*10 = {a*10}\" )\n " ,
76
+ " print(f\" a>2 = {a>2}\" )"
77
+ ]
78
+ },
79
+ {
80
+ "source" : [
81
+ " ## Product\n " ,
82
+ " \n " ,
83
+ " Unlike in many matrix languages, product operator `*` operates elementwise in numpy, the **dot product** or **matrix product** can be performed by the `@` operator or `dot()` method."
84
+ ],
85
+ "cell_type" : " markdown" ,
86
+ "metadata" : {}
87
+ },
88
+ {
89
+ "cell_type" : " code" ,
90
+ "execution_count" : 40 ,
91
+ "metadata" : {},
92
+ "outputs" : [
93
+ {
94
+ "output_type" : " stream" ,
95
+ "name" : " stdout" ,
96
+ "text" : [
97
+ " a = [0 1 2 3 4]\n b = [1 1 1 1 1]\n a*b = [0 1 2 3 4]\n a@b = 10\n a.dot(b) = 10\n "
98
+ ]
99
+ }
100
+ ],
101
+ "source" : [
102
+ " print(f\" a = {a}\" , f\" b = {b}\" , sep=\"\\ n\" )\n " ,
103
+ " print(f\" a*b = {a*b}\" )\n " ,
104
+ " print(f\" a@b = {a@b}\" )\n " ,
105
+ " print(f\" a.dot(b) = {a.dot(b)}\" )"
106
+ ]
107
+ },
108
+ {
109
+ "cell_type" : " code" ,
110
+ "execution_count" : 54 ,
111
+ "metadata" : {},
112
+ "outputs" : [
113
+ {
114
+ "output_type" : " stream" ,
115
+ "name" : " stdout" ,
116
+ "text" : [
117
+ " c:\n [[1 2]\n [3 4]]\n d:\n [[1 7]\n [6 4]]\n\n c*d=\n [[ 1 14]\n [18 16]]\n\n c@d=\n [[13 15]\n [27 37]]\n "
118
+ ]
119
+ }
120
+ ],
121
+ "source" : [
122
+ " c = np.array([[1, 2], [3, 4]])\n " ,
123
+ " d = np.random.default_rng(42).integers(1, 10, (2, 2))\n " ,
124
+ " \n " ,
125
+ " print(\" c:\" , c, \" d:\" , d, sep=\"\\ n\" )\n " ,
126
+ " print()\n " ,
127
+ " print(f\" c*d=\\ n{c*d}\" )\n " ,
128
+ " print()\n " ,
129
+ " print(f\" c@d=\\ n{c@d}\" )"
130
+ ]
131
+ },
132
+ {
133
+ "source" : [
134
+ " ## Upcasting\n " ,
135
+ " \n " ,
136
+ " **Upcasting** will automatically convert the different types to the precise one."
137
+ ],
138
+ "cell_type" : " markdown" ,
139
+ "metadata" : {}
140
+ },
141
+ {
142
+ "cell_type" : " code" ,
143
+ "execution_count" : 12 ,
144
+ "metadata" : {},
145
+ "outputs" : [
146
+ {
147
+ "output_type" : " stream" ,
148
+ "name" : " stdout" ,
149
+ "text" : [
150
+ " [1 1 1]\n [0. 1.57079633 3.14159265]\n [1. 2.57079633 4.14159265]\n "
151
+ ]
152
+ }
153
+ ],
154
+ "source" : [
155
+ " e = np.ones(3, dtype=np.int32)\n " ,
156
+ " f = np.linspace(0, np.pi, 3)\n " ,
157
+ " \n " ,
158
+ " print(e)\n " ,
159
+ " print(f)\n " ,
160
+ " print(e+f)"
161
+ ]
162
+ },
163
+ {
164
+ "source" : [
165
+ " ## Functions\n " ,
166
+ " \n " ,
167
+ " By specifying the axis parameter you can apply an operation along the specified axis of an array:\n " ,
168
+ " \n " ,
169
+ " - **None**: all elemnts in the array or matrix\n " ,
170
+ " - **0**: each columns\n " ,
171
+ " - **1**: each rows"
172
+ ],
173
+ "cell_type" : " markdown" ,
174
+ "metadata" : {}
175
+ },
176
+ {
177
+ "cell_type" : " code" ,
178
+ "execution_count" : 26 ,
179
+ "metadata" : {},
180
+ "outputs" : [
181
+ {
182
+ "output_type" : " stream" ,
183
+ "name" : " stdout" ,
184
+ "text" : [
185
+ " [[0.77395605 0.43887844 0.85859792 0.69736803]\n [0.09417735 0.97562235 0.7611397 0.78606431]]\n\n m.sum(): 5.385804144070475\n m.sum(axis=0): [0.8681334 1.41450079 1.61973762 1.48343233]\n m.sum(axis=1): [2.76880044 2.61700371]\n\n m.min() 0.09417734788764953\n m.min(axis=0) [0.09417735 0.43887844 0.7611397 0.69736803]\n m.min(axis=1) [0.43887844 0.09417735]\n\n m.max() 0.9756223516367559\n m.max(axis=0) [0.77395605 0.97562235 0.85859792 0.78606431]\n m.max(axis=1) [0.85859792 0.97562235]\n "
186
+ ]
187
+ }
188
+ ],
189
+ "source" : [
190
+ " m = np.random.default_rng(42).random((2, 4))\n " ,
191
+ " print(m)\n " ,
192
+ " print()\n " ,
193
+ " print(\" m.sum(): \" , m.sum())\n " ,
194
+ " print(\" m.sum(axis=0): \" , m.sum(axis=0))\n " ,
195
+ " print(\" m.sum(axis=1): \" , m.sum(axis=1))\n " ,
196
+ " print()\n " ,
197
+ " print(\" m.min()\" , m.min())\n " ,
198
+ " print(\" m.min(axis=0)\" , m.min(axis=0))\n " ,
199
+ " print(\" m.min(axis=1)\" , m.min(axis=1))\n " ,
200
+ " print()\n " ,
201
+ " print(\" m.max()\" , m.max())\n " ,
202
+ " print(\" m.max(axis=0)\" , m.max(axis=0))\n " ,
203
+ " print(\" m.max(axis=1)\" , m.max(axis=1))"
204
+ ]
205
+ },
206
+ {
207
+ "source" : [
208
+ " ### Universal Functions\n " ,
209
+ " \n " ,
210
+ " NumPy provides familiar mathematical functions such as **sin**, **cos**, and **exp** (These are called **universal functions**). They all operate elementwise on an array.\n " ,
211
+ " \n " ,
212
+ " > exp, sqrt, all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor, inner, invert, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var, vdot, vectorize, where"
213
+ ],
214
+ "cell_type" : " markdown" ,
215
+ "metadata" : {}
216
+ },
217
+ {
218
+ "cell_type" : " code" ,
219
+ "execution_count" : 46 ,
220
+ "metadata" : {},
221
+ "outputs" : [
222
+ {
223
+ "output_type" : " stream" ,
224
+ "name" : " stdout" ,
225
+ "text" : [
226
+ " A = [0 1 2 3]\n np.sqrt(A) = [0. 1. 1.41421356 1.73205081]\n np.exp(A) = [ 1. 2.71828183 7.3890561 20.08553692]\n np.mean(A) = 1.5\n np.median(A) = 1.5\n np.all(A) = False\n np.any(A) = True\n "
227
+ ]
228
+ }
229
+ ],
230
+ "source" : [
231
+ " A = np.arange(4)\n " ,
232
+ " print(f\" A = {A}\" )\n " ,
233
+ " print(f\" np.sqrt(A) = {np.sqrt(A)}\" )\n " ,
234
+ " print(f\" np.exp(A) = {np.exp(A)}\" )\n " ,
235
+ " print(f\" np.mean(A) = {np.mean(A)}\" )\n " ,
236
+ " print(f\" np.median(A) = {np.median(A)}\" )\n " ,
237
+ " print(f\" np.all(A) = {np.all(A)}\" )\n " ,
238
+ " print(f\" np.any(A) = {np.any(A)}\" )"
239
+ ]
240
+ }
241
+ ]
242
+ }
0 commit comments