@@ -46,60 +46,60 @@ Neophyte
4646 import numpy as np
4747
4848
49- # . Print the numpy version and the configuration.
49+ 2 . Print the numpy version and the configuration.
5050
5151 .. code :: python
5252
5353 print np.__version__
5454 np.__config__.show()
5555
5656
57- # . Create a null vector of size 10
57+ 3 . Create a null vector of size 10
5858
5959 .. code :: python
6060
6161 Z = np.zeros(10 )
6262
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
6464
6565 .. code :: python
6666
6767 Z = np.zeros(10 )
6868 Z[4 ] = 1
6969
70- # . Create a vector with values ranging from 10 to 99
70+ 5 . Create a vector with values ranging from 10 to 99
7171
7272 .. code :: python
7373
7474 Z = np.arange(10 ,100 )
7575
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
7777
7878 .. code :: python
7979
8080 Z = np.arange(9 ).reshape(3 ,3 )
8181
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]
8383
8484 .. code :: python
8585
8686 nz = np.nonzero([1 ,2 ,0 ,0 ,4 ,0 ])
8787
8888
89- # . Declare a 3x3 identity matrix
89+ 8 . Declare a 3x3 identity matrix
9090
9191 .. code :: python
9292
9393 Z = np.eye(3 )
9494
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
9696
9797 .. code :: python
9898
9999 Z = np.diag(1 + np.arange(4 ),k = - 1 )
100100
101101
102- # . Declare a 10x10x10 array with random values
102+ 10 . Declare a 10x10x10 array with random values
103103
104104 .. code :: python
105105
@@ -108,28 +108,28 @@ Neophyte
108108 Novice
109109======
110110
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
112112
113113 .. code :: python
114114
115115 Z = np.zeros((8 ,8 ))
116116 Z[1 ::2 ,::2 ] = 1
117117 Z[::2 ,1 ::2 ] = 1
118118
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
120120
121121 .. code :: python
122122
123123 Z = np.random.random((10 ,10 ))
124124 Zmin, Zmax = Z.min(), Z.max()
125125
126- # . Create a checkerboard 8x8 matrix using the tile function
126+ 3 . Create a checkerboard 8x8 matrix using the tile function
127127
128128 .. code :: python
129129
130130 Z = np.tile( np.array([[0 ,1 ],[1 ,0 ]]), (4 ,4 ))
131131
132- # . Normalize a 5x5 random matrix (between 0 and 1)
132+ 4 . Normalize a 5x5 random matrix (between 0 and 1)
133133
134134 .. code :: python
135135
@@ -138,42 +138,42 @@ Novice
138138 Z = (Z - Zmin)/ (Zmax - Zmin)
139139
140140
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)
142142
143143 .. code :: python
144144
145145 Z = np.dot(np.ones((5 ,3 )), np.ones((3 ,2 )))
146146
147147
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
149149
150150 .. code :: python
151151
152152 Z = np.zeros((10 ,10 ))
153153 Z += np.arange(10 )
154154
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
156156
157157 .. code :: python
158158
159159 Z = np.random.linspace(0 ,1 ,1002 ,endpoint = True )[1 :- 1 ]
160160
161- # . Create a random vector of size 100 and sort it
161+ 8 . Create a random vector of size 100 and sort it
162162
163163 .. code :: python
164164
165165 Z = np.random.random(100 )
166166 Z.sort()
167167
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.
169169
170170 .. code :: python
171171
172172 A = np.random.randint(0 ,2 ,(2 ,2 ))
173173 B = np.random.randint(0 ,2 ,(2 ,2 ))
174174 equal = np.allclose(A,B)
175175
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
177177
178178 .. code :: python
179179
@@ -186,15 +186,15 @@ Apprentice
186186==========
187187
188188
189- # . Make an array immutable
189+ 1 . Make an array immutable
190190
191191 .. code :: python
192192
193193 Z = np.zeros(10 )
194194 Z.flags.writeable = False
195195
196196
197- # . Consider a random 100x2 matrix representing cartesian coordinates, convert
197+ 2 . Consider a random 100x2 matrix representing cartesian coordinates, convert
198198 them to polar coordinates
199199
200200 .. code :: python
@@ -205,15 +205,15 @@ Apprentice
205205 T = np.arctan2(Y,X)
206206
207207
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
209209
210210 .. code :: python
211211
212212 Z = np.random.random(100 )
213213 Z[Z.argmax()] = 0
214214
215215
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
217217 [0,1]x[0,1] area.
218218
219219 .. code :: python
@@ -222,7 +222,7 @@ Apprentice
222222 Z[' x' ], Z[' y' ] = np.meshgrid(np.linspace(0 ,1 ,10 ),
223223 np.linspace(0 ,1 ,10 ))
224224
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
226226
227227 .. code :: python
228228
@@ -235,7 +235,7 @@ Apprentice
235235 print np.finfo(dtype).eps
236236
237237
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)
239239
240240 .. code :: python
241241
@@ -246,7 +246,7 @@ Apprentice
246246 (' b' , float , 1 )])])
247247
248248
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
250250 point by point distances
251251
252252 .. code :: python
@@ -261,7 +261,7 @@ Apprentice
261261
262262
263263
264- # . Generate a generic 2D Gaussian-like array
264+ 8 . Generate a generic 2D Gaussian-like array
265265
266266 .. code :: python
267267
@@ -270,7 +270,7 @@ Apprentice
270270 sigma, mu = 1.0 , 0.0
271271 G = np.exp(- ( (D- mu)** 2 / ( 2.0 * sigma** 2 ) ) )
272272
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
274274 consecutive zeros interleaved between each value ?
275275
276276 .. code :: python
@@ -283,7 +283,7 @@ Apprentice
283283 Z0[::nz+ 1 ] = Z
284284
285285
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
287287
288288 .. code :: python
289289
@@ -294,7 +294,7 @@ Apprentice
294294 Journeyman
295295==========
296296
297- # . Consider the following file::
297+ 1 . Consider the following file::
298298
299299 1,2,3,4,5
300300 6,,,7,8
@@ -307,7 +307,7 @@ Journeyman
307307 Z = genfromtxt("missing.dat", delimiter=",")
308308
309309
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
311311 array
312312
313313 .. code :: python
@@ -318,7 +318,7 @@ Journeyman
318318 Z = np.fromiter(generate(),dtype = float ,count = - 1 )
319319
320320
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
322322 vector (be careful with repeated indices) ?
323323
324324 .. code :: python
@@ -330,7 +330,7 @@ Journeyman
330330 Z += np.bincount(I, minlength = len (Z))
331331
332332
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
334334 list (I) ?
335335
336336 .. code :: python
@@ -341,7 +341,7 @@ Journeyman
341341 I = [1 ,3 ,9 ,3 ,4 ,1 ]
342342 F = np.bincount(I,X)
343343
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
345345 colors
346346
347347 .. code :: python
@@ -355,7 +355,7 @@ Journeyman
355355
356356 np.unique(I)
357357
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 ?
359359
360360
361361 .. code :: python
@@ -365,10 +365,11 @@ Journeyman
365365
366366
367367
368+
368369 Craftsman
369370=========
370371
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
372373 first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last
373374 row should be (Z[-3],Z[-2],Z[-1])
374375
@@ -383,7 +384,7 @@ Craftsman
383384
384385 Z = rolling(np.arange(100 ), 3 )
385386
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
387388 vertices), find the set of unique line segments composing all the triangles.
388389
389390 .. code :: python
@@ -399,11 +400,21 @@ Craftsman
399400 G = np.unique(G)
400401
401402
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+
402413
403414 Artisan
404415=======
405416
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])
407418
408419 .. code :: python
409420
@@ -413,7 +424,7 @@ Artisan
413424 E = np.logical_and.reduce(Z[:,1 :] == Z[:,:- 1 ], axis = 1 )
414425 U = Z[~ E]
415426
416- # . Convert a vector of ints into a matrix binary representation.
427+ 2 . Convert a vector of ints into a matrix binary representation.
417428
418429 .. code :: python
419430
@@ -433,7 +444,7 @@ Artisan
433444 Adept
434445=====
435446
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
437448 fixed shape and centered on a given element (pad with a ``fill `` value when
438449 necessary)
439450
@@ -473,7 +484,7 @@ Adept
473484 Expert
474485======
475486
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
477488 that contain elements of each row of B regardless of the order of the elements
478489 in B ?
479490
@@ -488,7 +499,7 @@ Expert
488499 rows = (C.sum(axis = (1 ,2 ,3 )) >= B.shape[1 ]).nonzero()[0 ]
489500
490501
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.
492503
493504 .. code :: python
494505
0 commit comments