Skip to content

Commit 19af334

Browse files
committed
changes for Q29
1 parent 493c48d commit 19af334

6 files changed

+50
-42
lines changed

100_Numpy_exercises.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@
564564
"cell_type": "markdown",
565565
"metadata": {},
566566
"source": [
567-
"#### 36. Extract the integer part of a random array using 5 different methods (★★☆)"
567+
"#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)"
568568
]
569569
},
570570
{

100_Numpy_exercises.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
113113

114114
#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
115115

116-
#### 36. Extract the integer part of a random array using 5 different methods (★★☆)
116+
#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
117117

118118
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
119119

100_Numpy_exercises_with_hints.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ np.array([np.nan]).astype(int).astype(float)
9797
```
9898
`No hints provided...`
9999
#### 29. How to round away from zero a float array ? (★☆☆)
100-
`hint: np.uniform, np.copysign, np.ceil, np.abs`
100+
`hint: np.uniform, np.copysign, np.ceil, np.abs, np.where`
101101
#### 30. How to find common values between two arrays? (★☆☆)
102102
`hint: np.intersect1d`
103103
#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)
@@ -113,8 +113,8 @@ np.sqrt(-1) == np.emath.sqrt(-1)
113113
`hint: np.arange(dtype=datetime64['D'])`
114114
#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
115115
`hint: np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=)`
116-
#### 36. Extract the integer part of a random array using 5 different methods (★★☆)
117-
`hint: %, np.floor, np.ceil, astype, np.trunc`
116+
#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
117+
`hint: %, np.floor, astype, np.trunc`
118118
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
119119
`hint: np.arange`
120120
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)

100_Numpy_exercises_with_hints_with_solutions.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
264264
print(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

272272
Z = 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)
331334
np.negative(A,out=A)
332335
np.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
338341
Z = 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())
717720
A = np.random.uniform(0,1,(5,5))
718721
B = np.random.uniform(0,1,(5,5))
719722

720-
# Slow version
723+
# Slow version
721724
np.diag(np.dot(A, B))
722725

723726
# Fast version

100_Numpy_exercises_with_solutions.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ print(Z)
7272

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

@@ -211,7 +211,7 @@ print(Z)
211211
# Author: Evgeni Burovski
212212

213213
Z = np.arange(11)
214-
Z[(3 < Z) & (Z < 8)] *= -1
214+
Z[(3 < Z) & (Z <= 8)] *= -1
215215
print(Z)
216216
```
217217
#### 26. What is the output of the following script? (★☆☆)
@@ -270,7 +270,10 @@ print(np.array([np.nan]).astype(int).astype(float))
270270
# Author: Charles R Harris
271271

272272
Z = 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

@@ -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

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

@@ -331,17 +334,17 @@ np.divide(A,2,out=A)
331334
np.negative(A,out=A)
332335
np.multiply(A,B,out=A)
333336
```
334-
#### 36. Extract the integer part of a random array using 5 different methods (★★☆)
337+
#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
335338

336339

337340
```python
338341
Z = 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

@@ -717,7 +720,7 @@ print(pd.Series(D).groupby(S).mean())
717720
A = np.random.uniform(0,1,(5,5))
718721
B = np.random.uniform(0,1,(5,5))
719722

720-
# Slow version
723+
# Slow version
721724
np.diag(np.dot(A, B))
722725

723726
# Fast version

source/exercises100.ktx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,16 @@ print(np.array([np.nan]).astype(int).astype(float))
337337
How to round away from zero a float array ? (★☆☆)
338338

339339
< h29
340-
hint: np.uniform, np.copysign, np.ceil, np.abs
340+
hint: np.uniform, np.copysign, np.ceil, np.abs, np.where
341341

342342
< a29
343343
# Author: Charles R Harris
344344

345345
Z = np.random.uniform(-10,+10,10)
346-
print (np.copysign(np.ceil(np.abs(Z)), Z))
346+
print(np.copysign(np.ceil(np.abs(Z)), Z))
347+
348+
# More readable but less efficient
349+
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
347350

348351
< q30
349352
How to find common values between two arrays? (★☆☆)
@@ -910,7 +913,7 @@ hint: np.diag
910913
A = np.random.uniform(0,1,(5,5))
911914
B = np.random.uniform(0,1,(5,5))
912915

913-
# Slow version
916+
# Slow version
914917
np.diag(np.dot(A, B))
915918

916919
# Fast version
@@ -1457,4 +1460,3 @@ idx = np.random.randint(0, X.size, (N, X.size))
14571460
means = X[idx].mean(axis=1)
14581461
confint = np.percentile(means, [2.5, 97.5])
14591462
print(confint)
1460-

0 commit comments

Comments
 (0)