Skip to content

Commit c246011

Browse files
committed
[add] numpy indexing and slicing
1 parent 4d6a70c commit c246011

File tree

3 files changed

+714
-5
lines changed

3 files changed

+714
-5
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ All skills are base on the implementation of Python 3.
165165
<td><ul style="margin: 8px">
166166
<li><a href="#create-array-or-matrix">Create Array or Matrix</li>
167167
<li><a href="#basic-operations">Basic Operations</li>
168+
<li><a href="#indexing-and-slicing">Indexing and Slicing</li>
169+
168170
</ul>
169171
</table>
170172

@@ -1372,6 +1374,18 @@ rng.integers(low=2, high=10, size=(10, 2)) # random integer matrix
13721374

13731375
## [Basic Operations](numpy/basic_operations.ipynb)
13741376

1377+
### Sort and Concatenate
1378+
1379+
``` py
1380+
np.sort(a, axis=None)
1381+
np.sort(a, axis=-1)[::-1]
1382+
a.sort()
1383+
a[::-1].sort()
1384+
1385+
np.concatenate((a, b), axis=None)
1386+
np.concatenate((a, b), axis=2)
1387+
```
1388+
13751389
### Element-wise
13761390

13771391
``` py
@@ -1404,11 +1418,31 @@ A.mean(axis=0) # [0.4340667 , 0.7072504 , 0.80986881, 0.74171617]
14041418
A.mean(axis=1) # [0.69220011, 0.65425093]
14051419
```
14061420

1407-
## Indexing, Slicing, and Iterating
1421+
## [Indexing and Slicing](numpy/indexing_slicing.ipynb)
1422+
1423+
``` py
1424+
# Index and slicing arrays
1425+
x[1, 3] == x[1][3]
1426+
y[1:5:2, ::3]
1427+
1428+
1429+
# Indexing arrays
1430+
x[np.array([0, 1, 2, -1, -2])]
1431+
y[np.array([1, 2, 3]), 1:4:2]
1432+
y[np.array([1, 2]), np.array([-1, -1])]
1433+
14081434

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
1435+
# Masking arrays
1436+
x[x>5]
1437+
x[(x%2==0) | (x>7)]
1438+
y[[True]*3 + [False] + [True] + [False], 2::2]
1439+
1440+
1441+
# Ellipsis syntax
1442+
x[-1, ..., 3] # same as x[-1, :, 3]
1443+
x[:3, ...] # same as x[0:3, :, :] and x[0:3] and x[:3]
1444+
x[::2, ..., np.array([0, 2])] # same as x[0:5:2, :, np.array([0, 2])]
1445+
```
14121446

14131447
## Shape Manipulation
14141448

numpy/basic_operations.ipynb

Lines changed: 244 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,244 @@
3535
},
3636
{
3737
"cell_type": "code",
38-
"execution_count": 3,
38+
"execution_count": 2,
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
4242
"import numpy as np"
4343
]
4444
},
45+
{
46+
"source": [
47+
"## Sort"
48+
],
49+
"cell_type": "markdown",
50+
"metadata": {}
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 23,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"a = np.arange(5)\n",
59+
"a = np.sort(a) # not an in-place sorting\n",
60+
"# [0, 1, 2, 3, 4]\n",
61+
"\n",
62+
"a[::-1].sort() # perform in-place sorting\n",
63+
"# [4, 3, 2, 1, 0]"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 38,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"output_type": "execute_result",
73+
"data": {
74+
"text/plain": [
75+
"array([[[0, 7, 6, 4],\n",
76+
" [4, 8, 0, 6],\n",
77+
" [2, 0, 5, 9]],\n",
78+
"\n",
79+
" [[7, 7, 7, 7],\n",
80+
" [5, 1, 8, 4],\n",
81+
" [5, 3, 1, 9]]], dtype=int64)"
82+
]
83+
},
84+
"metadata": {},
85+
"execution_count": 38
86+
}
87+
],
88+
"source": [
89+
"a = np.random.default_rng(42).integers(0, 10, (2,3,4))\n",
90+
"a"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 40,
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"output_type": "execute_result",
100+
"data": {
101+
"text/plain": [
102+
"array([[[0, 4, 6, 7],\n",
103+
" [0, 4, 6, 8],\n",
104+
" [0, 2, 5, 9]],\n",
105+
"\n",
106+
" [[7, 7, 7, 7],\n",
107+
" [1, 4, 5, 8],\n",
108+
" [1, 3, 5, 9]]], dtype=int64)"
109+
]
110+
},
111+
"metadata": {},
112+
"execution_count": 40
113+
}
114+
],
115+
"source": [
116+
"np.sort(a, axis=-1) # default: sort on last (axis=2 in this case) dimension"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 45,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"output_type": "execute_result",
126+
"data": {
127+
"text/plain": [
128+
"array([[[0, 0, 0, 4],\n",
129+
" [2, 7, 5, 6],\n",
130+
" [4, 8, 6, 9]],\n",
131+
"\n",
132+
" [[5, 1, 1, 4],\n",
133+
" [5, 3, 7, 7],\n",
134+
" [7, 7, 8, 9]]], dtype=int64)"
135+
]
136+
},
137+
"metadata": {},
138+
"execution_count": 45
139+
}
140+
],
141+
"source": [
142+
"np.sort(a, axis=1) # sort on the second demension"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 47,
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"output_type": "execute_result",
152+
"data": {
153+
"text/plain": [
154+
"array([9, 9, 8, 8, 7, 7, 7, 7, 7, 6, 6, 5, 5, 5, 4, 4, 4, 3, 2, 1, 1, 0,\n",
155+
" 0, 0], dtype=int64)"
156+
]
157+
},
158+
"metadata": {},
159+
"execution_count": 47
160+
}
161+
],
162+
"source": [
163+
"np.sort(a, axis=None)[::-1] # sort on all elements and flatten"
164+
]
165+
},
166+
{
167+
"source": [
168+
"Note: The `axis` uses in `np.sort()` is not as in `max()`, `mean()` and other functions.\n",
169+
"\n",
170+
"- `axis=None` will sort the arrays into **flattened** arrays.\n",
171+
"- `axis=n` will sort the arrays on the `n-th` dimension.\n",
172+
" - e.g., `np.sort((3, 3, 1), axis=2)` will sort the elements on the last dimension. "
173+
],
174+
"cell_type": "markdown",
175+
"metadata": {}
176+
},
177+
{
178+
"source": [
179+
"## Concatenate"
180+
],
181+
"cell_type": "markdown",
182+
"metadata": {}
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 9,
187+
"metadata": {},
188+
"outputs": [
189+
{
190+
"output_type": "execute_result",
191+
"data": {
192+
"text/plain": [
193+
"(array([[2, 1]]),\n",
194+
" array([[0, 1],\n",
195+
" [2, 3]]))"
196+
]
197+
},
198+
"metadata": {},
199+
"execution_count": 9
200+
}
201+
],
202+
"source": [
203+
"a = np.array([[2, 1]])\n",
204+
"b = np.arange(4).reshape(2, 2)\n",
205+
"a, b"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 13,
211+
"metadata": {},
212+
"outputs": [
213+
{
214+
"output_type": "stream",
215+
"name": "stdout",
216+
"text": [
217+
"[2 1 0 1 2 3]\n[[2 1]\n [0 1]\n [2 3]]\n[[2 0 1]\n [1 2 3]]\n"
218+
]
219+
}
220+
],
221+
"source": [
222+
"print( np.concatenate((a, b), axis=None) )\n",
223+
"print( np.concatenate((a, b), axis=0) ) # default axis\n",
224+
"print( np.concatenate((a.T, b), axis=1) )"
225+
]
226+
},
227+
{
228+
"cell_type": "code",
229+
"execution_count": 48,
230+
"metadata": {},
231+
"outputs": [
232+
{
233+
"output_type": "stream",
234+
"name": "stdout",
235+
"text": [
236+
"[[[0]\n [1]\n [2]]\n\n [[3]\n [4]\n [5]]\n\n [[6]\n [7]\n [8]]]\n[[[ 0 1]\n [ 2 3]\n [ 4 5]]\n\n [[ 6 7]\n [ 8 9]\n [10 11]]\n\n [[12 13]\n [14 15]\n [16 17]]]\n"
237+
]
238+
}
239+
],
240+
"source": [
241+
"a = np.arange(9).reshape(3, 3, 1)\n",
242+
"b = np.arange(18).reshape(3, 3, 2)\n",
243+
"print(a)\n",
244+
"print(b)"
245+
]
246+
},
247+
{
248+
"cell_type": "code",
249+
"execution_count": 50,
250+
"metadata": {},
251+
"outputs": [
252+
{
253+
"output_type": "stream",
254+
"name": "stdout",
255+
"text": [
256+
"[ 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14\n 15 16 17]\n[[[ 0 0 1]\n [ 1 2 3]\n [ 2 4 5]]\n\n [[ 3 6 7]\n [ 4 8 9]\n [ 5 10 11]]\n\n [[ 6 12 13]\n [ 7 14 15]\n [ 8 16 17]]]\n"
257+
]
258+
}
259+
],
260+
"source": [
261+
"print( np.concatenate((a, b), axis=None) )\n",
262+
"print( np.concatenate((a, b), axis=2) )"
263+
]
264+
},
265+
{
266+
"source": [
267+
"Note: The `axis` uses in `np.concatenate()` is not as in `max()`, `mean()` and other functions.\n",
268+
"\n",
269+
"- `axis=None` will merge the arrays into **flattened** arrays.\n",
270+
"- `axis=n` will merge the arrays on the `n-th` dimension.\n",
271+
" - e.g., `np.concatenate((3, 3, 1), (3, 3, 2), axis=2) = (3, 3, 3)`"
272+
],
273+
"cell_type": "markdown",
274+
"metadata": {}
275+
},
45276
{
46277
"source": [
47278
"## Artithemetics\n",
@@ -237,6 +468,18 @@
237468
"print(f\"np.all(A) = {np.all(A)}\")\n",
238469
"print(f\"np.any(A) = {np.any(A)}\")"
239470
]
471+
},
472+
{
473+
"source": [
474+
"# Reference\n",
475+
"\n",
476+
"- https://numpy.org/doc/stable/user/quickstart.html#basic-operations\n",
477+
"- https://numpy.org/doc/stable/user/absolute_beginners.html#adding-removing-and-sorting-elements\n",
478+
"- https://numpy.org/doc/stable/user/absolute_beginners.html#basic-array-operations\n",
479+
"- https://numpy.org/doc/stable/user/absolute_beginners.html#more-useful-array-operations"
480+
],
481+
"cell_type": "markdown",
482+
"metadata": {}
240483
}
241484
]
242485
}

0 commit comments

Comments
 (0)