@@ -72,8 +72,8 @@ print(Z)
7272` hint: reshape `
7373
7474``` python
75- nz = np.nonzero([ 1 , 2 , 0 , 0 , 4 , 0 ] )
76- print (nz )
75+ Z = np.arange( 9 ).reshape( 3 , 3 )
76+ print (Z )
7777```
7878#### 10. Find indices of non-zero elements from [ 1,2,0,0,4,0] (★☆☆)
7979` hint: np.nonzero `
@@ -264,13 +264,16 @@ print(np.array(0) // np.array(0))
264264print (np.array([np.nan]).astype(int ).astype(float ))
265265```
266266#### 29. How to round away from zero a float array ? (★☆☆)
267- ` hint: np.uniform, np.copysign, np.ceil, np.abs `
267+ ` hint: np.uniform, np.copysign, np.ceil, np.abs, np.where `
268268
269269``` python
270270# Author: Charles R Harris
271271
272272Z = np.random.uniform(- 10 ,+ 10 ,10 )
273- print (np.copysign(np.ceil(np.abs(Z)), Z))
273+ print (np.copysign(np.ceil(np.abs(Z)), Z))
274+
275+ # More readable but less efficient
276+ print (np.where(Z> 0 , np.ceil(Z), np.floor(Z)))
274277```
275278#### 30. How to find common values between two arrays? (★☆☆)
276279` hint: np.intersect1d `
@@ -292,8 +295,8 @@ Z = np.ones(1) / 0
292295_ = np.seterr(** defaults)
293296
294297# Equivalently with a context manager
295- nz = np.nonzero([ 1 , 2 , 0 , 0 , 4 , 0 ])
296- print (nz)
298+ with np.errstate( all = " ignore " ):
299+ np.arange( 3 ) / 0
297300```
298301#### 32. Is the following expressions true? (★☆☆)
299302``` python
@@ -308,9 +311,9 @@ np.sqrt(-1) == np.emath.sqrt(-1)
308311` hint: np.datetime64, np.timedelta64 `
309312
310313``` python
311- yesterday = np.datetime64(' today' , ' D ' ) - np.timedelta64(1 , ' D ' )
312- today = np.datetime64(' today' , ' D ' )
313- tomorrow = np.datetime64(' today' , ' D ' ) + np.timedelta64(1 , ' D ' )
314+ yesterday = np.datetime64(' today' ) - np.timedelta64(1 )
315+ today = np.datetime64(' today' )
316+ tomorrow = np.datetime64(' today' ) + np.timedelta64(1 )
314317```
315318#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)
316319` hint: np.arange(dtype=datetime64['D']) `
@@ -331,17 +334,17 @@ np.divide(A,2,out=A)
331334np.negative(A,out = A)
332335np.multiply(A,B,out = A)
333336```
334- #### 36. Extract the integer part of a random array using 5 different methods (★★☆)
335- ` hint: %, np.floor, np.ceil, astype, np.trunc `
337+ #### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
338+ ` hint: %, np.floor, astype, np.trunc `
336339
337340``` python
338341Z = np.random.uniform(0 ,10 ,10 )
339342
340- print (Z - Z% 1 )
341- print (np.floor(Z) )
342- print (np.ceil (Z)- 1 )
343- print (Z.astype(int ))
344- print (np.trunc(Z))
343+ print (Z - Z% 1 )
344+ print (Z // 1 )
345+ print (np.floor (Z))
346+ print (Z.astype(int ))
347+ print (np.trunc(Z))
345348```
346349#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
347350` hint: np.arange `
@@ -717,7 +720,7 @@ print(pd.Series(D).groupby(S).mean())
717720A = np.random.uniform(0 ,1 ,(5 ,5 ))
718721B = np.random.uniform(0 ,1 ,(5 ,5 ))
719722
720- # Slow version
723+ # Slow version
721724np.diag(np.dot(A, B))
722725
723726# Fast version
0 commit comments