@@ -46,60 +46,60 @@ Neophyte
46
46
import numpy as np
47
47
48
48
49
- # . Print the numpy version and the configuration.
49
+ 2 . Print the numpy version and the configuration.
50
50
51
51
.. code :: python
52
52
53
53
print np.__version__
54
54
np.__config__.show()
55
55
56
56
57
- # . Create a null vector of size 10
57
+ 3 . Create a null vector of size 10
58
58
59
59
.. code :: python
60
60
61
61
Z = np.zeros(10 )
62
62
63
- # . Create a null vector of size 10 but the fifth value which is 1
63
+ 4 . Create a null vector of size 10 but the fifth value which is 1
64
64
65
65
.. code :: python
66
66
67
67
Z = np.zeros(10 )
68
68
Z[4 ] = 1
69
69
70
- # . Create a vector with values ranging from 10 to 99
70
+ 5 . Create a vector with values ranging from 10 to 99
71
71
72
72
.. code :: python
73
73
74
74
Z = np.arange(10 ,100 )
75
75
76
- # . Create a 3x3 matrix with values ranging from 0 to 8
76
+ 6 . Create a 3x3 matrix with values ranging from 0 to 8
77
77
78
78
.. code :: python
79
79
80
80
Z = np.arange(9 ).reshape(3 ,3 )
81
81
82
- # . Find indices of non-zero elements from [1,2,0,0,4,0]
82
+ 7 . Find indices of non-zero elements from [1,2,0,0,4,0]
83
83
84
84
.. code :: python
85
85
86
86
nz = np.nonzero([1 ,2 ,0 ,0 ,4 ,0 ])
87
87
88
88
89
- # . Declare a 3x3 identity matrix
89
+ 8 . Declare a 3x3 identity matrix
90
90
91
91
.. code :: python
92
92
93
93
Z = np.eye(3 )
94
94
95
- # . Declare a 5x5 matrix with values 1,2,3,4 just below the diagonal
95
+ 9 . Declare a 5x5 matrix with values 1,2,3,4 just below the diagonal
96
96
97
97
.. code :: python
98
98
99
99
Z = np.diag(1 + np.arange(4 ),k = - 1 )
100
100
101
101
102
- # . Declare a 10x10x10 array with random values
102
+ 10 . Declare a 10x10x10 array with random values
103
103
104
104
.. code :: python
105
105
@@ -108,28 +108,28 @@ Neophyte
108
108
Novice
109
109
======
110
110
111
- # . Declare a 8x8 matrix and fill it with a checkerboard pattern
111
+ 1 . Declare a 8x8 matrix and fill it with a checkerboard pattern
112
112
113
113
.. code :: python
114
114
115
115
Z = np.zeros((8 ,8 ))
116
116
Z[1 ::2 ,::2 ] = 1
117
117
Z[::2 ,1 ::2 ] = 1
118
118
119
- # . Declare a 10x10 array with random values and find the minimum and maximum values
119
+ 2 . Declare a 10x10 array with random values and find the minimum and maximum values
120
120
121
121
.. code :: python
122
122
123
123
Z = np.random.random((10 ,10 ))
124
124
Zmin, Zmax = Z.min(), Z.max()
125
125
126
- # . Create a checkerboard 8x8 matrix using the tile function
126
+ 3 . Create a checkerboard 8x8 matrix using the tile function
127
127
128
128
.. code :: python
129
129
130
130
Z = np.tile( np.array([[0 ,1 ],[1 ,0 ]]), (4 ,4 ))
131
131
132
- # . Normalize a 5x5 random matrix (between 0 and 1)
132
+ 4 . Normalize a 5x5 random matrix (between 0 and 1)
133
133
134
134
.. code :: python
135
135
@@ -138,42 +138,42 @@ Novice
138
138
Z = (Z - Zmin)/ (Zmax - Zmin)
139
139
140
140
141
- # . Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
141
+ 5 . Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
142
142
143
143
.. code :: python
144
144
145
145
Z = np.dot(np.ones((5 ,3 )), np.ones((3 ,2 )))
146
146
147
147
148
- # . Create a 10x10 matrix with row values ranging from 0 to 9
148
+ 6 . Create a 10x10 matrix with row values ranging from 0 to 9
149
149
150
150
.. code :: python
151
151
152
152
Z = np.zeros((10 ,10 ))
153
153
Z += np.arange(10 )
154
154
155
- # . Create a vector of size 1000 with values ranging from 0 to 1, both excluded
155
+ 7 . Create a vector of size 1000 with values ranging from 0 to 1, both excluded
156
156
157
157
.. code :: python
158
158
159
159
Z = np.random.linspace(0 ,1 ,1002 ,endpoint = True )[1 :- 1 ]
160
160
161
- # . Create a random vector of size 100 and sort it
161
+ 8 . Create a random vector of size 100 and sort it
162
162
163
163
.. code :: python
164
164
165
165
Z = np.random.random(100 )
166
166
Z.sort()
167
167
168
- # . Consider two random matrices A anb B, check if they are equal.
168
+ 9 . Consider two random matrices A anb B, check if they are equal.
169
169
170
170
.. code :: python
171
171
172
172
A = np.random.randint(0 ,2 ,(2 ,2 ))
173
173
B = np.random.randint(0 ,2 ,(2 ,2 ))
174
174
equal = np.allclose(A,B)
175
175
176
- # . Create a random vector of size 1000 and find the mean value
176
+ 10 . Create a random vector of size 1000 and find the mean value
177
177
178
178
.. code :: python
179
179
@@ -186,15 +186,15 @@ Apprentice
186
186
==========
187
187
188
188
189
- # . Make an array immutable
189
+ 1 . Make an array immutable
190
190
191
191
.. code :: python
192
192
193
193
Z = np.zeros(10 )
194
194
Z.flags.writeable = False
195
195
196
196
197
- # . Consider a random 100x2 matrix representing cartesian coordinates, convert
197
+ 2 . Consider a random 100x2 matrix representing cartesian coordinates, convert
198
198
them to polar coordinates
199
199
200
200
.. code :: python
@@ -205,15 +205,15 @@ Apprentice
205
205
T = np.arctan2(Y,X)
206
206
207
207
208
- # . Create random vector of size 100 and replace the maximum value by 0
208
+ 3 . Create random vector of size 100 and replace the maximum value by 0
209
209
210
210
.. code :: python
211
211
212
212
Z = np.random.random(100 )
213
213
Z[Z.argmax()] = 0
214
214
215
215
216
- # . Declare a structured array with ``x `` and ``y `` coordinates covering the
216
+ 4 . Declare a structured array with ``x `` and ``y `` coordinates covering the
217
217
[0,1]x[0,1] area.
218
218
219
219
.. code :: python
@@ -222,7 +222,7 @@ Apprentice
222
222
Z[' x' ], Z[' y' ] = np.meshgrid(np.linspace(0 ,1 ,10 ),
223
223
np.linspace(0 ,1 ,10 ))
224
224
225
- # . Print the minimum and maximum representable value for each numpy scalar type
225
+ 5 . Print the minimum and maximum representable value for each numpy scalar type
226
226
227
227
.. code :: python
228
228
@@ -235,7 +235,7 @@ Apprentice
235
235
print np.finfo(dtype).eps
236
236
237
237
238
- # . Create a structured array representing a position (x,y) and a color (r,g,b)
238
+ 6 . Create a structured array representing a position (x,y) and a color (r,g,b)
239
239
240
240
.. code :: python
241
241
@@ -246,7 +246,7 @@ Apprentice
246
246
(' b' , float , 1 )])])
247
247
248
248
249
- # . Consider a random vector with shape (100,2) representing coordinates, find
249
+ 7 . Consider a random vector with shape (100,2) representing coordinates, find
250
250
point by point distances
251
251
252
252
.. code :: python
@@ -261,7 +261,7 @@ Apprentice
261
261
262
262
263
263
264
- # . Generate a generic 2D Gaussian-like array
264
+ 8 . Generate a generic 2D Gaussian-like array
265
265
266
266
.. code :: python
267
267
@@ -270,7 +270,7 @@ Apprentice
270
270
sigma, mu = 1.0 , 0.0
271
271
G = np.exp(- ( (D- mu)** 2 / ( 2.0 * sigma** 2 ) ) )
272
272
273
- # . Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
273
+ 9 . Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
274
274
consecutive zeros interleaved between each value ?
275
275
276
276
.. code :: python
@@ -283,7 +283,7 @@ Apprentice
283
283
Z0[::nz+ 1 ] = Z
284
284
285
285
286
- # . Find the nearest value from a given value in an array
286
+ 10 . Find the nearest value from a given value in an array
287
287
288
288
.. code :: python
289
289
@@ -294,7 +294,7 @@ Apprentice
294
294
Journeyman
295
295
==========
296
296
297
- # . Consider the following file::
297
+ 1 . Consider the following file::
298
298
299
299
1,2,3,4,5
300
300
6,,,7,8
@@ -307,7 +307,7 @@ Journeyman
307
307
Z = genfromtxt("missing.dat", delimiter=",")
308
308
309
309
310
- # . Consider a generator function that generates 10 integers and use it to build an
310
+ 2 . Consider a generator function that generates 10 integers and use it to build an
311
311
array
312
312
313
313
.. code :: python
@@ -318,7 +318,7 @@ Journeyman
318
318
Z = np.fromiter(generate(),dtype = float ,count = - 1 )
319
319
320
320
321
- # . Consider a given vector, how to add 1 to each element indexed by a second
321
+ 3 . Consider a given vector, how to add 1 to each element indexed by a second
322
322
vector (be careful with repeated indices) ?
323
323
324
324
.. code :: python
@@ -330,7 +330,7 @@ Journeyman
330
330
Z += np.bincount(I, minlength = len (Z))
331
331
332
332
333
- # . How to accumulate elements of a vector (X) to an array (F) based on an index
333
+ 4 . How to accumulate elements of a vector (X) to an array (F) based on an index
334
334
list (I) ?
335
335
336
336
.. code :: python
@@ -341,7 +341,7 @@ Journeyman
341
341
I = [1 ,3 ,9 ,3 ,4 ,1 ]
342
342
F = np.bincount(I,X)
343
343
344
- # . Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique
344
+ 5 . Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique
345
345
colors
346
346
347
347
.. code :: python
@@ -355,7 +355,7 @@ Journeyman
355
355
356
356
np.unique(I)
357
357
358
- # . Considering a four dimensions array, how to get sum over the last two axis at once ?
358
+ 6 . Considering a four dimensions array, how to get sum over the last two axis at once ?
359
359
360
360
361
361
.. code :: python
@@ -365,10 +365,11 @@ Journeyman
365
365
366
366
367
367
368
+
368
369
Craftsman
369
370
=========
370
371
371
- # . Consider a one-dimensional array Z, build a two-dimensional array whose
372
+ 1 . Consider a one-dimensional array Z, build a two-dimensional array whose
372
373
first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last
373
374
row should be (Z[-3],Z[-2],Z[-1])
374
375
@@ -383,7 +384,7 @@ Craftsman
383
384
384
385
Z = rolling(np.arange(100 ), 3 )
385
386
386
- # . Consider a set of 100 triplets describing 100 triangles (with shared
387
+ 2 . Consider a set of 100 triplets describing 100 triangles (with shared
387
388
vertices), find the set of unique line segments composing all the triangles.
388
389
389
390
.. code :: python
@@ -399,11 +400,21 @@ Craftsman
399
400
G = np.unique(G)
400
401
401
402
403
+ 3. Given an array C that is a bincount, how to procude an array A such that
404
+ np.bincount(A) == C ?
405
+
406
+ .. code :: python
407
+
408
+ # Jaime Fernández del Río
409
+ C = np.bincount([1 ,1 ,2 ,3 ,4 ,4 ,6 ])
410
+ A = np.repeat(np.arange(len (C)), C)
411
+
412
+
402
413
403
414
Artisan
404
415
=======
405
416
406
- # . Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3])
417
+ 1 . Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3])
407
418
408
419
.. code :: python
409
420
@@ -413,7 +424,7 @@ Artisan
413
424
E = np.logical_and.reduce(Z[:,1 :] == Z[:,:- 1 ], axis = 1 )
414
425
U = Z[~ E]
415
426
416
- # . Convert a vector of ints into a matrix binary representation.
427
+ 2 . Convert a vector of ints into a matrix binary representation.
417
428
418
429
.. code :: python
419
430
@@ -433,7 +444,7 @@ Artisan
433
444
Adept
434
445
=====
435
446
436
- # . Consider an arbitrary array, write a function that extract a subpart with a
447
+ 1 . Consider an arbitrary array, write a function that extract a subpart with a
437
448
fixed shape and centered on a given element (pad with a ``fill `` value when
438
449
necessary)
439
450
@@ -473,7 +484,7 @@ Adept
473
484
Expert
474
485
======
475
486
476
- # . Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A
487
+ 1 . Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A
477
488
that contain elements of each row of B regardless of the order of the elements
478
489
in B ?
479
490
@@ -488,7 +499,7 @@ Expert
488
499
rows = (C.sum(axis = (1 ,2 ,3 )) >= B.shape[1 ]).nonzero()[0 ]
489
500
490
501
491
- # . Extract all the contiguous 3x3 blocks from a random 10x10 matrix.
502
+ 2 . Extract all the contiguous 3x3 blocks from a random 10x10 matrix.
492
503
493
504
.. code :: python
494
505
0 commit comments