Merge pull request #122 from acharles7/enhancement

Q29 alternate solution
This commit is contained in:
Nicolas P. Rougier
2020-06-02 11:07:32 +02:00
committed by GitHub
6 changed files with 50 additions and 42 deletions

View File

@@ -564,7 +564,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### 36. Extract the integer part of a random array using 5 different methods (★★☆)" "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)"
] ]
}, },
{ {

View File

@@ -113,7 +113,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆) #### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
#### 36. Extract the integer part of a random array using 5 different methods (★★☆) #### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) #### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)

View File

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

View File

@@ -72,8 +72,8 @@ print(Z)
`hint: reshape` `hint: reshape`
```python ```python
nz = np.nonzero([1,2,0,0,4,0]) Z = np.arange(9).reshape(3, 3)
print(nz) print(Z)
``` ```
#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆) #### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
`hint: np.nonzero` `hint: np.nonzero`
@@ -264,13 +264,16 @@ print(np.array(0) // np.array(0))
print(np.array([np.nan]).astype(int).astype(float)) print(np.array([np.nan]).astype(int).astype(float))
``` ```
#### 29. How to round away from zero a float array ? (★☆☆) #### 29. How to round away from zero a float array ? (★☆☆)
`hint: np.uniform, np.copysign, np.ceil, np.abs` `hint: np.uniform, np.copysign, np.ceil, np.abs, np.where`
```python ```python
# Author: Charles R Harris # Author: Charles R Harris
Z = np.random.uniform(-10,+10,10) Z = np.random.uniform(-10,+10,10)
print (np.copysign(np.ceil(np.abs(Z)), Z)) print(np.copysign(np.ceil(np.abs(Z)), Z))
# More readable but less efficient
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
``` ```
#### 30. How to find common values between two arrays? (★☆☆) #### 30. How to find common values between two arrays? (★☆☆)
`hint: np.intersect1d` `hint: np.intersect1d`
@@ -292,8 +295,8 @@ Z = np.ones(1) / 0
_ = np.seterr(**defaults) _ = np.seterr(**defaults)
# Equivalently with a context manager # Equivalently with a context manager
nz = np.nonzero([1,2,0,0,4,0]) with np.errstate(all="ignore"):
print(nz) np.arange(3) / 0
``` ```
#### 32. Is the following expressions true? (★☆☆) #### 32. Is the following expressions true? (★☆☆)
```python ```python
@@ -308,9 +311,9 @@ np.sqrt(-1) == np.emath.sqrt(-1)
`hint: np.datetime64, np.timedelta64` `hint: np.datetime64, np.timedelta64`
```python ```python
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D') yesterday = np.datetime64('today') - np.timedelta64(1)
today = np.datetime64('today', 'D') today = np.datetime64('today')
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D') tomorrow = np.datetime64('today') + np.timedelta64(1)
``` ```
#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆) #### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)
`hint: np.arange(dtype=datetime64['D'])` `hint: np.arange(dtype=datetime64['D'])`
@@ -331,17 +334,17 @@ np.divide(A,2,out=A)
np.negative(A,out=A) np.negative(A,out=A)
np.multiply(A,B,out=A) np.multiply(A,B,out=A)
``` ```
#### 36. Extract the integer part of a random array using 5 different methods (★★☆) #### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
`hint: %, np.floor, np.ceil, astype, np.trunc` `hint: %, np.floor, astype, np.trunc`
```python ```python
Z = np.random.uniform(0,10,10) Z = np.random.uniform(0,10,10)
print (Z - Z%1) print(Z - Z%1)
print (np.floor(Z)) print(Z // 1)
print (np.ceil(Z)-1) print(np.floor(Z))
print (Z.astype(int)) print(Z.astype(int))
print (np.trunc(Z)) print(np.trunc(Z))
``` ```
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) #### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
`hint: np.arange` `hint: np.arange`
@@ -717,7 +720,7 @@ print(pd.Series(D).groupby(S).mean())
A = np.random.uniform(0,1,(5,5)) A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5)) B = np.random.uniform(0,1,(5,5))
# Slow version # Slow version
np.diag(np.dot(A, B)) np.diag(np.dot(A, B))
# Fast version # Fast version

View File

@@ -72,8 +72,8 @@ print(Z)
```python ```python
nz = np.nonzero([1,2,0,0,4,0]) Z = np.arange(9).reshape(3, 3)
print(nz) print(Z)
``` ```
#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆) #### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
@@ -211,7 +211,7 @@ print(Z)
# Author: Evgeni Burovski # Author: Evgeni Burovski
Z = np.arange(11) Z = np.arange(11)
Z[(3 < Z) & (Z < 8)] *= -1 Z[(3 < Z) & (Z <= 8)] *= -1
print(Z) print(Z)
``` ```
#### 26. What is the output of the following script? (★☆☆) #### 26. What is the output of the following script? (★☆☆)
@@ -270,7 +270,10 @@ print(np.array([np.nan]).astype(int).astype(float))
# Author: Charles R Harris # Author: Charles R Harris
Z = np.random.uniform(-10,+10,10) Z = np.random.uniform(-10,+10,10)
print (np.copysign(np.ceil(np.abs(Z)), Z)) print(np.copysign(np.ceil(np.abs(Z)), Z))
# More readable but less efficient
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
``` ```
#### 30. How to find common values between two arrays? (★☆☆) #### 30. How to find common values between two arrays? (★☆☆)
@@ -292,8 +295,8 @@ Z = np.ones(1) / 0
_ = np.seterr(**defaults) _ = np.seterr(**defaults)
# Equivalently with a context manager # Equivalently with a context manager
nz = np.nonzero([1,2,0,0,4,0]) with np.errstate(all="ignore"):
print(nz) np.arange(3) / 0
``` ```
#### 32. Is the following expressions true? (★☆☆) #### 32. Is the following expressions true? (★☆☆)
```python ```python
@@ -308,9 +311,9 @@ np.sqrt(-1) == np.emath.sqrt(-1)
```python ```python
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D') yesterday = np.datetime64('today') - np.timedelta64(1)
today = np.datetime64('today', 'D') today = np.datetime64('today')
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D') tomorrow = np.datetime64('today') + np.timedelta64(1)
``` ```
#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆) #### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)
@@ -331,17 +334,17 @@ np.divide(A,2,out=A)
np.negative(A,out=A) np.negative(A,out=A)
np.multiply(A,B,out=A) np.multiply(A,B,out=A)
``` ```
#### 36. Extract the integer part of a random array using 5 different methods (★★☆) #### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
```python ```python
Z = np.random.uniform(0,10,10) Z = np.random.uniform(0,10,10)
print (Z - Z%1) print(Z - Z%1)
print (np.floor(Z)) print(Z // 1)
print (np.ceil(Z)-1) print(np.floor(Z))
print (Z.astype(int)) print(Z.astype(int))
print (np.trunc(Z)) print(np.trunc(Z))
``` ```
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) #### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
@@ -717,7 +720,7 @@ print(pd.Series(D).groupby(S).mean())
A = np.random.uniform(0,1,(5,5)) A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5)) B = np.random.uniform(0,1,(5,5))
# Slow version # Slow version
np.diag(np.dot(A, B)) np.diag(np.dot(A, B))
# Fast version # Fast version

View File

@@ -337,13 +337,16 @@ print(np.array([np.nan]).astype(int).astype(float))
How to round away from zero a float array ? (★☆☆) How to round away from zero a float array ? (★☆☆)
< h29 < h29
hint: np.uniform, np.copysign, np.ceil, np.abs hint: np.uniform, np.copysign, np.ceil, np.abs, np.where
< a29 < a29
# Author: Charles R Harris # Author: Charles R Harris
Z = np.random.uniform(-10,+10,10) Z = np.random.uniform(-10,+10,10)
print (np.copysign(np.ceil(np.abs(Z)), Z)) print(np.copysign(np.ceil(np.abs(Z)), Z))
# More readable but less efficient
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
< q30 < q30
How to find common values between two arrays? (★☆☆) How to find common values between two arrays? (★☆☆)
@@ -910,7 +913,7 @@ hint: np.diag
A = np.random.uniform(0,1,(5,5)) A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5)) B = np.random.uniform(0,1,(5,5))
# Slow version # Slow version
np.diag(np.dot(A, B)) np.diag(np.dot(A, B))
# Fast version # Fast version
@@ -1457,4 +1460,3 @@ idx = np.random.randint(0, X.size, (N, X.size))
means = X[idx].mean(axis=1) means = X[idx].mean(axis=1)
confint = np.percentile(means, [2.5, 97.5]) confint = np.percentile(means, [2.5, 97.5])
print(confint) print(confint)