Merge pull request #122 from acharles7/enhancement
Q29 alternate solution
This commit is contained in:
commit
df801f8cbc
@ -564,7 +564,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"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 (★★☆)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -113,7 +113,7 @@ np.sqrt(-1) == np.emath.sqrt(-1)
|
||||
|
||||
#### 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 (★★☆)
|
||||
|
||||
|
@ -97,7 +97,7 @@ np.array([np.nan]).astype(int).astype(float)
|
||||
```
|
||||
`No hints provided...`
|
||||
#### 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? (★☆☆)
|
||||
`hint: np.intersect1d`
|
||||
#### 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'])`
|
||||
#### 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=)`
|
||||
#### 36. Extract the integer part of a random array using 5 different methods (★★☆)
|
||||
`hint: %, np.floor, np.ceil, astype, np.trunc`
|
||||
#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
|
||||
`hint: %, np.floor, astype, np.trunc`
|
||||
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
|
||||
`hint: np.arange`
|
||||
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
|
||||
|
@ -72,8 +72,8 @@ print(Z)
|
||||
`hint: reshape`
|
||||
|
||||
```python
|
||||
nz = np.nonzero([1,2,0,0,4,0])
|
||||
print(nz)
|
||||
Z = np.arange(9).reshape(3, 3)
|
||||
print(Z)
|
||||
```
|
||||
#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
|
||||
`hint: np.nonzero`
|
||||
@ -264,13 +264,16 @@ print(np.array(0) // np.array(0))
|
||||
print(np.array([np.nan]).astype(int).astype(float))
|
||||
```
|
||||
#### 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
|
||||
# Author: Charles R Harris
|
||||
|
||||
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? (★☆☆)
|
||||
`hint: np.intersect1d`
|
||||
@ -292,8 +295,8 @@ Z = np.ones(1) / 0
|
||||
_ = np.seterr(**defaults)
|
||||
|
||||
# Equivalently with a context manager
|
||||
nz = np.nonzero([1,2,0,0,4,0])
|
||||
print(nz)
|
||||
with np.errstate(all="ignore"):
|
||||
np.arange(3) / 0
|
||||
```
|
||||
#### 32. Is the following expressions true? (★☆☆)
|
||||
```python
|
||||
@ -308,9 +311,9 @@ np.sqrt(-1) == np.emath.sqrt(-1)
|
||||
`hint: np.datetime64, np.timedelta64`
|
||||
|
||||
```python
|
||||
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
|
||||
today = np.datetime64('today', 'D')
|
||||
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
|
||||
yesterday = np.datetime64('today') - np.timedelta64(1)
|
||||
today = np.datetime64('today')
|
||||
tomorrow = np.datetime64('today') + np.timedelta64(1)
|
||||
```
|
||||
#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)
|
||||
`hint: np.arange(dtype=datetime64['D'])`
|
||||
@ -331,17 +334,17 @@ np.divide(A,2,out=A)
|
||||
np.negative(A,out=A)
|
||||
np.multiply(A,B,out=A)
|
||||
```
|
||||
#### 36. Extract the integer part of a random array using 5 different methods (★★☆)
|
||||
`hint: %, np.floor, np.ceil, astype, np.trunc`
|
||||
#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
|
||||
`hint: %, np.floor, astype, np.trunc`
|
||||
|
||||
```python
|
||||
Z = np.random.uniform(0,10,10)
|
||||
|
||||
print (Z - Z%1)
|
||||
print (np.floor(Z))
|
||||
print (np.ceil(Z)-1)
|
||||
print (Z.astype(int))
|
||||
print (np.trunc(Z))
|
||||
print(Z - Z%1)
|
||||
print(Z // 1)
|
||||
print(np.floor(Z))
|
||||
print(Z.astype(int))
|
||||
print(np.trunc(Z))
|
||||
```
|
||||
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
|
||||
`hint: np.arange`
|
||||
@ -717,7 +720,7 @@ print(pd.Series(D).groupby(S).mean())
|
||||
A = 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))
|
||||
|
||||
# Fast version
|
||||
|
@ -72,8 +72,8 @@ print(Z)
|
||||
|
||||
|
||||
```python
|
||||
nz = np.nonzero([1,2,0,0,4,0])
|
||||
print(nz)
|
||||
Z = np.arange(9).reshape(3, 3)
|
||||
print(Z)
|
||||
```
|
||||
#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
|
||||
|
||||
@ -211,7 +211,7 @@ print(Z)
|
||||
# Author: Evgeni Burovski
|
||||
|
||||
Z = np.arange(11)
|
||||
Z[(3 < Z) & (Z < 8)] *= -1
|
||||
Z[(3 < Z) & (Z <= 8)] *= -1
|
||||
print(Z)
|
||||
```
|
||||
#### 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
|
||||
|
||||
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? (★☆☆)
|
||||
|
||||
@ -292,8 +295,8 @@ Z = np.ones(1) / 0
|
||||
_ = np.seterr(**defaults)
|
||||
|
||||
# Equivalently with a context manager
|
||||
nz = np.nonzero([1,2,0,0,4,0])
|
||||
print(nz)
|
||||
with np.errstate(all="ignore"):
|
||||
np.arange(3) / 0
|
||||
```
|
||||
#### 32. Is the following expressions true? (★☆☆)
|
||||
```python
|
||||
@ -308,9 +311,9 @@ np.sqrt(-1) == np.emath.sqrt(-1)
|
||||
|
||||
|
||||
```python
|
||||
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
|
||||
today = np.datetime64('today', 'D')
|
||||
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
|
||||
yesterday = np.datetime64('today') - np.timedelta64(1)
|
||||
today = np.datetime64('today')
|
||||
tomorrow = np.datetime64('today') + np.timedelta64(1)
|
||||
```
|
||||
#### 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.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
|
||||
Z = np.random.uniform(0,10,10)
|
||||
|
||||
print (Z - Z%1)
|
||||
print (np.floor(Z))
|
||||
print (np.ceil(Z)-1)
|
||||
print (Z.astype(int))
|
||||
print (np.trunc(Z))
|
||||
print(Z - Z%1)
|
||||
print(Z // 1)
|
||||
print(np.floor(Z))
|
||||
print(Z.astype(int))
|
||||
print(np.trunc(Z))
|
||||
```
|
||||
#### 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))
|
||||
B = np.random.uniform(0,1,(5,5))
|
||||
|
||||
# Slow version
|
||||
# Slow version
|
||||
np.diag(np.dot(A, B))
|
||||
|
||||
# Fast version
|
||||
|
@ -337,13 +337,16 @@ print(np.array([np.nan]).astype(int).astype(float))
|
||||
How to round away from zero a float array ? (★☆☆)
|
||||
|
||||
< h29
|
||||
hint: np.uniform, np.copysign, np.ceil, np.abs
|
||||
hint: np.uniform, np.copysign, np.ceil, np.abs, np.where
|
||||
|
||||
< a29
|
||||
# Author: Charles R Harris
|
||||
|
||||
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
|
||||
How to find common values between two arrays? (★☆☆)
|
||||
@ -910,7 +913,7 @@ hint: np.diag
|
||||
A = 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))
|
||||
|
||||
# Fast version
|
||||
@ -1457,4 +1460,3 @@ idx = np.random.randint(0, X.size, (N, X.size))
|
||||
means = X[idx].mean(axis=1)
|
||||
confint = np.percentile(means, [2.5, 97.5])
|
||||
print(confint)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user