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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Setup
|
2 |
+
temperature = 0.7, topP = 0.95, turns = 10
|
3 |
+
|
4 |
+
A0: change example
|
5 |
+
A1: change logits(decimal places, array, etc)
|
6 |
+
A2: change output type (array -> dict, etc)
|
7 |
+
A3: analogy
|
8 |
+
A4: dimension(index) involved
|
9 |
+
A5: inverted operation
|
10 |
+
A6: order
|
11 |
+
A7: ±condition/operation
|
12 |
+
|
13 |
+
combinations involved, only show the highest level.
|
14 |
+
|
15 |
+
|
16 |
+
MAP
|
17 |
+
1.
|
18 |
+
Score:
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
1
|
23 |
+
2
|
24 |
+
3
|
25 |
+
4
|
26 |
+
5
|
27 |
+
6
|
28 |
+
7
|
29 |
+
8
|
30 |
+
9
|
31 |
+
10
|
32 |
+
Top-10
|
33 |
+
Avg
|
34 |
+
Origin
|
35 |
+
0
|
36 |
+
0
|
37 |
+
0
|
38 |
+
1
|
39 |
+
1
|
40 |
+
1
|
41 |
+
1
|
42 |
+
1
|
43 |
+
1
|
44 |
+
1
|
45 |
+
1
|
46 |
+
0.7
|
47 |
+
A1
|
48 |
+
0
|
49 |
+
0
|
50 |
+
0
|
51 |
+
0
|
52 |
+
1
|
53 |
+
0
|
54 |
+
0
|
55 |
+
1
|
56 |
+
1
|
57 |
+
1
|
58 |
+
1
|
59 |
+
0.4
|
60 |
+
A3
|
61 |
+
0
|
62 |
+
0
|
63 |
+
0
|
64 |
+
0
|
65 |
+
0
|
66 |
+
1
|
67 |
+
0
|
68 |
+
1
|
69 |
+
1
|
70 |
+
1
|
71 |
+
1
|
72 |
+
0.4
|
73 |
+
|
74 |
+
|
75 |
+
Origin:
|
76 |
+
Problem:
|
77 |
+
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first column of A, the second element to the second column and so on.
|
78 |
+
|
79 |
+
For example, given:
|
80 |
+
import numpy as np
|
81 |
+
|
82 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
|
83 |
+
A=np.array([[0, 1, 1, 1, 0],
|
84 |
+
[1, 0, 1, 0, 1],
|
85 |
+
[1, 1, 0, 1, 1],
|
86 |
+
[1, 0, 1, 0, 1],
|
87 |
+
[0, 1, 1, 1, 0]])
|
88 |
+
|
89 |
+
I want to produce:
|
90 |
+
array([[0, 2.464, 4.991, 5.799, 0],
|
91 |
+
[10, 0, 4.991, 0, 0],
|
92 |
+
[10, 2.464, 0, 5.799, 0],
|
93 |
+
[10, 0, 4.991, 0, 0],
|
94 |
+
[0, 2.464, 4.991, 5.799, 0]])
|
95 |
+
|
96 |
+
|
97 |
+
A:
|
98 |
+
|
99 |
+
<code>
|
100 |
+
import numpy as np
|
101 |
+
|
102 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
|
103 |
+
A=np.array([[0, 1, 1, 1, 0],
|
104 |
+
[1, 0, 1, 0, 1],
|
105 |
+
[1, 1, 0, 1, 1],
|
106 |
+
[1, 0, 1, 0, 1],
|
107 |
+
[0, 1, 1, 1, 0]])
|
108 |
+
### BEGIN SOLUTION
|
109 |
+
[insert]
|
110 |
+
### END SOLUTION
|
111 |
+
print(B)
|
112 |
+
</code>
|
113 |
+
|
114 |
+
test:
|
115 |
+
ans = A * X
|
116 |
+
try:
|
117 |
+
np.testing.assert_array_equal(ans, B)
|
118 |
+
print('Test passed!')
|
119 |
+
except:
|
120 |
+
print('Test failed...')
|
121 |
+
|
122 |
+
Problem:
|
123 |
+
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first column of A, the second element to the second column and so on.
|
124 |
+
|
125 |
+
For example, given:
|
126 |
+
import numpy as np
|
127 |
+
|
128 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
|
129 |
+
A=np.array([[0, 1, 1, 1, 0],
|
130 |
+
[1, 0, 1, 0, 1],
|
131 |
+
[1, 1, 0, 1, 1],
|
132 |
+
[1, 0, 1, 0, 1],
|
133 |
+
[0, 1, 1, 1, 0]])
|
134 |
+
|
135 |
+
I want to produce:
|
136 |
+
array([[0, 2.464, 4.991, 5.799, 0],
|
137 |
+
[10, 0, 4.991, 0, 0],
|
138 |
+
[10, 2.464, 0, 5.799, 0],
|
139 |
+
[10, 0, 4.991, 0, 0],
|
140 |
+
[0, 2.464, 4.991, 5.799, 0]])
|
141 |
+
|
142 |
+
Note that the result should be kept 3 decimal places just as the example.
|
143 |
+
|
144 |
+
|
145 |
+
A:
|
146 |
+
|
147 |
+
<code>
|
148 |
+
import numpy as np
|
149 |
+
|
150 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
|
151 |
+
A=np.array([[0, 1, 1, 1, 0],
|
152 |
+
[1, 0, 1, 0, 1],
|
153 |
+
[1, 1, 0, 1, 1],
|
154 |
+
[1, 0, 1, 0, 1],
|
155 |
+
[0, 1, 1, 1, 0]])
|
156 |
+
### BEGIN SOLUTION
|
157 |
+
[insert]
|
158 |
+
### END SOLUTION
|
159 |
+
print(B)
|
160 |
+
</code>
|
161 |
+
|
162 |
+
test:
|
163 |
+
ans = np.round(A * X, 3)
|
164 |
+
try:
|
165 |
+
np.testing.assert_array_equal(ans, B)
|
166 |
+
print('Test passed!')
|
167 |
+
except:
|
168 |
+
print('Test failed...')
|
169 |
+
|
170 |
+
|
171 |
+
A1:
|
172 |
+
Problem:
|
173 |
+
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first column of A, the second element to the second column and so on.
|
174 |
+
|
175 |
+
For example, given:
|
176 |
+
import numpy as np
|
177 |
+
|
178 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
|
179 |
+
A=np.array([[0, 1, 1, 1, 0],
|
180 |
+
[1, 0, 1, 0, 1],
|
181 |
+
[1, 1, 0, 1, 1],
|
182 |
+
[1, 0, 1, 0, 1],
|
183 |
+
[0, 1, 1, 1, 0]])
|
184 |
+
|
185 |
+
I want to produce:
|
186 |
+
array([[0, 2.464, 4.991, 5.799, 0],
|
187 |
+
[10, 0, 4.991, 0, 0],
|
188 |
+
[10, 2.464, 0, 5.799, 0],
|
189 |
+
[10, 0, 4.991, 0, 0],
|
190 |
+
[0, 2.464, 4.991, 5.799, 0]])
|
191 |
+
|
192 |
+
Note that the result should be kept 3 decimal places just as the example.
|
193 |
+
|
194 |
+
|
195 |
+
A:
|
196 |
+
|
197 |
+
<code>
|
198 |
+
import numpy as np
|
199 |
+
|
200 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
|
201 |
+
A=np.array([[0, 1, 1, 1, 0],
|
202 |
+
[1, 0, 1, 0, 1],
|
203 |
+
[1, 1, 0, 1, 1],
|
204 |
+
[1, 0, 1, 0, 1],
|
205 |
+
[0, 1, 1, 1, 0]])
|
206 |
+
### BEGIN SOLUTION
|
207 |
+
[insert]
|
208 |
+
### END SOLUTION
|
209 |
+
print(B)
|
210 |
+
</code>
|
211 |
+
|
212 |
+
|
213 |
+
test:
|
214 |
+
ans = np.round(A * X, 3)
|
215 |
+
try:
|
216 |
+
np.testing.assert_array_equal(ans, B)
|
217 |
+
print('Test passed!')
|
218 |
+
except:
|
219 |
+
print('Test failed...')
|
220 |
+
|
221 |
+
|
222 |
+
A3:
|
223 |
+
Problem:
|
224 |
+
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first row of A, the second element to the second row and so on.
|
225 |
+
|
226 |
+
For example, given:
|
227 |
+
import numpy as np
|
228 |
+
|
229 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
|
230 |
+
A=np.array([[0, 1, 1, 1, 0],
|
231 |
+
[1, 0, 1, 0, 1],
|
232 |
+
[1, 1, 0, 1, 1],
|
233 |
+
[1, 0, 1, 0, 1],
|
234 |
+
[0, 1, 1, 1, 0]])
|
235 |
+
|
236 |
+
I want to produce:
|
237 |
+
array([[0, 2.464, 4.991, 5.799, 0],
|
238 |
+
[10, 0, 4.991, 0, 0],
|
239 |
+
[10, 2.464, 0, 5.799, 0],
|
240 |
+
[10, 0, 4.991, 0, 0],
|
241 |
+
[0, 2.464, 4.991, 5.799, 0]])
|
242 |
+
|
243 |
+
Note that the result should be kept 3 decimal places just as the example.
|
244 |
+
|
245 |
+
A:
|
246 |
+
|
247 |
+
<code>
|
248 |
+
import numpy as np
|
249 |
+
|
250 |
+
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
|
251 |
+
A=np.array([[0, 1, 1, 1, 0],
|
252 |
+
[1, 0, 1, 0, 1],
|
253 |
+
[1, 1, 0, 1, 1],
|
254 |
+
[1, 0, 1, 0, 1],
|
255 |
+
[0, 1, 1, 1, 0]])
|
256 |
+
### BEGIN SOLUTION
|
257 |
+
[insert]
|
258 |
+
### END SOLUTION
|
259 |
+
print(B)
|
260 |
+
</code>
|
261 |
+
|
262 |
+
Test:
|
263 |
+
ans = np.round((A.T * X).T, 3)
|
264 |
+
try:
|
265 |
+
np.testing.assert_array_equal(ans, B)
|
266 |
+
print('Test passed!')
|
267 |
+
except:
|
268 |
+
print('Test failed...')
|
269 |
+
|
270 |
+
|
271 |
+
|
272 |
+
2.
|
273 |
+
Score:
|
274 |
+
|
275 |
+
|
276 |
+
|
277 |
+
1
|
278 |
+
2
|
279 |
+
3
|
280 |
+
4
|
281 |
+
5
|
282 |
+
6
|
283 |
+
7
|
284 |
+
8
|
285 |
+
9
|
286 |
+
10
|
287 |
+
Top-10
|
288 |
+
Avg
|
289 |
+
Origin
|
290 |
+
0
|
291 |
+
0
|
292 |
+
0
|
293 |
+
0
|
294 |
+
0
|
295 |
+
0
|
296 |
+
0
|
297 |
+
0
|
298 |
+
0
|
299 |
+
0
|
300 |
+
0
|
301 |
+
0
|
302 |
+
|
303 |
+
|
304 |
+
Origin:
|
305 |
+
|
306 |
+
Problem:
|
307 |
+
I have a NumPy record array of floats:
|
308 |
+
|
309 |
+
ar = np.array([(238.03, 238.0, 237.0),
|
310 |
+
(238.02, 238.0, 237.01),
|
311 |
+
(238.05, 238.01, 237.0)],
|
312 |
+
dtype=[('A', 'f'), ('B', 'f'), ('C', 'f')])
|
313 |
+
|
314 |
+
How can I determine min from each column of this record array?
|
315 |
+
|
316 |
+
desired:
|
317 |
+
[238.02 ,238. ,237. ]
|
318 |
+
|
319 |
+
A:
|
320 |
+
<code>
|
321 |
+
import numpy as np
|
322 |
+
ar = np.array([(238.03, 238.0, 237.0),
|
323 |
+
(238.02, 238.0, 237.01),
|
324 |
+
(238.05, 238.01, 237.0)],
|
325 |
+
dtype=[('A', 'f'), ('B', 'f'), ('C', 'f')])
|
326 |
+
### BEGIN SOLUTION
|
327 |
+
[insert]
|
328 |
+
### END SOLUTION
|
329 |
+
print(result)
|
330 |
+
</code>
|
331 |
+
|
332 |
+
Test:
|
333 |
+
ar_view = ar.view((ar.dtype[0], len(ar.dtype.names)))
|
334 |
+
ans = ar_view.min(axis=0)
|
335 |
+
try:
|
336 |
+
np.testing.assert_array_equal(ans, result)
|
337 |
+
print('Test passed!')
|
338 |
+
except:
|
339 |
+
print('Test failed...')
|
340 |
+
|
341 |
+
|
342 |
+
3.
|
343 |
+
Score:
|
344 |
+
|
345 |
+
|
346 |
+
|
347 |
+
1
|
348 |
+
2
|
349 |
+
3
|
350 |
+
4
|
351 |
+
5
|
352 |
+
6
|
353 |
+
7
|
354 |
+
8
|
355 |
+
9
|
356 |
+
10
|
357 |
+
Top-10
|
358 |
+
Avg
|
359 |
+
Origin
|
360 |
+
0
|
361 |
+
1
|
362 |
+
0
|
363 |
+
1
|
364 |
+
0
|
365 |
+
1
|
366 |
+
1
|
367 |
+
0
|
368 |
+
0
|
369 |
+
0
|
370 |
+
1
|
371 |
+
0.5
|
372 |
+
A2
|
373 |
+
1
|
374 |
+
1
|
375 |
+
1
|
376 |
+
1
|
377 |
+
1
|
378 |
+
1
|
379 |
+
0
|
380 |
+
1
|
381 |
+
1
|
382 |
+
1
|
383 |
+
1
|
384 |
+
0.9
|
385 |
+
|
386 |
+
|
387 |
+
Origin:
|
388 |
+
Problem:
|
389 |
+
Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.
|
390 |
+
|
391 |
+
A:
|
392 |
+
<code>
|
393 |
+
import numpy as np
|
394 |
+
x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])
|
395 |
+
### BEGIN SOLUTION
|
396 |
+
[insert]
|
397 |
+
### END SOLUTION
|
398 |
+
print(u, indices)
|
399 |
+
</code>
|
400 |
+
|
401 |
+
Test:
|
402 |
+
try:
|
403 |
+
np.testing.assert_array_equal(u, np.array([1, 2, 3, 4, 5]))
|
404 |
+
np.testing.assert_array_equal(indices, np.array([2, 3, 1, 1, 2]))
|
405 |
+
print('Test passed!')
|
406 |
+
except:
|
407 |
+
print('Test failed...')
|
408 |
+
|
409 |
+
|
410 |
+
A2:
|
411 |
+
Problem:
|
412 |
+
Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.
|
413 |
+
Desired output(dict):
|
414 |
+
{1: 2, 2: 3, 3: 1, 4: 1, 5: 2}
|
415 |
+
|
416 |
+
A:
|
417 |
+
<code>
|
418 |
+
import numpy as np
|
419 |
+
x = np.array([2, 2, 2, 1, 5, 4, 5, 1, 2, 3])
|
420 |
+
### BEGIN SOLUTION
|
421 |
+
[insert]
|
422 |
+
### END SOLUTION
|
423 |
+
print(result)
|
424 |
+
</code>
|
425 |
+
|
426 |
+
test:
|
427 |
+
try:
|
428 |
+
assert result == {2: 4, 1: 2, 5: 2, 4: 1, 3: 1}
|
429 |
+
print('Test passed!')
|
430 |
+
except:
|
431 |
+
print('Test failed...')
|
432 |
+
|
433 |
+
|
434 |
+
|
435 |
+
|
436 |
+
|
437 |
+
|
438 |
+
|
439 |
+
4.
|
440 |
+
Score:
|
441 |
+
|
442 |
+
|
443 |
+
|
444 |
+
1
|
445 |
+
2
|
446 |
+
3
|
447 |
+
4
|
448 |
+
5
|
449 |
+
6
|
450 |
+
7
|
451 |
+
8
|
452 |
+
9
|
453 |
+
10
|
454 |
+
Top-10
|
455 |
+
Avg
|
456 |
+
Origin
|
457 |
+
0
|
458 |
+
0
|
459 |
+
0
|
460 |
+
0
|
461 |
+
0
|
462 |
+
0
|
463 |
+
0
|
464 |
+
0
|
465 |
+
0
|
466 |
+
0
|
467 |
+
0
|
468 |
+
0
|
469 |
+
|
470 |
+
|
471 |
+
Origin:
|
472 |
+
Problem:
|
473 |
+
Using NumPy, complete the function below. The function should create and return the following 2-D array. You must find a way to generate the array without typing it explicitly:
|
474 |
+
|
475 |
+
[[1, 6, 11],
|
476 |
+
[2, 7, 12],
|
477 |
+
[3, 8, 13],
|
478 |
+
[4, 9, 14],
|
479 |
+
[5, 10, 15]]
|
480 |
+
|
481 |
+
|
482 |
+
A:
|
483 |
+
<code>
|
484 |
+
import numpy as np
|
485 |
+
|
486 |
+
def create_array():
|
487 |
+
### BEGIN SOLUTION
|
488 |
+
[insert]
|
489 |
+
### END SOLUTION
|
490 |
+
return result
|
491 |
+
</code>
|
492 |
+
|
493 |
+
test:
|
494 |
+
|
495 |
+
try:
|
496 |
+
np.testing.assert_array_equal(create_array(), np.array([[1,6,11],[2,7,12],[3,8,13],[4,9,14],[5,10,15]]))
|
497 |
+
print('Test passed!')
|
498 |
+
except:
|
499 |
+
print('Test failed...')
|
500 |
+
|
501 |
+
|
502 |
+
|
503 |
+
|
504 |
+
5.
|
505 |
+
Score:
|
506 |
+
|
507 |
+
|
508 |
+
|
509 |
+
1
|
510 |
+
2
|
511 |
+
3
|
512 |
+
4
|
513 |
+
5
|
514 |
+
6
|
515 |
+
7
|
516 |
+
8
|
517 |
+
9
|
518 |
+
10
|
519 |
+
Top-10
|
520 |
+
Avg
|
521 |
+
Origin
|
522 |
+
0
|
523 |
+
0
|
524 |
+
0
|
525 |
+
0
|
526 |
+
0
|
527 |
+
0
|
528 |
+
0
|
529 |
+
0
|
530 |
+
0
|
531 |
+
0
|
532 |
+
0
|
533 |
+
0
|
534 |
+
|
535 |
+
|
536 |
+
Origin:
|
537 |
+
Problem:
|
538 |
+
Complete the function below. The function must return an array that contains the third column of the array "original" which is passed as an argument. The argument must be a 2-D array. If the argument is invalid, return None.
|
539 |
+
|
540 |
+
A:
|
541 |
+
<code>
|
542 |
+
import numpy as np
|
543 |
+
|
544 |
+
def new_array_second_column(original):
|
545 |
+
### BEGIN SOLUTION
|
546 |
+
[insert]
|
547 |
+
### END SOLUTION
|
548 |
+
return result
|
549 |
+
</code>
|
550 |
+
|
551 |
+
|
552 |
+
Test:
|
553 |
+
case = np.arange(16)[1:].reshape((3,5)).T
|
554 |
+
try:
|
555 |
+
np.testing.assert_array_equal(new_array_second_column(case), np.array([[11],[12],[13],[14],[15]]))
|
556 |
+
np.testing.assert_array_equal(new_array_second_column(np.array([1,2,3])), None)
|
557 |
+
np.testing.assert_array_equal(new_array_second_column(np.array([[1,2],[4,5],[7,8]])), None)
|
558 |
+
print('Test passed!')
|
559 |
+
except:
|
560 |
+
print('Test failed...')
|
561 |
+
|
562 |
+
|
563 |
+
|
564 |
+
|
565 |
+
6.
|
566 |
+
Score:
|
567 |
+
|
568 |
+
|
569 |
+
|
570 |
+
1
|
571 |
+
2
|
572 |
+
3
|
573 |
+
4
|
574 |
+
5
|
575 |
+
6
|
576 |
+
7
|
577 |
+
8
|
578 |
+
9
|
579 |
+
10
|
580 |
+
Top-10
|
581 |
+
Avg
|
582 |
+
Origin
|
583 |
+
0
|
584 |
+
0
|
585 |
+
0
|
586 |
+
0
|
587 |
+
0
|
588 |
+
0
|
589 |
+
0
|
590 |
+
0
|
591 |
+
0
|
592 |
+
0
|
593 |
+
0
|
594 |
+
0
|
595 |
+
|
596 |
+
|
597 |
+
Origin:
|
598 |
+
Problem:
|
599 |
+
I have an array that looks like below:
|
600 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7, 7, 7]])
|
601 |
+
How can I use reshape to divide it into 4 chucks, such that it looks like
|
602 |
+
array([[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7]]])
|
603 |
+
|
604 |
+
A:
|
605 |
+
<code>
|
606 |
+
import numpy as np
|
607 |
+
|
608 |
+
a = np.arange(8)[:,None].repeat(8,axis=1)
|
609 |
+
#BEGIN SOLUTION
|
610 |
+
[insert]
|
611 |
+
### END SOLUTION
|
612 |
+
print(ans)
|
613 |
+
</code>
|
614 |
+
|
615 |
+
Test:
|
616 |
+
b = a.reshape(2,4,2,4).transpose(0,2,1,3)
|
617 |
+
|
618 |
+
try:
|
619 |
+
np.testing.assert_array_equal(ans, b)
|
620 |
+
print('Test passed!')
|
621 |
+
except:
|
622 |
+
print('Test failed...')
|
623 |
+
|
624 |
+
|
625 |
+
|
626 |
+
|
627 |
+
7.
|
628 |
+
Score:
|
629 |
+
|
630 |
+
|
631 |
+
|
632 |
+
1
|
633 |
+
2
|
634 |
+
3
|
635 |
+
4
|
636 |
+
5
|
637 |
+
6
|
638 |
+
7
|
639 |
+
8
|
640 |
+
9
|
641 |
+
10
|
642 |
+
Top-10
|
643 |
+
Avg
|
644 |
+
Origin
|
645 |
+
1
|
646 |
+
1
|
647 |
+
1
|
648 |
+
0
|
649 |
+
0
|
650 |
+
0
|
651 |
+
0
|
652 |
+
0
|
653 |
+
1
|
654 |
+
0
|
655 |
+
1
|
656 |
+
0.4
|
657 |
+
A4
|
658 |
+
1
|
659 |
+
0
|
660 |
+
0
|
661 |
+
0
|
662 |
+
0
|
663 |
+
0
|
664 |
+
0
|
665 |
+
0
|
666 |
+
1
|
667 |
+
0
|
668 |
+
1
|
669 |
+
0.2
|
670 |
+
|
671 |
+
|
672 |
+
Origin:
|
673 |
+
Problem:
|
674 |
+
I have a numpy array of shape (3, 3, k), where the length k is fixed. The array was processed to a flatten one dimensional one with:
|
675 |
+
mat2 = numpy.transpose(data, (1, 0, 2)).flatten('C')
|
676 |
+
How do I reverse this transpose / flattening process to get the original (3, 3, k) shape and ordering of the data array?
|
677 |
+
|
678 |
+
A:
|
679 |
+
<code>
|
680 |
+
import numpy as np
|
681 |
+
k = 10
|
682 |
+
a = np.linspace(0, 89, 90).reshape((3, 3, k))
|
683 |
+
b = np.transpose(a, (1, 0, 2)).flatten('C')
|
684 |
+
|
685 |
+
### BEGIN SOLUTION
|
686 |
+
[insert]
|
687 |
+
### END SOLUTION
|
688 |
+
print(ans.shape)
|
689 |
+
</code>
|
690 |
+
|
691 |
+
test:
|
692 |
+
try:
|
693 |
+
assert id(ans) != id(a)
|
694 |
+
np.testing.assert_array_equal(ans, a)
|
695 |
+
print('Test passed!')
|
696 |
+
except:
|
697 |
+
print('Test failed...')
|
698 |
+
|
699 |
+
|
700 |
+
A4:
|
701 |
+
Problem:
|
702 |
+
I have a numpy array of shape (3, 3, k), where the length k is fixed. The array was processed to a flatten one dimensional one with:
|
703 |
+
mat2 = numpy.transpose(data, (1, 2, 0)).flatten('C')
|
704 |
+
How do I reverse this transpose / flattening process to get the original (3, 3, k) shape and ordering of the data array?
|
705 |
+
|
706 |
+
A:
|
707 |
+
<code>
|
708 |
+
import numpy as np
|
709 |
+
k = 10
|
710 |
+
a = np.linspace(0, 89, 90).reshape((3, 3, k))
|
711 |
+
b = np.transpose(a, (1, 2, 0)).flatten('C')
|
712 |
+
|
713 |
+
### BEGIN SOLUTION
|
714 |
+
[insert]
|
715 |
+
### END SOLUTION
|
716 |
+
print(ans.shape)
|
717 |
+
</code>
|
718 |
+
|
719 |
+
test:
|
720 |
+
try:
|
721 |
+
assert id(ans) != id(a)
|
722 |
+
np.testing.assert_array_equal(ans, a)
|
723 |
+
print('Test passed!')
|
724 |
+
except:
|
725 |
+
print('Test failed...')
|
726 |
+
|
727 |
+
|
728 |
+
|
729 |
+
|
730 |
+
|
731 |
+
8.
|
732 |
+
Score:
|
733 |
+
|
734 |
+
|
735 |
+
|
736 |
+
1
|
737 |
+
2
|
738 |
+
3
|
739 |
+
4
|
740 |
+
5
|
741 |
+
6
|
742 |
+
7
|
743 |
+
8
|
744 |
+
9
|
745 |
+
10
|
746 |
+
Top-10
|
747 |
+
Avg
|
748 |
+
Origin
|
749 |
+
1
|
750 |
+
1
|
751 |
+
1
|
752 |
+
1
|
753 |
+
1
|
754 |
+
0
|
755 |
+
1
|
756 |
+
1
|
757 |
+
1
|
758 |
+
1
|
759 |
+
1
|
760 |
+
0.9
|
761 |
+
A5
|
762 |
+
1
|
763 |
+
0
|
764 |
+
1
|
765 |
+
1
|
766 |
+
0
|
767 |
+
1
|
768 |
+
1
|
769 |
+
0
|
770 |
+
1
|
771 |
+
1
|
772 |
+
1
|
773 |
+
0.7
|
774 |
+
|
775 |
+
|
776 |
+
Origin:
|
777 |
+
Problem:
|
778 |
+
I'm generating matrix representations of images with height*width size, and I need to transform them into a vector of pixels. To generate the images, I'm using the following instruction
|
779 |
+
np.array([[np.random.randint(0, 255, 3) for dummy_row in range(height)] for dummy_col in range(width)])
|
780 |
+
e.g., (2x2) image
|
781 |
+
array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
|
782 |
+
when I'm requiring
|
783 |
+
array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
|
784 |
+
|
785 |
+
A:
|
786 |
+
<code>
|
787 |
+
import numpy as np
|
788 |
+
|
789 |
+
def f(arr):
|
790 |
+
### BEGIN SOLUTION
|
791 |
+
[insert]
|
792 |
+
### END SOLUTION
|
793 |
+
return result
|
794 |
+
</code>
|
795 |
+
|
796 |
+
tset:
|
797 |
+
a = np.array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
|
798 |
+
b = np.array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
|
799 |
+
try:
|
800 |
+
np.testing.assert_array_equal(f(a), b)
|
801 |
+
print('Test passed!')
|
802 |
+
except:
|
803 |
+
print('Test failed...')
|
804 |
+
|
805 |
+
A5:
|
806 |
+
Problem:
|
807 |
+
I'm generating matrix representations of images with height*width size, and I need to transform them into a vector of pixels. To generate the images, I'm using the following instruction
|
808 |
+
e.g., (2x2) image
|
809 |
+
array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
|
810 |
+
when I'm requiring
|
811 |
+
array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
|
812 |
+
|
813 |
+
A:
|
814 |
+
<code>
|
815 |
+
import numpy as np
|
816 |
+
|
817 |
+
def f(arr):
|
818 |
+
### BEGIN SOLUTION
|
819 |
+
[insert]
|
820 |
+
### END SOLUTION
|
821 |
+
return result
|
822 |
+
</code>
|
823 |
+
|
824 |
+
tset:
|
825 |
+
a = np.array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
|
826 |
+
b = np.array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
|
827 |
+
try:
|
828 |
+
np.testing.assert_array_equal(f(b), a)
|
829 |
+
print('Test passed!')
|
830 |
+
except:
|
831 |
+
print('Test failed...')
|
832 |
+
|
833 |
+
|
834 |
+
|
835 |
+
|
836 |
+
|
837 |
+
9*.
|
838 |
+
Score:
|
839 |
+
|
840 |
+
|
841 |
+
|
842 |
+
1
|
843 |
+
2
|
844 |
+
3
|
845 |
+
4
|
846 |
+
5
|
847 |
+
6
|
848 |
+
7
|
849 |
+
8
|
850 |
+
9
|
851 |
+
10
|
852 |
+
Top-10
|
853 |
+
Avg
|
854 |
+
Origin
|
855 |
+
1
|
856 |
+
1
|
857 |
+
1
|
858 |
+
1
|
859 |
+
1
|
860 |
+
0
|
861 |
+
1
|
862 |
+
1
|
863 |
+
1
|
864 |
+
1
|
865 |
+
1
|
866 |
+
0.9
|
867 |
+
A4
|
868 |
+
0
|
869 |
+
0
|
870 |
+
0
|
871 |
+
0
|
872 |
+
0
|
873 |
+
0
|
874 |
+
0
|
875 |
+
0
|
876 |
+
0
|
877 |
+
0
|
878 |
+
0
|
879 |
+
0
|
880 |
+
|
881 |
+
|
882 |
+
Origin:
|
883 |
+
Problem:
|
884 |
+
I have a df like this:
|
885 |
+
import pandas as pd
|
886 |
+
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
|
887 |
+
df = pd.DataFrame.from_records(a[0:],columns=a[0])
|
888 |
+
I want to flatten the df so it is one continuous list like so:
|
889 |
+
['1/2/2014', 'a', '6', 'z1', '1/2/2014', 'a', '3', 'z1','1/3/2014', 'c', '1', 'x3']
|
890 |
+
|
891 |
+
A:
|
892 |
+
<code>
|
893 |
+
import pandas as pd
|
894 |
+
import numpy as np
|
895 |
+
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
|
896 |
+
df = pd.DataFrame.from_records(a[0:],columns=a[0])
|
897 |
+
### BEGIN SOLUTION
|
898 |
+
[insert]
|
899 |
+
### END SOLUTION
|
900 |
+
print(ans)
|
901 |
+
</code>
|
902 |
+
|
903 |
+
Test:
|
904 |
+
try:
|
905 |
+
np.testing.assert_array_equal(df.to_numpy().flatten(),ans)
|
906 |
+
print('Test passed!')
|
907 |
+
except:
|
908 |
+
print('Test failed...')
|
909 |
+
|
910 |
+
A4:
|
911 |
+
Problem:
|
912 |
+
I have a df like this:
|
913 |
+
import pandas as pd
|
914 |
+
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
|
915 |
+
df = pd.DataFrame.from_records(a[0:],columns=a[0])
|
916 |
+
I want to flatten the df so it is one continuous list like so:
|
917 |
+
['1/2/2014', '1/2/2014', '1/3/2014', 'a', 'a', 'c', '6', '3', '1', 'z1', 'z1', 'x3']
|
918 |
+
|
919 |
+
A:
|
920 |
+
<code>
|
921 |
+
import pandas as pd
|
922 |
+
import numpy as np
|
923 |
+
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
|
924 |
+
df = pd.DataFrame.from_records(a[0:],columns=a[0])
|
925 |
+
### BEGIN SOLUTION
|
926 |
+
[insert]
|
927 |
+
### END SOLUTION
|
928 |
+
print(ans)
|
929 |
+
</code>
|
930 |
+
|
931 |
+
Test:
|
932 |
+
try:
|
933 |
+
np.testing.assert_array_equal(df.to_numpy().T.flatten(),ans)
|
934 |
+
print('Test passed!')
|
935 |
+
except:
|
936 |
+
print('Test failed...')
|
937 |
+
|
938 |
+
|
939 |
+
|
940 |
+
10.
|
941 |
+
Score:
|
942 |
+
|
943 |
+
|
944 |
+
|
945 |
+
1
|
946 |
+
2
|
947 |
+
3
|
948 |
+
4
|
949 |
+
5
|
950 |
+
6
|
951 |
+
7
|
952 |
+
8
|
953 |
+
9
|
954 |
+
10
|
955 |
+
Top-10
|
956 |
+
Avg
|
957 |
+
Origin
|
958 |
+
0
|
959 |
+
0
|
960 |
+
0
|
961 |
+
0
|
962 |
+
0
|
963 |
+
0
|
964 |
+
1
|
965 |
+
1
|
966 |
+
0
|
967 |
+
0
|
968 |
+
1
|
969 |
+
0.2
|
970 |
+
A4
|
971 |
+
0
|
972 |
+
0
|
973 |
+
0
|
974 |
+
0
|
975 |
+
0
|
976 |
+
0
|
977 |
+
0
|
978 |
+
0
|
979 |
+
0
|
980 |
+
0
|
981 |
+
0
|
982 |
+
0
|
983 |
+
|
984 |
+
|
985 |
+
Origin:
|
986 |
+
Problem:
|
987 |
+
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
|
988 |
+
[array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]), array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
|
989 |
+
For instance, I'd like to compute the total number of array elements, which is 16 for the array above, but without doing a for loop since in practice my "nested array" will be quite large.
|
990 |
+
|
991 |
+
A:
|
992 |
+
<code>
|
993 |
+
import numpy as np
|
994 |
+
from numpy import array
|
995 |
+
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
|
996 |
+
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
|
997 |
+
### BEGIN SOLUTION
|
998 |
+
[insert]
|
999 |
+
### END SOLUTION
|
1000 |
+
print(ans)
|
1001 |
+
</code>
|
1002 |
+
|
1003 |
+
Test:
|
1004 |
+
try:
|
1005 |
+
np.testing.assert_array_equal(ans,len(np.concatenate(a).ravel()))
|
1006 |
+
print('Test passed!')
|
1007 |
+
except:
|
1008 |
+
print('Test failed...')
|
1009 |
+
+for elimination
|
1010 |
+
|
1011 |
+
|
1012 |
+
|
1013 |
+
A4:
|
1014 |
+
Problem:
|
1015 |
+
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
|
1016 |
+
[array([0, 1, 3]) ,array([[0, 1, 7]]), array([2]), array([[0, 3]]), array([4]), array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
|
1017 |
+
For instance, I'd like to compute the total number of array elements, but without doing a for loop since in practice my "nested array" will be quite large.
|
1018 |
+
|
1019 |
+
A:
|
1020 |
+
<code>
|
1021 |
+
import numpy as np
|
1022 |
+
from numpy import array
|
1023 |
+
a = [array([0, 1, 3]) ,array([[0, 1, 7]]), array([2]), array([[0, 3]]), array([4]),
|
1024 |
+
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
|
1025 |
+
### BEGIN SOLUTION
|
1026 |
+
[insert]
|
1027 |
+
### END SOLUTION
|
1028 |
+
print(ans)
|
1029 |
+
</code>
|
1030 |
+
|
1031 |
+
Test:
|
1032 |
+
a = map(lambda x: x.flatten(), a)
|
1033 |
+
result = sum(map(len, a))
|
1034 |
+
try:
|
1035 |
+
assert result == ans
|
1036 |
+
print('Test passed!')
|
1037 |
+
except:
|
1038 |
+
print('Test failed...')
|
1039 |
+
+for elimination
|
1040 |
+
|
1041 |
+
11.
|
1042 |
+
Score:
|
1043 |
+
|
1044 |
+
|
1045 |
+
|
1046 |
+
1
|
1047 |
+
2
|
1048 |
+
3
|
1049 |
+
4
|
1050 |
+
5
|
1051 |
+
6
|
1052 |
+
7
|
1053 |
+
8
|
1054 |
+
9
|
1055 |
+
10
|
1056 |
+
Top-10
|
1057 |
+
Avg
|
1058 |
+
Origin
|
1059 |
+
0
|
1060 |
+
0
|
1061 |
+
0
|
1062 |
+
1
|
1063 |
+
1
|
1064 |
+
0
|
1065 |
+
0
|
1066 |
+
0
|
1067 |
+
1
|
1068 |
+
0
|
1069 |
+
1
|
1070 |
+
0.3
|
1071 |
+
A1
|
1072 |
+
0
|
1073 |
+
0
|
1074 |
+
0
|
1075 |
+
0
|
1076 |
+
0
|
1077 |
+
0
|
1078 |
+
0
|
1079 |
+
0
|
1080 |
+
0
|
1081 |
+
0
|
1082 |
+
0
|
1083 |
+
0
|
1084 |
+
|
1085 |
+
|
1086 |
+
Origin:
|
1087 |
+
Problem:
|
1088 |
+
I have an array, R. I would like to remove elements corresponding to indices in Remove and then flatten them with the remaining elements. The desired output is attached.
|
1089 |
+
R=np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
|
1090 |
+
[6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
|
1091 |
+
[1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])
|
1092 |
+
Remove = [(0, 1),(0,2)]
|
1093 |
+
R1 = R.flatten()
|
1094 |
+
print([R1])
|
1095 |
+
|
1096 |
+
The desired output is
|
1097 |
+
array([1.05567452e+11, 6.94076420e+09, 1.96129124e+10, 1.11642674e+09,
|
1098 |
+
1.88618492e+10, 1.73640817e+10, 4.84980874e+09])
|
1099 |
+
|
1100 |
+
A:
|
1101 |
+
<code>
|
1102 |
+
import numpy as np
|
1103 |
+
R = np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
|
1104 |
+
[6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
|
1105 |
+
[1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])
|
1106 |
+
Remove = [(0, 1), (0, 2)]
|
1107 |
+
### BEGIN SOLUTION
|
1108 |
+
[insert]
|
1109 |
+
### END SOLUTION
|
1110 |
+
print(ans)
|
1111 |
+
</code>
|
1112 |
+
|
1113 |
+
Test:
|
1114 |
+
a = np.array([1.05567452e+11,6.94076420e+09,1.96129124e+10,1.11642674e+09,
|
1115 |
+
1.88618492e+10, 1.73640817e+10, 4.84980874e+09])
|
1116 |
+
|
1117 |
+
try:
|
1118 |
+
np.testing.assert_array_equal(a, ans)
|
1119 |
+
print('Test passed!')
|
1120 |
+
except:
|
1121 |
+
print('Test failed...')
|
1122 |
+
|
1123 |
+
A1:
|
1124 |
+
Problem:
|
1125 |
+
I have an array, R. I would like to remove elements corresponding to indices in Remove and then flatten them with the remaining elements. The desired output is attached.
|
1126 |
+
R=np.array([[1.05567452, 1.51583103, 5.66466172],
|
1127 |
+
[6.94076420, 1.96129124, 1.11642674],
|
1128 |
+
[1.88618492, 1.73640817, 4.84980874]])
|
1129 |
+
Remove = [(0, 1),(0,2)]
|
1130 |
+
R1 = R.flatten()
|
1131 |
+
print([R1])
|
1132 |
+
|
1133 |
+
and I want to just keep 2 decimal places.
|
1134 |
+
|
1135 |
+
The desired output is
|
1136 |
+
array([1.06, 6.94, 1.96, 1.12, 1.89, 1.74, 4.85])
|
1137 |
+
|
1138 |
+
A:
|
1139 |
+
<code>
|
1140 |
+
import numpy as np
|
1141 |
+
R = np.array([[1.05567452, 1.51583103, 5.66466172],
|
1142 |
+
[6.94076420, 1.808484, 1.11642674],
|
1143 |
+
[1.88618492, 1.73640817, 4.84980874]])
|
1144 |
+
Remove = [(0, 1), (0, 2)]
|
1145 |
+
### BEGIN SOLUTION
|
1146 |
+
[insert]
|
1147 |
+
### END SOLUTION
|
1148 |
+
print(ans)
|
1149 |
+
</code>
|
1150 |
+
|
1151 |
+
Test:
|
1152 |
+
a = np.array([1.06, 6.94, 1.81, 1.12, 1.89, 1.74, 4.85])
|
1153 |
+
|
1154 |
+
try:
|
1155 |
+
np.testing.assert_array_equal(a, ans)
|
1156 |
+
print('Test passed!')
|
1157 |
+
except:
|
1158 |
+
print('Test failed...')
|
1159 |
+
|
1160 |
+
|
1161 |
+
|
1162 |
+
|
1163 |
+
|
1164 |
+
|
1165 |
+
12.
|
1166 |
+
Score:
|
1167 |
+
|
1168 |
+
|
1169 |
+
|
1170 |
+
1
|
1171 |
+
2
|
1172 |
+
3
|
1173 |
+
4
|
1174 |
+
5
|
1175 |
+
6
|
1176 |
+
7
|
1177 |
+
8
|
1178 |
+
9
|
1179 |
+
10
|
1180 |
+
Top-10
|
1181 |
+
Avg
|
1182 |
+
Origin
|
1183 |
+
0
|
1184 |
+
0
|
1185 |
+
0
|
1186 |
+
1
|
1187 |
+
0
|
1188 |
+
1
|
1189 |
+
1
|
1190 |
+
1
|
1191 |
+
1
|
1192 |
+
0
|
1193 |
+
1
|
1194 |
+
0.5
|
1195 |
+
A4
|
1196 |
+
0
|
1197 |
+
0
|
1198 |
+
1
|
1199 |
+
0
|
1200 |
+
0
|
1201 |
+
0
|
1202 |
+
0
|
1203 |
+
0
|
1204 |
+
0
|
1205 |
+
0
|
1206 |
+
1
|
1207 |
+
0.1
|
1208 |
+
|
1209 |
+
|
1210 |
+
Origin:
|
1211 |
+
Problem:
|
1212 |
+
Now I have a 3D numpy array with shape (2,3,4) as follows:
|
1213 |
+
[[[ 0 1 2 3]
|
1214 |
+
[ 4 5 6 7]
|
1215 |
+
[ 8 9 10 11]]
|
1216 |
+
[[12 13 14 15]
|
1217 |
+
[16 17 18 19]
|
1218 |
+
[20 21 22 23]]]
|
1219 |
+
Now, I want to reshape the array to (2,4,3) by swapping the last 2 dimensions of the array as follows:
|
1220 |
+
[[[ 0 4 8]
|
1221 |
+
[ 1 5 9]
|
1222 |
+
[ 2 6 10]
|
1223 |
+
[ 3 7 11]]
|
1224 |
+
[[12 16 20]
|
1225 |
+
[13 17 21]
|
1226 |
+
[14 18 22]
|
1227 |
+
[15 19 23]]]
|
1228 |
+
|
1229 |
+
A:
|
1230 |
+
<code>
|
1231 |
+
import numpy as np
|
1232 |
+
arr = np.array([[[ 0 , 1, 2, 3], [ 4 , 5, 6, 7], [ 8 , 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
|
1233 |
+
### BEGIN SOLUTION
|
1234 |
+
[insert]
|
1235 |
+
### END SOLUTION
|
1236 |
+
print(ans)
|
1237 |
+
</code>
|
1238 |
+
|
1239 |
+
Test:
|
1240 |
+
a = np.transpose(arr, axes=(0, 2, 1))
|
1241 |
+
|
1242 |
+
try:
|
1243 |
+
np.testing.assert_array_equal(a, ans)
|
1244 |
+
print('Test passed!')
|
1245 |
+
except:
|
1246 |
+
print('Test failed...')
|
1247 |
+
|
1248 |
+
|
1249 |
+
A4:
|
1250 |
+
Problem:
|
1251 |
+
Now I have a 3D numpy array with shape (2,3,4) as follows:
|
1252 |
+
[[[ 0 1 2 3]
|
1253 |
+
[ 4 5 6 7]
|
1254 |
+
[ 8 9 10 11]]
|
1255 |
+
[[12 13 14 15]
|
1256 |
+
[16 17 18 19]
|
1257 |
+
[20 21 22 23]]]
|
1258 |
+
Now, I want to reshape the array by swapping the axes of the array as follows:
|
1259 |
+
[[[ 0, 4, 8],
|
1260 |
+
[12, 16, 20]],
|
1261 |
+
[[ 1, 5, 9],
|
1262 |
+
[13, 17, 21]],
|
1263 |
+
[[ 2, 6, 10],
|
1264 |
+
[14, 18, 22]],
|
1265 |
+
[[ 3, 7, 11],
|
1266 |
+
[15, 19, 23]]]
|
1267 |
+
|
1268 |
+
A:
|
1269 |
+
<code>
|
1270 |
+
import numpy as np
|
1271 |
+
arr = np.array([[[ 0 , 1, 2, 3], [ 4 , 5, 6, 7], [ 8 , 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
|
1272 |
+
### BEGIN SOLUTION
|
1273 |
+
[insert]
|
1274 |
+
### END SOLUTION
|
1275 |
+
print(ans)
|
1276 |
+
</code>
|
1277 |
+
|
1278 |
+
Test:
|
1279 |
+
a = np.transpose(arr, axes=(2, 0, 1))
|
1280 |
+
|
1281 |
+
try:
|
1282 |
+
np.testing.assert_array_equal(a, ans)
|
1283 |
+
print('Test passed!')
|
1284 |
+
except:
|
1285 |
+
print('Test failed...')
|
1286 |
+
|
1287 |
+
|
1288 |
+
|
1289 |
+
13.
|
1290 |
+
Score:
|
1291 |
+
|
1292 |
+
|
1293 |
+
|
1294 |
+
1
|
1295 |
+
2
|
1296 |
+
3
|
1297 |
+
4
|
1298 |
+
5
|
1299 |
+
6
|
1300 |
+
7
|
1301 |
+
8
|
1302 |
+
9
|
1303 |
+
10
|
1304 |
+
Top-10
|
1305 |
+
Avg
|
1306 |
+
Origin
|
1307 |
+
0
|
1308 |
+
0
|
1309 |
+
0
|
1310 |
+
0
|
1311 |
+
0
|
1312 |
+
1
|
1313 |
+
0
|
1314 |
+
0
|
1315 |
+
0
|
1316 |
+
0
|
1317 |
+
1
|
1318 |
+
0.1
|
1319 |
+
|
1320 |
+
|
1321 |
+
Origin:
|
1322 |
+
Problem:
|
1323 |
+
I have a numpy array x = np.array([145100, [ 1,2,3 ], [6,5,4]]) and I wish to ravel it to this: [145100, 1,2,3 , 6,5,4]
|
1324 |
+
I tried this, but it didn't give any results:
|
1325 |
+
x = np.ravel(x)
|
1326 |
+
As the shape was still (3,) instead of (5,). What am I missing?
|
1327 |
+
|
1328 |
+
A:
|
1329 |
+
<code>
|
1330 |
+
import numpy as np
|
1331 |
+
x = np.array([145100, [1, 2, 3], [6,5,4]])
|
1332 |
+
### BEGIN SOLUTION
|
1333 |
+
[insert]
|
1334 |
+
### END SOLUTION
|
1335 |
+
print(ans)
|
1336 |
+
</code>
|
1337 |
+
|
1338 |
+
Test:
|
1339 |
+
a = np.hstack(x)
|
1340 |
+
try:
|
1341 |
+
np.testing.assert_array_equal(a, ans)
|
1342 |
+
print('Test passed!')
|
1343 |
+
except:
|
1344 |
+
print('Test failed...')
|
1345 |
+
|
1346 |
+
|
1347 |
+
|
1348 |
+
14.
|
1349 |
+
Score:
|
1350 |
+
|
1351 |
+
|
1352 |
+
|
1353 |
+
1
|
1354 |
+
2
|
1355 |
+
3
|
1356 |
+
4
|
1357 |
+
5
|
1358 |
+
6
|
1359 |
+
7
|
1360 |
+
8
|
1361 |
+
9
|
1362 |
+
10
|
1363 |
+
Top-10
|
1364 |
+
Avg
|
1365 |
+
Origin
|
1366 |
+
0
|
1367 |
+
1
|
1368 |
+
0
|
1369 |
+
0
|
1370 |
+
0
|
1371 |
+
0
|
1372 |
+
1
|
1373 |
+
0
|
1374 |
+
0
|
1375 |
+
0
|
1376 |
+
1
|
1377 |
+
0.2
|
1378 |
+
|
1379 |
+
|
1380 |
+
Origin:
|
1381 |
+
Problem:
|
1382 |
+
I have an array H of dimension MxN, and an array A of dimension M . I want to scale H rows with array A. I do it this way, taking advantage of element-wise behavior of Numpy
|
1383 |
+
H = numpy.swapaxes(H, 0, 1)
|
1384 |
+
H /= A
|
1385 |
+
H = numpy.swapaxes(H, 0, 1)
|
1386 |
+
|
1387 |
+
It works, but the two swapaxes operations are not very elegant, and I feel there is a more elegant and concise way to achieve the result, without creating temporaries. Would you tell me how ?
|
1388 |
+
|
1389 |
+
A:
|
1390 |
+
<code>
|
1391 |
+
import numpy as np
|
1392 |
+
H = np.array([[ 1.05550870e+00, -1.54640644e-01, 2.01796906e+00],
|
1393 |
+
[6.59741375e-02, 4.69242500e-01, -5.57339470e-03],
|
1394 |
+
[-2.12376646e-01, -9.17792113e-01, -1.20153176e+00],
|
1395 |
+
[3.68068789e-01, -9.98131619e+00, -1.14438249e+01]])
|
1396 |
+
A = np.array([ 1.1845468 , 1.30376536, -0.44912446, 0.04675434])
|
1397 |
+
### BEGIN SOLUTION
|
1398 |
+
[insert]
|
1399 |
+
### END SOLUTION
|
1400 |
+
print(ans)
|
1401 |
+
</code>
|
1402 |
+
|
1403 |
+
Test:
|
1404 |
+
a = H/A[:, None]
|
1405 |
+
try:
|
1406 |
+
np.testing.assert_array_equal(a, ans)
|
1407 |
+
print('Test passed!')
|
1408 |
+
except:
|
1409 |
+
print('Test failed...')
|
1410 |
+
+for detection
|
1411 |
+
|
1412 |
+
|
1413 |
+
|
1414 |
+
15.
|
1415 |
+
Score:
|
1416 |
+
|
1417 |
+
|
1418 |
+
|
1419 |
+
1
|
1420 |
+
2
|
1421 |
+
3
|
1422 |
+
4
|
1423 |
+
5
|
1424 |
+
6
|
1425 |
+
7
|
1426 |
+
8
|
1427 |
+
9
|
1428 |
+
10
|
1429 |
+
Top-10
|
1430 |
+
Avg
|
1431 |
+
Origin
|
1432 |
+
1
|
1433 |
+
0
|
1434 |
+
0
|
1435 |
+
0
|
1436 |
+
0
|
1437 |
+
0
|
1438 |
+
0
|
1439 |
+
0
|
1440 |
+
0
|
1441 |
+
0
|
1442 |
+
1
|
1443 |
+
0.1
|
1444 |
+
|
1445 |
+
|
1446 |
+
Origin:
|
1447 |
+
Problem:
|
1448 |
+
I am trying to convert a string into n-dimensioned numpy array (x, 4, 4). Basic requirement is a 4x4 array with column major filling of values. We will use as many 4x4 arrays as per the length of the input string. For example if my string is:
|
1449 |
+
|
1450 |
+
'A quick brown fox jumps over dog'
|
1451 |
+
|
1452 |
+
The resultant array should look like this:
|
1453 |
+
|
1454 |
+
[[['A' 'i' 'b' 'n']
|
1455 |
+
[' ' 'c' 'r' ' ']
|
1456 |
+
['q' 'k' 'o' 'f']
|
1457 |
+
['u' ' ' 'w' 'o']]
|
1458 |
+
|
1459 |
+
[['x' 'm' 'o' ' ']
|
1460 |
+
[' ' 'p' 'v' 'd']
|
1461 |
+
['j' 's' 'e' 'o']
|
1462 |
+
['u' ' ' 'r' 'g']]]
|
1463 |
+
Note that instead of the conventional row-first filling of values requirement is for the filling to be column first within the 4x4 subarray.
|
1464 |
+
|
1465 |
+
A:
|
1466 |
+
<code>
|
1467 |
+
import numpy as np
|
1468 |
+
string = 'A quick brown fox jumps over dog'
|
1469 |
+
#BEGIN SOLUTION
|
1470 |
+
[insert]
|
1471 |
+
### END SOLUTION
|
1472 |
+
print(ans)
|
1473 |
+
</code>
|
1474 |
+
|
1475 |
+
test:
|
1476 |
+
matrix2 = np.array(list(string)).reshape(-1,4,4).swapaxes(1,2)
|
1477 |
+
try:
|
1478 |
+
np.testing.assert_array_equal(matrix2, ans)
|
1479 |
+
print('Test passed!')
|
1480 |
+
except:
|
1481 |
+
print('Test failed...')
|
1482 |
+
|
1483 |
+
|
1484 |
+
|
1485 |
+
|
1486 |
+
16.
|
1487 |
+
Score:
|
1488 |
+
|
1489 |
+
|
1490 |
+
|
1491 |
+
1
|
1492 |
+
2
|
1493 |
+
3
|
1494 |
+
4
|
1495 |
+
5
|
1496 |
+
6
|
1497 |
+
7
|
1498 |
+
8
|
1499 |
+
9
|
1500 |
+
10
|
1501 |
+
Top-10
|
1502 |
+
Avg
|
1503 |
+
Origin
|
1504 |
+
0
|
1505 |
+
0
|
1506 |
+
0
|
1507 |
+
0
|
1508 |
+
0
|
1509 |
+
0
|
1510 |
+
0
|
1511 |
+
0
|
1512 |
+
0
|
1513 |
+
0
|
1514 |
+
0
|
1515 |
+
0
|
1516 |
+
|
1517 |
+
|
1518 |
+
Origin:
|
1519 |
+
Problem:
|
1520 |
+
Consider the following arrays:
|
1521 |
+
a = np.array([0,1])[:,None]
|
1522 |
+
b = np.array([1,2,3])
|
1523 |
+
print(a)
|
1524 |
+
array([[0],
|
1525 |
+
[1]])
|
1526 |
+
print(b)
|
1527 |
+
b = np.array([1,2,3])
|
1528 |
+
Is there a simple way to concatenate these two arrays in a way that the latter is broadcast, in order to obtain the following?
|
1529 |
+
|
1530 |
+
array([[0, 1, 2, 3],
|
1531 |
+
[1, 1, 2, 3]])
|
1532 |
+
|
1533 |
+
A:
|
1534 |
+
<code>
|
1535 |
+
import numpy as np
|
1536 |
+
a = np.array([0,1])[:,None]
|
1537 |
+
b = np.array([1,2,3])
|
1538 |
+
#BEGIN SOLUTION
|
1539 |
+
[insert]
|
1540 |
+
### END SOLUTION
|
1541 |
+
print(ans)
|
1542 |
+
</code>
|
1543 |
+
|
1544 |
+
test:
|
1545 |
+
b_new = np.broadcast_to(b,(a.shape[0],b.shape[0]))
|
1546 |
+
c = np.concatenate((a,b_new),axis=1)
|
1547 |
+
|
1548 |
+
try:
|
1549 |
+
np.testing.assert_array_equal(c, ans)
|
1550 |
+
print('Test passed!')
|
1551 |
+
except:
|
1552 |
+
print('Test failed...')
|
1553 |
+
|
1554 |
+
|
1555 |
+
|
1556 |
+
17.
|
1557 |
+
Score:
|
1558 |
+
|
1559 |
+
|
1560 |
+
|
1561 |
+
1
|
1562 |
+
2
|
1563 |
+
3
|
1564 |
+
4
|
1565 |
+
5
|
1566 |
+
6
|
1567 |
+
7
|
1568 |
+
8
|
1569 |
+
9
|
1570 |
+
10
|
1571 |
+
Top-10
|
1572 |
+
Avg
|
1573 |
+
Origin
|
1574 |
+
1
|
1575 |
+
0
|
1576 |
+
1
|
1577 |
+
1
|
1578 |
+
0
|
1579 |
+
1
|
1580 |
+
1
|
1581 |
+
0
|
1582 |
+
1
|
1583 |
+
0
|
1584 |
+
1
|
1585 |
+
0.6
|
1586 |
+
A4
|
1587 |
+
0
|
1588 |
+
0
|
1589 |
+
0
|
1590 |
+
0
|
1591 |
+
0
|
1592 |
+
0
|
1593 |
+
0
|
1594 |
+
0
|
1595 |
+
1
|
1596 |
+
0
|
1597 |
+
1
|
1598 |
+
0.1
|
1599 |
+
|
1600 |
+
|
1601 |
+
Origin:
|
1602 |
+
Problem:
|
1603 |
+
Is there a Pythonic way to calculate the array z without using the loop?
|
1604 |
+
|
1605 |
+
import numpy as np
|
1606 |
+
x = np.array([[1, 2, 3], [6, 7, 8]])
|
1607 |
+
y = np.array([5, 8])
|
1608 |
+
z = np.array([x[i] * y[i] for i in range(0, len(x))])
|
1609 |
+
|
1610 |
+
A:
|
1611 |
+
<code>
|
1612 |
+
import numpy as np
|
1613 |
+
x = np.array([[1, 2, 3], [6, 7, 8]])
|
1614 |
+
y = np.array([5, 8])
|
1615 |
+
#BEGIN SOLUTION
|
1616 |
+
[insert]
|
1617 |
+
### END SOLUTION
|
1618 |
+
print(ans)
|
1619 |
+
</code>
|
1620 |
+
|
1621 |
+
test:
|
1622 |
+
z = x * np.expand_dims(y, 1)
|
1623 |
+
|
1624 |
+
try:
|
1625 |
+
np.testing.assert_array_equal(z, ans)
|
1626 |
+
print('Test passed!')
|
1627 |
+
except:
|
1628 |
+
print('Test failed...')
|
1629 |
+
+for detection
|
1630 |
+
|
1631 |
+
A0:
|
1632 |
+
Problem:
|
1633 |
+
Is there a Pythonic way to calculate the array z without using the loop?
|
1634 |
+
|
1635 |
+
import numpy as np
|
1636 |
+
x = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
|
1637 |
+
y = np.array([5, 8, 10])
|
1638 |
+
z = np.array([x[i] * y[i] for i in range(0, len(x))])
|
1639 |
+
|
1640 |
+
A:
|
1641 |
+
<code>
|
1642 |
+
import numpy as np
|
1643 |
+
x = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
|
1644 |
+
y = np.array([5, 8, 10])
|
1645 |
+
#BEGIN SOLUTION
|
1646 |
+
[insert]
|
1647 |
+
### END SOLUTION
|
1648 |
+
print(ans)
|
1649 |
+
</code>
|
1650 |
+
|
1651 |
+
Test:
|
1652 |
+
z = x * np.expand_dims(y, 1)
|
1653 |
+
|
1654 |
+
try:
|
1655 |
+
np.testing.assert_array_equal(z, ans)
|
1656 |
+
print('Test passed!')
|
1657 |
+
except:
|
1658 |
+
print('Test failed...')
|
1659 |
+
+for detection
|
1660 |
+
|
1661 |
+
|
1662 |
+
|
1663 |
+
18.
|
1664 |
+
Score:
|
1665 |
+
|
1666 |
+
|
1667 |
+
|
1668 |
+
1
|
1669 |
+
2
|
1670 |
+
3
|
1671 |
+
4
|
1672 |
+
5
|
1673 |
+
6
|
1674 |
+
7
|
1675 |
+
8
|
1676 |
+
9
|
1677 |
+
10
|
1678 |
+
Top-10
|
1679 |
+
Avg
|
1680 |
+
Origin
|
1681 |
+
0
|
1682 |
+
0
|
1683 |
+
0
|
1684 |
+
0
|
1685 |
+
0
|
1686 |
+
0
|
1687 |
+
0
|
1688 |
+
0
|
1689 |
+
0
|
1690 |
+
0
|
1691 |
+
0
|
1692 |
+
0
|
1693 |
+
|
1694 |
+
|
1695 |
+
Origin:
|
1696 |
+
Problem:
|
1697 |
+
I have a table in a Python script with numpy in the following shape:
|
1698 |
+
|
1699 |
+
[array([[a1, b1, c1], ..., [x1, y1, z1]]),
|
1700 |
+
array([a2, b2, c2, ..., x2, y2, z2])
|
1701 |
+
]
|
1702 |
+
I would like to reshape it to a format like this:
|
1703 |
+
|
1704 |
+
(array([[a2],
|
1705 |
+
[b2],
|
1706 |
+
.
|
1707 |
+
.
|
1708 |
+
.
|
1709 |
+
[z2]],
|
1710 |
+
dtype = ...),
|
1711 |
+
array([[a1],
|
1712 |
+
[b1],
|
1713 |
+
.
|
1714 |
+
.
|
1715 |
+
.
|
1716 |
+
[z1]])
|
1717 |
+
)
|
1718 |
+
To be honest, I'm also quite confused about the different parentheses. array1, array2] is a list of arrays, right? What is (array1, array2), then?
|
1719 |
+
|
1720 |
+
|
1721 |
+
A:
|
1722 |
+
<code>
|
1723 |
+
import numpy as np
|
1724 |
+
a = [
|
1725 |
+
np.array([[1, 2, 3], [4, 5, 6]]),
|
1726 |
+
np.array([10, 11, 12, 13, 14])
|
1727 |
+
]
|
1728 |
+
#BEGIN SOLUTION
|
1729 |
+
[insert]
|
1730 |
+
### END SOLUTION
|
1731 |
+
print(ans)
|
1732 |
+
</code>
|
1733 |
+
|
1734 |
+
Test:
|
1735 |
+
b = (
|
1736 |
+
np.expand_dims(a[1], axis=1),
|
1737 |
+
np.expand_dims(a[0].flatten(), axis=1)
|
1738 |
+
)
|
1739 |
+
|
1740 |
+
try:
|
1741 |
+
np.testing.assert_array_equal(b, ans)
|
1742 |
+
print('Test passed!')
|
1743 |
+
except:
|
1744 |
+
print('Test failed...')
|
1745 |
+
+for detection
|
1746 |
+
|
1747 |
+
|
1748 |
+
19.
|
1749 |
+
Score:
|
1750 |
+
|
1751 |
+
|
1752 |
+
|
1753 |
+
1
|
1754 |
+
2
|
1755 |
+
3
|
1756 |
+
4
|
1757 |
+
5
|
1758 |
+
6
|
1759 |
+
7
|
1760 |
+
8
|
1761 |
+
9
|
1762 |
+
10
|
1763 |
+
Top-10
|
1764 |
+
Avg
|
1765 |
+
Origin
|
1766 |
+
1
|
1767 |
+
1
|
1768 |
+
0
|
1769 |
+
1
|
1770 |
+
1
|
1771 |
+
0
|
1772 |
+
1
|
1773 |
+
1
|
1774 |
+
0
|
1775 |
+
0
|
1776 |
+
1
|
1777 |
+
0.6
|
1778 |
+
A4
|
1779 |
+
0
|
1780 |
+
0
|
1781 |
+
0
|
1782 |
+
0
|
1783 |
+
0
|
1784 |
+
0
|
1785 |
+
0
|
1786 |
+
0
|
1787 |
+
0
|
1788 |
+
0
|
1789 |
+
0
|
1790 |
+
0
|
1791 |
+
|
1792 |
+
|
1793 |
+
Origin:
|
1794 |
+
Problem:
|
1795 |
+
I have a three dimensional numpy source array and a two-dimensional numpy array of indexes.
|
1796 |
+
|
1797 |
+
For example:
|
1798 |
+
|
1799 |
+
src = np.array([[[1,2,3],[4,5,6]],
|
1800 |
+
[[7,8,9],[10,11,12]]])
|
1801 |
+
idx = np.array([[0,1],
|
1802 |
+
[1,2]])
|
1803 |
+
I'd like to get a 2d array, where each element represents the indexed value in the innermost dimension in that position:
|
1804 |
+
|
1805 |
+
array([[1,5],
|
1806 |
+
[8,12]])
|
1807 |
+
How do I do this with numpy?
|
1808 |
+
|
1809 |
+
A:
|
1810 |
+
<code>
|
1811 |
+
import numpy as np
|
1812 |
+
src = np.array([[[1,2,3],[4,5,6]],
|
1813 |
+
[[7,8,9],[10,11,12]]])
|
1814 |
+
idx = np.array([[0,1],
|
1815 |
+
[1,2]])
|
1816 |
+
#BEGIN SOLUTION
|
1817 |
+
[insert]
|
1818 |
+
### END SOLUTION
|
1819 |
+
print(ans)
|
1820 |
+
</code>
|
1821 |
+
|
1822 |
+
Test:
|
1823 |
+
idx = np.expand_dims(idx, axis=-1)
|
1824 |
+
res = np.take_along_axis(src, idx, axis=2).squeeze(-1)
|
1825 |
+
|
1826 |
+
try:
|
1827 |
+
np.testing.assert_array_equal(res, ans)
|
1828 |
+
print('Test passed!')
|
1829 |
+
except:
|
1830 |
+
print('Test failed...')
|
1831 |
+
|
1832 |
+
A4:
|
1833 |
+
Problem:
|
1834 |
+
I have a three dimensional numpy source array and a two-dimensional numpy array of indexes.
|
1835 |
+
|
1836 |
+
For example:
|
1837 |
+
|
1838 |
+
src = np.array([[[1,2,3],[4,5,6]],
|
1839 |
+
[[7,8,9],[10,11,12]]])
|
1840 |
+
idx = np.array([[0,2],
|
1841 |
+
[1,2]])
|
1842 |
+
I'd like to get a 2d array:
|
1843 |
+
|
1844 |
+
array([[1,5],
|
1845 |
+
[9,12]])
|
1846 |
+
For example, the 5 on the top right corresponds to the 1st element of [4, 5, 6], and the 9 on the bottom left corresponds to the 2nd element of [7, 8, 9]
|
1847 |
+
In other words, the indices on idx[0, 1] and idx[1, 0] corresponds to src[1, 0] and src[0, 1]
|
1848 |
+
How do I do this with numpy?
|
1849 |
+
|
1850 |
+
A:
|
1851 |
+
<code>
|
1852 |
+
import numpy as np
|
1853 |
+
src = np.array([[[1,2,3],[4,5,6]],
|
1854 |
+
[[7,8,9],[10,11,12]]])
|
1855 |
+
idx = np.array([[0,2],
|
1856 |
+
[1,2]])
|
1857 |
+
#BEGIN SOLUTION
|
1858 |
+
[insert]
|
1859 |
+
### END SOLUTION
|
1860 |
+
print(ans)
|
1861 |
+
</code>
|
1862 |
+
|
1863 |
+
Test:
|
1864 |
+
idx = np.expand_dims(idx.T, axis=-1)
|
1865 |
+
res = np.take_along_axis(src, idx, axis=2).squeeze(-1)
|
1866 |
+
|
1867 |
+
try:
|
1868 |
+
np.testing.assert_array_equal(res, ans)
|
1869 |
+
print('Test passed!')
|
1870 |
+
except:
|
1871 |
+
print('Test failed...')
|
1872 |
+
+for detection
|
1873 |
+
|
1874 |
+
|
1875 |
+
|
1876 |
+
20.
|
1877 |
+
Score:
|
1878 |
+
|
1879 |
+
|
1880 |
+
|
1881 |
+
1
|
1882 |
+
2
|
1883 |
+
3
|
1884 |
+
4
|
1885 |
+
5
|
1886 |
+
6
|
1887 |
+
7
|
1888 |
+
8
|
1889 |
+
9
|
1890 |
+
10
|
1891 |
+
Top-10
|
1892 |
+
Avg
|
1893 |
+
Origin
|
1894 |
+
0
|
1895 |
+
0
|
1896 |
+
0
|
1897 |
+
0
|
1898 |
+
0
|
1899 |
+
0
|
1900 |
+
0
|
1901 |
+
0
|
1902 |
+
1
|
1903 |
+
0
|
1904 |
+
1
|
1905 |
+
0.1
|
1906 |
+
|
1907 |
+
|
1908 |
+
Origin:
|
1909 |
+
Problem:
|
1910 |
+
I have an issue in applying argmax to an array which has multiple brackets. In real life I am getting this as a result of a pytorch tensor. Here I can put an example:
|
1911 |
+
|
1912 |
+
a = np.array([[1.0, 1.1],[2.1,2.0]])
|
1913 |
+
np.argmax(a,axis=1)
|
1914 |
+
|
1915 |
+
array([1, 0])
|
1916 |
+
It is correct. But:
|
1917 |
+
|
1918 |
+
a = np.array([[[1.0, 1.1]],[[2.1,2.0]]])
|
1919 |
+
np.argmax(a,axis=1)
|
1920 |
+
|
1921 |
+
array([[0, 0],
|
1922 |
+
[0, 0]])
|
1923 |
+
It does not give me what I expect. Consider that in reality I have this level of inner brackets:
|
1924 |
+
|
1925 |
+
a = np.array([[[[1.0, 1.1]]],[[[2.1,2.0]]]])
|
1926 |
+
|
1927 |
+
A:
|
1928 |
+
<code>
|
1929 |
+
import numpy as np
|
1930 |
+
a = np.array([[[[1.0, 1.1]]], [[[2.1, 2.0]]]])
|
1931 |
+
#BEGIN SOLUTION
|
1932 |
+
[insert]
|
1933 |
+
### END SOLUTION
|
1934 |
+
print(ans)
|
1935 |
+
</code>
|
1936 |
+
|
1937 |
+
Test:
|
1938 |
+
try:
|
1939 |
+
np.testing.assert_array_equal(np.argmax(a, axis=-1).squeeze(), ans)
|
1940 |
+
print('Test passed!')
|
1941 |
+
except:
|
1942 |
+
print('Test failed...')
|
1943 |
+
|
1944 |
+
|
1945 |
+
|
1946 |
+
|
1947 |
+
21.
|
1948 |
+
Score:
|
1949 |
+
|
1950 |
+
|
1951 |
+
|
1952 |
+
1
|
1953 |
+
2
|
1954 |
+
3
|
1955 |
+
4
|
1956 |
+
5
|
1957 |
+
6
|
1958 |
+
7
|
1959 |
+
8
|
1960 |
+
9
|
1961 |
+
10
|
1962 |
+
Top-10
|
1963 |
+
Avg
|
1964 |
+
Origin
|
1965 |
+
0
|
1966 |
+
0
|
1967 |
+
0
|
1968 |
+
1
|
1969 |
+
1
|
1970 |
+
1
|
1971 |
+
1
|
1972 |
+
0
|
1973 |
+
0
|
1974 |
+
1
|
1975 |
+
1
|
1976 |
+
0.5
|
1977 |
+
|
1978 |
+
|
1979 |
+
Origin:
|
1980 |
+
Problem:
|
1981 |
+
I have a large list files that contain 2D numpy arrays pickled through numpy.save. I am trying to read the first column of each file and create a new 2D array.
|
1982 |
+
|
1983 |
+
I currently read each column using numpy.load with a mmap. The 1D arrays are now in a list.
|
1984 |
+
|
1985 |
+
col_list = []
|
1986 |
+
for f in file_list:
|
1987 |
+
Temp = np.load(f,mmap_mode='r')
|
1988 |
+
col_list.append(Temp[:,0])
|
1989 |
+
How can I convert this into a 2D array?
|
1990 |
+
|
1991 |
+
A:
|
1992 |
+
<code>
|
1993 |
+
import numpy as np
|
1994 |
+
def f(arrays):
|
1995 |
+
### BEGIN SOLUTION
|
1996 |
+
[insert]
|
1997 |
+
### END SOLUTION
|
1998 |
+
return result
|
1999 |
+
</code>
|
2000 |
+
|
2001 |
+
test:
|
2002 |
+
arrs = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9])]
|
2003 |
+
try:
|
2004 |
+
np.testing.assert_array_equal(f(arrs), np.stack(arrs, axis=0))
|
2005 |
+
print('Test passed!')
|
2006 |
+
except:
|
2007 |
+
print('Test failed...')
|
2008 |
+
|
2009 |
+
22.
|
2010 |
+
Score:
|
2011 |
+
|
2012 |
+
|
2013 |
+
|
2014 |
+
1
|
2015 |
+
2
|
2016 |
+
3
|
2017 |
+
4
|
2018 |
+
5
|
2019 |
+
6
|
2020 |
+
7
|
2021 |
+
8
|
2022 |
+
9
|
2023 |
+
10
|
2024 |
+
Top-10
|
2025 |
+
Avg
|
2026 |
+
Origin
|
2027 |
+
0
|
2028 |
+
1
|
2029 |
+
0
|
2030 |
+
0
|
2031 |
+
0
|
2032 |
+
0
|
2033 |
+
0
|
2034 |
+
0
|
2035 |
+
0
|
2036 |
+
0
|
2037 |
+
1
|
2038 |
+
0.1
|
2039 |
+
|
2040 |
+
|
2041 |
+
Origin:
|
2042 |
+
Problem:
|
2043 |
+
I.m facing a little issue to combine arrays in a certain manner. Let's say we have
|
2044 |
+
|
2045 |
+
a=array([[1,1,1],[2,2,2],[3,3,3]])
|
2046 |
+
|
2047 |
+
b=array([[10,10,10],[20,20,20],[30,30,30]])
|
2048 |
+
I wish to get
|
2049 |
+
|
2050 |
+
c=array([[[1,1,1],[10,10,10]],[[2,2,2],[20,20,20]],[[3,3,3],[30,30,30]]])
|
2051 |
+
The real issue is that my arrays a and b are much longer than 3 coordinates!
|
2052 |
+
|
2053 |
+
A:
|
2054 |
+
<code>
|
2055 |
+
import numpy as np
|
2056 |
+
a = np.array([[1,1,1],[2,2,2],[3,3,3], [4,4,4]])
|
2057 |
+
b = np.array([[10,10,10],[20,20,20],[30,30,30], [40, 40, 40]])
|
2058 |
+
### BEGIN SOLUTION
|
2059 |
+
[insert]
|
2060 |
+
### END SOLUTION
|
2061 |
+
print(ans)
|
2062 |
+
</code>
|
2063 |
+
|
2064 |
+
test:
|
2065 |
+
c = np.concatenate((a[:, None, :], b[:, None, :]), axis=1)
|
2066 |
+
try:
|
2067 |
+
np.testing.assert_array_equal(c, ans)
|
2068 |
+
print('Test passed!')
|
2069 |
+
except:
|
2070 |
+
print('Test failed...')
|
2071 |
+
|
2072 |
+
+for detection
|
2073 |
+
|
2074 |
+
23.
|
2075 |
+
Score:
|
2076 |
+
|
2077 |
+
|
2078 |
+
|
2079 |
+
1
|
2080 |
+
2
|
2081 |
+
3
|
2082 |
+
4
|
2083 |
+
5
|
2084 |
+
6
|
2085 |
+
7
|
2086 |
+
8
|
2087 |
+
9
|
2088 |
+
10
|
2089 |
+
Top-10
|
2090 |
+
Avg
|
2091 |
+
Origin
|
2092 |
+
0
|
2093 |
+
0
|
2094 |
+
0
|
2095 |
+
0
|
2096 |
+
1
|
2097 |
+
0
|
2098 |
+
0
|
2099 |
+
1
|
2100 |
+
0
|
2101 |
+
0
|
2102 |
+
1
|
2103 |
+
0.2
|
2104 |
+
A7
|
2105 |
+
0
|
2106 |
+
0
|
2107 |
+
0
|
2108 |
+
0
|
2109 |
+
0
|
2110 |
+
0
|
2111 |
+
0
|
2112 |
+
0
|
2113 |
+
0
|
2114 |
+
0
|
2115 |
+
0
|
2116 |
+
0
|
2117 |
+
|
2118 |
+
|
2119 |
+
Origin:
|
2120 |
+
Problem:
|
2121 |
+
I currently looking for method in which i can split a ndarray into smaller ndarrays.
|
2122 |
+
|
2123 |
+
example: given ndarray with shape (78,1440,3), from which i want to extract a list of smaller ndarrays of the size (78,72,3), that would be 20 smaller sub ndarrays.
|
2124 |
+
|
2125 |
+
I tried using numpy.split.
|
2126 |
+
|
2127 |
+
numpy.split(matrix,72,axis=1)
|
2128 |
+
which generates a list of length 72 and the first entry has the shape (78,20,3)..
|
2129 |
+
|
2130 |
+
Why am I not able to extract the size I need?
|
2131 |
+
|
2132 |
+
A:
|
2133 |
+
<code>
|
2134 |
+
import numpy as np
|
2135 |
+
matrix = np.random.rand(78,1440,3)
|
2136 |
+
### BEGIN SOLUTION
|
2137 |
+
[insert]
|
2138 |
+
### END SOLUTION
|
2139 |
+
print(ans)
|
2140 |
+
</code>
|
2141 |
+
|
2142 |
+
Test:
|
2143 |
+
c = np.split(matrix, matrix.shape[1]//72, axis=1)
|
2144 |
+
|
2145 |
+
try:
|
2146 |
+
np.testing.assert_array_equal(c, ans)
|
2147 |
+
print('Test passed!')
|
2148 |
+
except:
|
2149 |
+
print('Test failed...')
|
2150 |
+
|
2151 |
+
|
2152 |
+
A7:
|
2153 |
+
Problem:
|
2154 |
+
I currently looking for method in which i can split a ndarray into smaller ndarrays.
|
2155 |
+
|
2156 |
+
example: given ndarray with shape (78,1440,3), from which i want to extract a list of smaller ndarrays of the size (78,73,3).
|
2157 |
+
|
2158 |
+
Note that if shape[1] is not divisible by new size(in this example: 1440 is not divisible by 73), then fill zeros on the axis until it is dividible.
|
2159 |
+
|
2160 |
+
Why am I not able to extract the size I need?
|
2161 |
+
|
2162 |
+
A:
|
2163 |
+
<code>
|
2164 |
+
import numpy as np
|
2165 |
+
matrix = np.random.rand(78,1440,3)
|
2166 |
+
### BEGIN SOLUTION
|
2167 |
+
[insert]
|
2168 |
+
### END SOLUTION
|
2169 |
+
print(ans)
|
2170 |
+
</code>
|
2171 |
+
|
2172 |
+
Test:
|
2173 |
+
t = matrix.shape[1] // 73
|
2174 |
+
if t * 73 < matrix.shape[1]:
|
2175 |
+
new_arr = np.zeros((78, (t+1)*73-1440, 3))
|
2176 |
+
matrix = np.hstack([matrix, new_arr])
|
2177 |
+
c = np.split(matrix, matrix.shape[1] // 73, axis = 1)
|
2178 |
+
|
2179 |
+
try:
|
2180 |
+
np.testing.assert_array_equal(c, ans)
|
2181 |
+
print('Test passed!')
|
2182 |
+
except:
|
2183 |
+
print('Test failed...')
|
2184 |
+
|
2185 |
+
|
2186 |
+
|
2187 |
+
|
2188 |
+
|
2189 |
+
24.
|
2190 |
+
Score:
|
2191 |
+
|
2192 |
+
|
2193 |
+
|
2194 |
+
1
|
2195 |
+
2
|
2196 |
+
3
|
2197 |
+
4
|
2198 |
+
5
|
2199 |
+
6
|
2200 |
+
7
|
2201 |
+
8
|
2202 |
+
9
|
2203 |
+
10
|
2204 |
+
Top-10
|
2205 |
+
Avg
|
2206 |
+
Origin
|
2207 |
+
0
|
2208 |
+
0
|
2209 |
+
0
|
2210 |
+
0
|
2211 |
+
0
|
2212 |
+
0
|
2213 |
+
0
|
2214 |
+
0
|
2215 |
+
0
|
2216 |
+
0
|
2217 |
+
0
|
2218 |
+
0
|
2219 |
+
|
2220 |
+
|
2221 |
+
Origin:
|
2222 |
+
Problem:
|
2223 |
+
Suppose I have an array like:
|
2224 |
+
|
2225 |
+
import numpy as np
|
2226 |
+
|
2227 |
+
np.array([[0, 0, 0],
|
2228 |
+
[1, 1, 1]])
|
2229 |
+
Here has shape (2,3) but it can be (n,3). I would like to transform it into a list of arrays representing columns.
|
2230 |
+
|
2231 |
+
Desired Output
|
2232 |
+
|
2233 |
+
[array([[0],[1]]), array([[0],[1]]), array([[0],[1]])]
|
2234 |
+
I tried list comprehension, reshape etc. but I did not manage to get there.
|
2235 |
+
|
2236 |
+
A:
|
2237 |
+
<code>
|
2238 |
+
import numpy as np
|
2239 |
+
|
2240 |
+
a=np.array([[0, 0, 0],[1, 1, 1]])
|
2241 |
+
### BEGIN SOLUTION
|
2242 |
+
[insert]
|
2243 |
+
### END SOLUTION
|
2244 |
+
print(ans)
|
2245 |
+
</code>
|
2246 |
+
|
2247 |
+
Test:
|
2248 |
+
c = [np.hsplit(a,3)]
|
2249 |
+
|
2250 |
+
try:
|
2251 |
+
np.testing.assert_array_equal(c, ans)
|
2252 |
+
print('Test passed!')
|
2253 |
+
except:
|
2254 |
+
print('Test failed...')
|
2255 |
+
+for detection
|
2256 |
+
|
2257 |
+
25.
|
2258 |
+
Score:
|
2259 |
+
|
2260 |
+
|
2261 |
+
|
2262 |
+
1
|
2263 |
+
2
|
2264 |
+
3
|
2265 |
+
4
|
2266 |
+
5
|
2267 |
+
6
|
2268 |
+
7
|
2269 |
+
8
|
2270 |
+
9
|
2271 |
+
10
|
2272 |
+
Top-10
|
2273 |
+
Avg
|
2274 |
+
Origin
|
2275 |
+
0
|
2276 |
+
0
|
2277 |
+
0
|
2278 |
+
0
|
2279 |
+
0
|
2280 |
+
0
|
2281 |
+
0
|
2282 |
+
0
|
2283 |
+
0
|
2284 |
+
0
|
2285 |
+
0
|
2286 |
+
0
|
2287 |
+
|
2288 |
+
|
2289 |
+
Origin:
|
2290 |
+
Problem:
|
2291 |
+
I have a numpy array of size nxm. I want the number of columns to be limited to k and the rest of the columns to be extended in new rows. Following is the scenario -
|
2292 |
+
|
2293 |
+
Initial array: nxm
|
2294 |
+
Final array: pxk
|
2295 |
+
where p = (m/k)*n
|
2296 |
+
|
2297 |
+
Eg. n = 2, m = 6, k = 2
|
2298 |
+
|
2299 |
+
Initial array:
|
2300 |
+
[[1, 2, 3, 4, 5, 6,],
|
2301 |
+
[7, 8, 9, 10, 11, 12]]
|
2302 |
+
|
2303 |
+
Final array:
|
2304 |
+
[[1, 2],
|
2305 |
+
[7, 8],
|
2306 |
+
[3, 4],
|
2307 |
+
[9, 10],
|
2308 |
+
[5, 6],
|
2309 |
+
[11, 12]]
|
2310 |
+
|
2311 |
+
I tried using reshape but I did not get the desired result.
|
2312 |
+
|
2313 |
+
A:
|
2314 |
+
<code>
|
2315 |
+
import numpy as np
|
2316 |
+
|
2317 |
+
q = np.array([[1, 2, 3, 4, 5, 6,], [7, 8, 9, 10, 11, 12]])
|
2318 |
+
### BEGIN SOLUTION
|
2319 |
+
[insert]
|
2320 |
+
### END SOLUTION
|
2321 |
+
print(ans)
|
2322 |
+
</code>
|
2323 |
+
|
2324 |
+
test:
|
2325 |
+
c = q.T.reshape(-1,2,2).swapaxes(1,2).reshape(-1,2)
|
2326 |
+
|
2327 |
+
try:
|
2328 |
+
np.testing.assert_array_equal(c, ans)
|
2329 |
+
print('Test passed!')
|
2330 |
+
except:
|
2331 |
+
print('Test failed...')
|
2332 |
+
|
2333 |
+
|
2334 |
+
26.
|
2335 |
+
Score:
|
2336 |
+
|
2337 |
+
|
2338 |
+
|
2339 |
+
1
|
2340 |
+
2
|
2341 |
+
3
|
2342 |
+
4
|
2343 |
+
5
|
2344 |
+
6
|
2345 |
+
7
|
2346 |
+
8
|
2347 |
+
9
|
2348 |
+
10
|
2349 |
+
Top-10
|
2350 |
+
Avg
|
2351 |
+
Origin
|
2352 |
+
0
|
2353 |
+
0
|
2354 |
+
0
|
2355 |
+
0
|
2356 |
+
0
|
2357 |
+
0
|
2358 |
+
0
|
2359 |
+
0
|
2360 |
+
0
|
2361 |
+
0
|
2362 |
+
0
|
2363 |
+
0
|
2364 |
+
|
2365 |
+
|
2366 |
+
Origin:
|
2367 |
+
Problem:
|
2368 |
+
Simple question here:
|
2369 |
+
|
2370 |
+
I'm trying to get an array that alternates values (1, -1, 1, -1.....) for a given length. np.repeat just gives me (1, 1, 1, 1,-1, -1,-1, -1). Thoughts?
|
2371 |
+
|
2372 |
+
A:
|
2373 |
+
<code>
|
2374 |
+
import numpy as np
|
2375 |
+
def f(n):
|
2376 |
+
### BEGIN SOLUTION
|
2377 |
+
[insert]
|
2378 |
+
### END SOLUTION
|
2379 |
+
return result
|
2380 |
+
</code>
|
2381 |
+
|
2382 |
+
test:
|
2383 |
+
a = np.array([1, -1, 1, -1, 1, -1, 1, -1])
|
2384 |
+
b = np.array([1, -1, 1, -1, 1, -1, 1, -1, 1])
|
2385 |
+
|
2386 |
+
try:
|
2387 |
+
np.testing.assert_array_equal(a, f(8))
|
2388 |
+
np.testing.assert_array_equal(b, f(9))
|
2389 |
+
print('Test passed!')
|
2390 |
+
except:
|
2391 |
+
print('Test failed...')
|
2392 |
+
|
2393 |
+
|
2394 |
+
|
2395 |
+
27.
|
2396 |
+
Score:
|
2397 |
+
|
2398 |
+
|
2399 |
+
|
2400 |
+
1
|
2401 |
+
2
|
2402 |
+
3
|
2403 |
+
4
|
2404 |
+
5
|
2405 |
+
6
|
2406 |
+
7
|
2407 |
+
8
|
2408 |
+
9
|
2409 |
+
10
|
2410 |
+
Top-10
|
2411 |
+
Avg
|
2412 |
+
Origin
|
2413 |
+
0
|
2414 |
+
0
|
2415 |
+
0
|
2416 |
+
0
|
2417 |
+
0
|
2418 |
+
0
|
2419 |
+
0
|
2420 |
+
0
|
2421 |
+
0
|
2422 |
+
0
|
2423 |
+
0
|
2424 |
+
0
|
2425 |
+
|
2426 |
+
|
2427 |
+
Origin:
|
2428 |
+
Problem:
|
2429 |
+
Simple question here:
|
2430 |
+
|
2431 |
+
I am trying to break a numpy array into chunks with a fixed size and pad the last one with 0. For example: [1,2,3,4,5,6,7] into chunks of 3 returns [[1,2,3],[4,5,6],[7,0,0]].
|
2432 |
+
|
2433 |
+
A:
|
2434 |
+
<code>
|
2435 |
+
import numpy as np
|
2436 |
+
l = np.array([1,2,3,4,5,6,7])
|
2437 |
+
ans = l.copy()
|
2438 |
+
### BEGIN SOLUTION
|
2439 |
+
[insert]
|
2440 |
+
### END SOLUTION
|
2441 |
+
print(ans)
|
2442 |
+
</code>
|
2443 |
+
|
2444 |
+
Test:
|
2445 |
+
t = l.copy()
|
2446 |
+
t.resize((3,3), refcheck=False)
|
2447 |
+
|
2448 |
+
try:
|
2449 |
+
np.testing.assert_array_equal(ans, t)
|
2450 |
+
print('Test passed!')
|
2451 |
+
except:
|
2452 |
+
print('Test failed...')
|
2453 |
+
|
2454 |
+
|
2455 |
+
28.
|
2456 |
+
Score:
|
2457 |
+
|
2458 |
+
|
2459 |
+
|
2460 |
+
1
|
2461 |
+
2
|
2462 |
+
3
|
2463 |
+
4
|
2464 |
+
5
|
2465 |
+
6
|
2466 |
+
7
|
2467 |
+
8
|
2468 |
+
9
|
2469 |
+
10
|
2470 |
+
Top-10
|
2471 |
+
Avg
|
2472 |
+
Origin
|
2473 |
+
0
|
2474 |
+
0
|
2475 |
+
0
|
2476 |
+
1
|
2477 |
+
0
|
2478 |
+
0
|
2479 |
+
1
|
2480 |
+
0
|
2481 |
+
0
|
2482 |
+
0
|
2483 |
+
1
|
2484 |
+
0.2
|
2485 |
+
|
2486 |
+
|
2487 |
+
Origin:
|
2488 |
+
Problem:
|
2489 |
+
Suppose I have the following array:
|
2490 |
+
a = np.array([1,0,2,3,0,4,5,0])
|
2491 |
+
|
2492 |
+
for each zero I would like to duplicate a zero and add it to the array such that I get:
|
2493 |
+
np.array([1,0,0,2,3,0,0,4,5,0,0])
|
2494 |
+
|
2495 |
+
A:
|
2496 |
+
<code>
|
2497 |
+
import numpy as np
|
2498 |
+
a = np.array([1, 0, 2, 3, 0, 4, 5, 0])
|
2499 |
+
### BEGIN SOLUTION
|
2500 |
+
[insert]
|
2501 |
+
### END SOLUTION
|
2502 |
+
print(a)
|
2503 |
+
</code>
|
2504 |
+
|
2505 |
+
test:
|
2506 |
+
b = np.array([1, 0, 2, 3, 0, 4, 5, 0])
|
2507 |
+
i = 0
|
2508 |
+
while i < len(b):
|
2509 |
+
if b[i] == 0:
|
2510 |
+
b = np.insert(b, i, 0)
|
2511 |
+
i += 1
|
2512 |
+
i += 1
|
2513 |
+
|
2514 |
+
try:
|
2515 |
+
np.testing.assert_array_equal(a, b)
|
2516 |
+
print('Test passed!')
|
2517 |
+
except:
|
2518 |
+
print('Test failed...')
|
2519 |
+
|
2520 |
+
|
2521 |
+
29.
|
2522 |
+
Score:
|
2523 |
+
|
2524 |
+
|
2525 |
+
|
2526 |
+
1
|
2527 |
+
2
|
2528 |
+
3
|
2529 |
+
4
|
2530 |
+
5
|
2531 |
+
6
|
2532 |
+
7
|
2533 |
+
8
|
2534 |
+
9
|
2535 |
+
10
|
2536 |
+
Top-10
|
2537 |
+
Avg
|
2538 |
+
Origin
|
2539 |
+
0
|
2540 |
+
1
|
2541 |
+
0
|
2542 |
+
0
|
2543 |
+
0
|
2544 |
+
0
|
2545 |
+
0
|
2546 |
+
0
|
2547 |
+
0
|
2548 |
+
0
|
2549 |
+
1
|
2550 |
+
0.1
|
2551 |
+
|
2552 |
+
|
2553 |
+
Origin:
|
2554 |
+
Problem:
|
2555 |
+
Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array
|
2556 |
+
R = [[4,3,2,1], [5,4,3,2], [6,5,4,3], ..., [14,13,12,11]]
|
2557 |
+
|
2558 |
+
A:
|
2559 |
+
<code>
|
2560 |
+
import numpy as np
|
2561 |
+
Z = np.arange(1, 15, dtype=np.uint32)
|
2562 |
+
### BEGIN SOLUTION
|
2563 |
+
[insert]
|
2564 |
+
### END SOLUTION
|
2565 |
+
print(R)
|
2566 |
+
</code>
|
2567 |
+
|
2568 |
+
test:
|
2569 |
+
A = np.arange(11, dtype=np.uint32).reshape(-1, 1) + np.broadcast_to(Z[3::-1], (11, 4))
|
2570 |
+
|
2571 |
+
try:
|
2572 |
+
np.testing.assert_array_equal(A, R)
|
2573 |
+
print('Test passed!')
|
2574 |
+
except:
|
2575 |
+
print('Test failed...')
|
2576 |
+
|
2577 |
+
|
2578 |
+
|
2579 |
+
30.
|
2580 |
+
Score:
|
2581 |
+
|
2582 |
+
|
2583 |
+
|
2584 |
+
1
|
2585 |
+
2
|
2586 |
+
3
|
2587 |
+
4
|
2588 |
+
5
|
2589 |
+
6
|
2590 |
+
7
|
2591 |
+
8
|
2592 |
+
9
|
2593 |
+
10
|
2594 |
+
Top-10
|
2595 |
+
Avg
|
2596 |
+
Origin
|
2597 |
+
0
|
2598 |
+
0
|
2599 |
+
0
|
2600 |
+
0
|
2601 |
+
0
|
2602 |
+
0
|
2603 |
+
0
|
2604 |
+
0
|
2605 |
+
0
|
2606 |
+
0
|
2607 |
+
0
|
2608 |
+
0
|
2609 |
+
|
2610 |
+
|
2611 |
+
Origin:
|
2612 |
+
Problem:
|
2613 |
+
Converts a 1-dimensional array to a binary representation matrix. For every row in the matrix, the i-th element is 0 or 1, representing 2^i. The order is from left to right.
|
2614 |
+
|
2615 |
+
example:
|
2616 |
+
given:
|
2617 |
+
[1,2,3,4]
|
2618 |
+
desired:
|
2619 |
+
[[1,0,0],
|
2620 |
+
[0,1,0],
|
2621 |
+
[1,1,0],
|
2622 |
+
[0,0,1]]
|
2623 |
+
|
2624 |
+
A:
|
2625 |
+
<code>
|
2626 |
+
import numpy as np
|
2627 |
+
A = np.array([1,2,3,4])
|
2628 |
+
A = A.reshape((-1,1))
|
2629 |
+
### BEGIN SOLUTION
|
2630 |
+
[insert]
|
2631 |
+
### END SOLUTION
|
2632 |
+
print(ans)
|
2633 |
+
</code>
|
2634 |
+
|
2635 |
+
Test:
|
2636 |
+
B = 2**np.arange(3)
|
2637 |
+
M = A & B
|
2638 |
+
M[M != 0] = 1
|
2639 |
+
|
2640 |
+
try:
|
2641 |
+
np.testing.assert_array_equal(ans, M)
|
2642 |
+
print('Test passed!')
|
2643 |
+
except:
|
2644 |
+
print('Test failed...')
|
2645 |
+
Numpy-100
|
2646 |
+
15.
|
2647 |
+
Score:
|
2648 |
+
|
2649 |
+
|
2650 |
+
|
2651 |
+
1
|
2652 |
+
2
|
2653 |
+
3
|
2654 |
+
4
|
2655 |
+
5
|
2656 |
+
6
|
2657 |
+
7
|
2658 |
+
8
|
2659 |
+
9
|
2660 |
+
10
|
2661 |
+
Top-10
|
2662 |
+
Avg
|
2663 |
+
Origin
|
2664 |
+
0
|
2665 |
+
1
|
2666 |
+
1
|
2667 |
+
1
|
2668 |
+
1
|
2669 |
+
1
|
2670 |
+
1
|
2671 |
+
1
|
2672 |
+
1
|
2673 |
+
0
|
2674 |
+
1
|
2675 |
+
0.8
|
2676 |
+
A3
|
2677 |
+
0
|
2678 |
+
0
|
2679 |
+
0
|
2680 |
+
1
|
2681 |
+
1
|
2682 |
+
1
|
2683 |
+
1
|
2684 |
+
1
|
2685 |
+
1
|
2686 |
+
1
|
2687 |
+
1
|
2688 |
+
0.7
|
2689 |
+
|
2690 |
+
|
2691 |
+
Origin:
|
2692 |
+
Problem:
|
2693 |
+
Create a 2d array with 1 on the border and 0 inside.
|
2694 |
+
|
2695 |
+
A:
|
2696 |
+
<code>
|
2697 |
+
import numpy as np
|
2698 |
+
### BEGIN SOLUTION
|
2699 |
+
[insert]
|
2700 |
+
### END SOLUTION
|
2701 |
+
print(Z)
|
2702 |
+
</code>
|
2703 |
+
|
2704 |
+
Test:
|
2705 |
+
ans = np.ones((10,10))
|
2706 |
+
ans[1:-1,1:-1] = 0
|
2707 |
+
|
2708 |
+
try:
|
2709 |
+
np.testing.assert_array_equal(ans, Z)
|
2710 |
+
print('Test passed!')
|
2711 |
+
except:
|
2712 |
+
print('Test failed...')
|
2713 |
+
|
2714 |
+
|
2715 |
+
A3:
|
2716 |
+
Problem:
|
2717 |
+
Create a 10*5 array with 2 on the border and 3 inside.
|
2718 |
+
|
2719 |
+
A:
|
2720 |
+
<code>
|
2721 |
+
import numpy as np
|
2722 |
+
### BEGIN SOLUTION
|
2723 |
+
[insert]
|
2724 |
+
### END SOLUTION
|
2725 |
+
print(Z)
|
2726 |
+
</code>
|
2727 |
+
|
2728 |
+
test:
|
2729 |
+
ans = 2* np.ones((10,5))
|
2730 |
+
ans[1:-1,1:-1] = 3
|
2731 |
+
|
2732 |
+
try:
|
2733 |
+
np.testing.assert_array_equal(ans, Z)
|
2734 |
+
print('Test passed!')
|
2735 |
+
except:
|
2736 |
+
print('Test failed...')
|
2737 |
+
|
2738 |
+
|
2739 |
+
|
2740 |
+
|
2741 |
+
18.
|
2742 |
+
Score:
|
2743 |
+
|
2744 |
+
|
2745 |
+
|
2746 |
+
1
|
2747 |
+
2
|
2748 |
+
3
|
2749 |
+
4
|
2750 |
+
5
|
2751 |
+
6
|
2752 |
+
7
|
2753 |
+
8
|
2754 |
+
9
|
2755 |
+
10
|
2756 |
+
Top-10
|
2757 |
+
Avg
|
2758 |
+
Origin
|
2759 |
+
1
|
2760 |
+
1
|
2761 |
+
0
|
2762 |
+
1
|
2763 |
+
1
|
2764 |
+
0
|
2765 |
+
1
|
2766 |
+
1
|
2767 |
+
1
|
2768 |
+
1
|
2769 |
+
1
|
2770 |
+
0.8
|
2771 |
+
A1
|
2772 |
+
0
|
2773 |
+
0
|
2774 |
+
0
|
2775 |
+
0
|
2776 |
+
0
|
2777 |
+
0
|
2778 |
+
0
|
2779 |
+
0
|
2780 |
+
0
|
2781 |
+
0
|
2782 |
+
0
|
2783 |
+
0
|
2784 |
+
A3
|
2785 |
+
1
|
2786 |
+
1
|
2787 |
+
1
|
2788 |
+
0
|
2789 |
+
1
|
2790 |
+
1
|
2791 |
+
1
|
2792 |
+
1
|
2793 |
+
0
|
2794 |
+
0
|
2795 |
+
1
|
2796 |
+
0.7
|
2797 |
+
|
2798 |
+
|
2799 |
+
Origin:
|
2800 |
+
Problem:
|
2801 |
+
Create a 5x5 matrix with values 1,2,3,4 just below the diagonal.
|
2802 |
+
|
2803 |
+
A:
|
2804 |
+
<code>
|
2805 |
+
import numpy as np
|
2806 |
+
### BEGIN SOLUTION
|
2807 |
+
[insert]
|
2808 |
+
### END SOLUTION
|
2809 |
+
print(Z)
|
2810 |
+
</code>
|
2811 |
+
|
2812 |
+
test:
|
2813 |
+
ans = np.diag(1+np.arange(4), k=-1)
|
2814 |
+
|
2815 |
+
try:
|
2816 |
+
np.testing.assert_array_equal(ans, Z)
|
2817 |
+
print('Test passed!')
|
2818 |
+
except:
|
2819 |
+
print('Test failed...')
|
2820 |
+
|
2821 |
+
A1:
|
2822 |
+
Problem:
|
2823 |
+
Create a 5x5 matrix with values 1,3,4,5 just below the diagonal.
|
2824 |
+
|
2825 |
+
A:
|
2826 |
+
<code>
|
2827 |
+
import numpy as np
|
2828 |
+
### BEGIN SOLUTION
|
2829 |
+
[insert]
|
2830 |
+
### END SOLUTION
|
2831 |
+
print(Z)
|
2832 |
+
</code>
|
2833 |
+
|
2834 |
+
test:
|
2835 |
+
ans = np.diag(2+np.arange(4), k=-1)
|
2836 |
+
ans[1][0] = 1
|
2837 |
+
|
2838 |
+
try:
|
2839 |
+
np.testing.assert_array_equal(ans, Z)
|
2840 |
+
print('Test passed!')
|
2841 |
+
except:
|
2842 |
+
print('Test failed...')
|
2843 |
+
|
2844 |
+
A3:
|
2845 |
+
Problem:
|
2846 |
+
Create a 5x5 matrix with values 1,2,3,4 just above the diagonal.
|
2847 |
+
|
2848 |
+
A:
|
2849 |
+
<code>
|
2850 |
+
import numpy as np
|
2851 |
+
### BEGIN SOLUTION
|
2852 |
+
[insert]
|
2853 |
+
### END SOLUTION
|
2854 |
+
print(Z)
|
2855 |
+
</code>
|
2856 |
+
|
2857 |
+
test:
|
2858 |
+
ans = np.diag(1+np.arange(4), k=1)
|
2859 |
+
|
2860 |
+
try:
|
2861 |
+
np.testing.assert_array_equal(ans, Z)
|
2862 |
+
print('Test passed!')
|
2863 |
+
except:
|
2864 |
+
print('Test failed...')
|
2865 |
+
|
2866 |
+
|
2867 |
+
|
2868 |
+
|
2869 |
+
|
2870 |
+
|
2871 |
+
|
2872 |
+
20.
|
2873 |
+
Score:
|
2874 |
+
|
2875 |
+
|
2876 |
+
|
2877 |
+
1
|
2878 |
+
2
|
2879 |
+
3
|
2880 |
+
4
|
2881 |
+
5
|
2882 |
+
6
|
2883 |
+
7
|
2884 |
+
8
|
2885 |
+
9
|
2886 |
+
10
|
2887 |
+
Top-10
|
2888 |
+
Avg
|
2889 |
+
Origin
|
2890 |
+
0
|
2891 |
+
1
|
2892 |
+
1
|
2893 |
+
0
|
2894 |
+
1
|
2895 |
+
0
|
2896 |
+
0
|
2897 |
+
0
|
2898 |
+
1
|
2899 |
+
0
|
2900 |
+
1
|
2901 |
+
0.4
|
2902 |
+
A1
|
2903 |
+
0
|
2904 |
+
0
|
2905 |
+
0
|
2906 |
+
0
|
2907 |
+
0
|
2908 |
+
0
|
2909 |
+
0
|
2910 |
+
0
|
2911 |
+
0
|
2912 |
+
0
|
2913 |
+
0
|
2914 |
+
0
|
2915 |
+
A6
|
2916 |
+
0
|
2917 |
+
0
|
2918 |
+
0
|
2919 |
+
1
|
2920 |
+
0
|
2921 |
+
0
|
2922 |
+
0
|
2923 |
+
0
|
2924 |
+
0
|
2925 |
+
0
|
2926 |
+
1
|
2927 |
+
0.1
|
2928 |
+
|
2929 |
+
|
2930 |
+
Origin:
|
2931 |
+
Problem:
|
2932 |
+
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
|
2933 |
+
|
2934 |
+
A:
|
2935 |
+
<code>
|
2936 |
+
import numpy as np
|
2937 |
+
### BEGIN SOLUTION
|
2938 |
+
[insert]
|
2939 |
+
### END SOLUTION
|
2940 |
+
print(index)
|
2941 |
+
</code>
|
2942 |
+
|
2943 |
+
Test:
|
2944 |
+
try:
|
2945 |
+
np.testing.assert_array_equal(index, np.unravel_index(99, (6,7,8)))
|
2946 |
+
print('Test passed!')
|
2947 |
+
except:
|
2948 |
+
print('Test failed...')
|
2949 |
+
|
2950 |
+
A1:
|
2951 |
+
Problem:
|
2952 |
+
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 99th element?
|
2953 |
+
|
2954 |
+
A:
|
2955 |
+
<code>
|
2956 |
+
import numpy as np
|
2957 |
+
### BEGIN SOLUTION
|
2958 |
+
[insert]
|
2959 |
+
### END SOLUTION
|
2960 |
+
print(index)
|
2961 |
+
</code>
|
2962 |
+
|
2963 |
+
Test:
|
2964 |
+
try:
|
2965 |
+
np.testing.assert_array_equal(index, np.unravel_index(98, (6,7,8)))
|
2966 |
+
print('Test passed!')
|
2967 |
+
except:
|
2968 |
+
print('Test failed...')
|
2969 |
+
|
2970 |
+
|
2971 |
+
A6:
|
2972 |
+
Problem:
|
2973 |
+
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element from back to front?
|
2974 |
+
|
2975 |
+
A:
|
2976 |
+
<code>
|
2977 |
+
import numpy as np
|
2978 |
+
### BEGIN SOLUTION
|
2979 |
+
[insert]
|
2980 |
+
### END SOLUTION
|
2981 |
+
print(index)
|
2982 |
+
</code>
|
2983 |
+
|
2984 |
+
Test:
|
2985 |
+
try:
|
2986 |
+
np.testing.assert_array_equal(index, np.unravel_index(6*7*8-100, (6,7,8)))
|
2987 |
+
print('Test passed!')
|
2988 |
+
except:
|
2989 |
+
print('Test failed...')
|
2990 |
+
|
2991 |
+
|
2992 |
+
|
2993 |
+
25.
|
2994 |
+
Score:
|
2995 |
+
|
2996 |
+
|
2997 |
+
|
2998 |
+
1
|
2999 |
+
2
|
3000 |
+
3
|
3001 |
+
4
|
3002 |
+
5
|
3003 |
+
6
|
3004 |
+
7
|
3005 |
+
8
|
3006 |
+
9
|
3007 |
+
10
|
3008 |
+
Top-10
|
3009 |
+
Avg
|
3010 |
+
Origin
|
3011 |
+
0
|
3012 |
+
0
|
3013 |
+
0
|
3014 |
+
0
|
3015 |
+
0
|
3016 |
+
0
|
3017 |
+
0
|
3018 |
+
0
|
3019 |
+
0
|
3020 |
+
0
|
3021 |
+
0
|
3022 |
+
0
|
3023 |
+
|
3024 |
+
|
3025 |
+
Origin:
|
3026 |
+
Problem:
|
3027 |
+
Given a 1D array, negate all elements which are between 3 and 8, or (3, 8), in place.
|
3028 |
+
|
3029 |
+
A:
|
3030 |
+
<code>
|
3031 |
+
import numpy as np
|
3032 |
+
Z = np.arange(11)
|
3033 |
+
### BEGIN SOLUTION
|
3034 |
+
[insert]
|
3035 |
+
### END SOLUTION
|
3036 |
+
print(Z)
|
3037 |
+
</code>
|
3038 |
+
|
3039 |
+
Test:
|
3040 |
+
test_Z = np.arange(11)
|
3041 |
+
test_Z[(3<test_Z) & (test_Z <8)] *= -1
|
3042 |
+
try:
|
3043 |
+
np.testing.assert_array_equal(Z, test_Z)
|
3044 |
+
print('Test passed!')
|
3045 |
+
except:
|
3046 |
+
print('Test failed...')
|
3047 |
+
|
3048 |
+
|
3049 |
+
|
3050 |
+
37.
|
3051 |
+
Score:
|
3052 |
+
|
3053 |
+
|
3054 |
+
|
3055 |
+
1
|
3056 |
+
2
|
3057 |
+
3
|
3058 |
+
4
|
3059 |
+
5
|
3060 |
+
6
|
3061 |
+
7
|
3062 |
+
8
|
3063 |
+
9
|
3064 |
+
10
|
3065 |
+
Top-10
|
3066 |
+
Avg
|
3067 |
+
Origin
|
3068 |
+
1
|
3069 |
+
1
|
3070 |
+
1
|
3071 |
+
1
|
3072 |
+
1
|
3073 |
+
1
|
3074 |
+
1
|
3075 |
+
1
|
3076 |
+
1
|
3077 |
+
1
|
3078 |
+
1
|
3079 |
+
1
|
3080 |
+
A1
|
3081 |
+
0
|
3082 |
+
0
|
3083 |
+
0
|
3084 |
+
0
|
3085 |
+
0
|
3086 |
+
0
|
3087 |
+
0
|
3088 |
+
0
|
3089 |
+
0
|
3090 |
+
0
|
3091 |
+
0
|
3092 |
+
0
|
3093 |
+
|
3094 |
+
|
3095 |
+
Origin:
|
3096 |
+
Problem:
|
3097 |
+
Create a 5x5 matrix with row values ranging from 0 to 4.
|
3098 |
+
|
3099 |
+
A:
|
3100 |
+
<code>
|
3101 |
+
import numpy as np
|
3102 |
+
### BEGIN SOLUTION
|
3103 |
+
[insert]
|
3104 |
+
### END SOLUTION
|
3105 |
+
print(Z)
|
3106 |
+
</code>
|
3107 |
+
|
3108 |
+
Test:
|
3109 |
+
test_Z = np.zeros((5, 5))
|
3110 |
+
test_Z += np.arange(5)
|
3111 |
+
|
3112 |
+
try:
|
3113 |
+
np.testing.assert_array_equal(Z, test_Z)
|
3114 |
+
print('Test passed!')
|
3115 |
+
except:
|
3116 |
+
print('Test failed...')
|
3117 |
+
|
3118 |
+
A1:
|
3119 |
+
Problem:
|
3120 |
+
Create a 5x5 matrix with row values equals 1, 3, 4, 5, 6.
|
3121 |
+
|
3122 |
+
A:
|
3123 |
+
<code>
|
3124 |
+
import numpy as np
|
3125 |
+
### BEGIN SOLUTION
|
3126 |
+
[insert]
|
3127 |
+
### END SOLUTION
|
3128 |
+
print(Z)
|
3129 |
+
</code>
|
3130 |
+
|
3131 |
+
test:
|
3132 |
+
test_Z = np.ones((5, 5))
|
3133 |
+
test_Z += np.arange(5)
|
3134 |
+
test_Z[:, 1:] += 1
|
3135 |
+
|
3136 |
+
try:
|
3137 |
+
np.testing.assert_array_equal(Z, test_Z)
|
3138 |
+
print('Test passed!')
|
3139 |
+
except:
|
3140 |
+
print('Test failed...')
|
3141 |
+
|
3142 |
+
|
3143 |
+
|
3144 |
+
|