35
35
},
36
36
{
37
37
"cell_type" : " code" ,
38
- "execution_count" : 3 ,
38
+ "execution_count" : 2 ,
39
39
"metadata" : {},
40
40
"outputs" : [],
41
41
"source" : [
42
42
" import numpy as np"
43
43
]
44
44
},
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
+ },
45
276
{
46
277
"source" : [
47
278
" ## Artithemetics\n " ,
237
468
" print(f\" np.all(A) = {np.all(A)}\" )\n " ,
238
469
" print(f\" np.any(A) = {np.any(A)}\" )"
239
470
]
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" : {}
240
483
}
241
484
]
242
485
}
0 commit comments