Skip to content

Commit 4d6a70c

Browse files
committed
[add] numpy basic operations
1 parent 1d2a89a commit 4d6a70c

File tree

3 files changed

+300
-440
lines changed

3 files changed

+300
-440
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ All skills are base on the implementation of Python 3.
164164
<tr><th>Numpy</th></tr>
165165
<td><ul style="margin: 8px">
166166
<li><a href="#create-array-or-matrix">Create Array or Matrix</li>
167+
<li><a href="#basic-operations">Basic Operations</li>
167168
</ul>
168169
</table>
169170

@@ -1369,6 +1370,63 @@ rng.normal(3, 2.5, size=(2, 4)) # sample from N(3, 6.25)
13691370
rng.integers(low=2, high=10, size=(10, 2)) # random integer matrix
13701371
```
13711372

1373+
## [Basic Operations](numpy/basic_operations.ipynb)
1374+
1375+
### Element-wise
1376+
1377+
``` py
1378+
a = np.arange(5) # [0, 1, 2, 3, 4]
1379+
b = np.ones(5, dtype=int) # [1, 1, 1, 1, 1]
1380+
1381+
a + b # [1 2 3 4 5]
1382+
a - b # [-1 0 1 2 3]
1383+
a ^ 2 # [ 0 1 4 9 16]
1384+
a * 10 # [ 0 10 20 30 40]
1385+
a > 2 # [False False False True True]
1386+
np.sqrt(a) # [0. , 1. , 1.41421356, 1.73205081, 2. ]
1387+
a*b # [0 1 2 3 4]
1388+
a@b # 10
1389+
```
1390+
1391+
### All (None) Column-wise (0), Row-wise (1)
1392+
1393+
``` py
1394+
A = np.random.default_rng(42).random((2, 4))
1395+
# [[0.77395605, 0.43887844, 0.85859792, 0.69736803],
1396+
# [0.09417735, 0.97562235, 0.7611397 , 0.78606431]])
1397+
1398+
A.max() # 0.97562235
1399+
A.max(axis=0) # [0.77395605, 0.97562235, 0.85859792, 0.78606431]
1400+
A.max(axis=1) # [0.85859792, 0.97562235]
1401+
1402+
A.mean() # 0.6732255180088094
1403+
A.mean(axis=0) # [0.4340667 , 0.7072504 , 0.80986881, 0.74171617]
1404+
A.mean(axis=1) # [0.69220011, 0.65425093]
1405+
```
1406+
1407+
## Indexing, Slicing, and Iterating
1408+
1409+
https://numpy.org/doc/stable/user/quickstart.html#indexing-slicing-and-iterating
1410+
https://numpy.org/doc/stable/user/quickstart.html#advanced-indexing-and-index-tricks
1411+
https://numpy.org/doc/stable/user/basics.indexing.html
1412+
1413+
## Shape Manipulation
1414+
1415+
https://numpy.org/doc/stable/user/quickstart.html#shape-manipulation
1416+
https://numpy.org/doc/stable/user/quickstart.html#automatic-reshaping
1417+
https://numpy.org/doc/stable/user/quickstart.html#vector-stacking
1418+
1419+
## Copying
1420+
1421+
https://numpy.org/doc/stable/user/quickstart.html#copies-and-views
1422+
1423+
## Broadcasting
1424+
1425+
https://numpy.org/doc/stable/user/quickstart.html#less-basic
1426+
https://numpy.org/doc/stable/user/basics.broadcasting.html
1427+
1428+
1429+
13721430
# TODOs
13731431

13741432
| TODOs |

numpy/basic_operations.ipynb

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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]\nb = [1 1 1 1 1]\na+b = [1 2 3 4 5]\na-b = [-1 0 1 2 3]\na^2 = [ 0 1 4 9 16]\na*10 = [ 0 10 20 30 40]\na>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]\nb = [1 1 1 1 1]\na*b = [0 1 2 3 4]\na@b = 10\na.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]]\nd:\n[[1 7]\n [6 4]]\n\nc*d=\n[[ 1 14]\n [18 16]]\n\nc@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\nm.sum(): 5.385804144070475\nm.sum(axis=0): [0.8681334 1.41450079 1.61973762 1.48343233]\nm.sum(axis=1): [2.76880044 2.61700371]\n\nm.min() 0.09417734788764953\nm.min(axis=0) [0.09417735 0.43887844 0.7611397 0.69736803]\nm.min(axis=1) [0.43887844 0.09417735]\n\nm.max() 0.9756223516367559\nm.max(axis=0) [0.77395605 0.97562235 0.85859792 0.78606431]\nm.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]\nnp.sqrt(A) = [0. 1. 1.41421356 1.73205081]\nnp.exp(A) = [ 1. 2.71828183 7.3890561 20.08553692]\nnp.mean(A) = 1.5\nnp.median(A) = 1.5\nnp.all(A) = False\nnp.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

Comments
 (0)