Compare commits
23 Commits
9be9c8cd23
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e865a958ed | ||
|
|
a4bf63e6bc | ||
|
|
d421b9750c | ||
|
|
4a3372ed11 | ||
|
|
6447594ad6 | ||
|
|
9642cab77c | ||
|
|
4ccd5c0689 | ||
|
|
bfd5e4dbcb | ||
|
|
564a329151 | ||
|
|
19dcbdd422 | ||
|
|
5c3481111f | ||
|
|
2db57d0e16 | ||
|
|
836eab7578 | ||
|
|
521b1c0c75 | ||
|
|
18a5097890 | ||
|
|
5b6d050a52 | ||
|
|
8ea76d1f91 | ||
|
|
fc72693160 | ||
|
|
301e0eb61e | ||
|
|
59a403da66 | ||
|
|
fca297a6e1 | ||
|
|
ed0a067b01 | ||
|
|
ee15dec2c7 |
File diff suppressed because it is too large
Load Diff
@@ -38,6 +38,9 @@ print(Z)
|
||||
```python
|
||||
Z = np.zeros((10,10))
|
||||
print("%d bytes" % (Z.size * Z.itemsize))
|
||||
|
||||
# Simpler alternative
|
||||
print("%d bytes" % Z.nbytes)
|
||||
```
|
||||
#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
|
||||
`hint: np.info`
|
||||
@@ -128,7 +131,7 @@ Z = np.ones((5,5))
|
||||
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
|
||||
print(Z)
|
||||
|
||||
# Using fancy indexing
|
||||
# Not a solution to this problem but good to know: using fancy indexing for in-place edit
|
||||
Z[:, [0, -1]] = 0
|
||||
Z[[0, -1], :] = 0
|
||||
print(Z)
|
||||
@@ -545,7 +548,7 @@ s = StringIO('''1, 2, 3, 4, 5
|
||||
|
||||
, , 9,10,11
|
||||
''')
|
||||
Z = np.genfromtxt(s, delimiter=",", dtype=np.int)
|
||||
Z = np.genfromtxt(s, delimiter=",", dtype = int, filling_values = 0)
|
||||
print(Z)
|
||||
```
|
||||
#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)
|
||||
@@ -993,7 +996,8 @@ print(sliding_window_view(Z, window_shape=4))
|
||||
|
||||
Z = np.random.uniform(0,1,(10,10))
|
||||
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
|
||||
rank = np.sum(S > 1e-10)
|
||||
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
|
||||
rank = np.sum(S > threshold)
|
||||
print(rank)
|
||||
|
||||
# alternative solution:
|
||||
@@ -1086,11 +1090,8 @@ k = 4
|
||||
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
||||
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
||||
|
||||
# Author: Jeff Luo (@Jeff1999)
|
||||
|
||||
Z = np.ones((16, 16))
|
||||
k = 4
|
||||
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
|
||||
# alternative solution (by @Gattocrucco)
|
||||
S = Z.reshape(4, 4, 4, 4).sum((1, 3))
|
||||
```
|
||||
#### 88. How to implement the Game of Life using numpy arrays? (★★★)
|
||||
`No hints provided...`
|
||||
|
||||
@@ -38,6 +38,9 @@ print(Z)
|
||||
```python
|
||||
Z = np.zeros((10,10))
|
||||
print("%d bytes" % (Z.size * Z.itemsize))
|
||||
|
||||
# Simpler alternative
|
||||
print("%d bytes" % Z.nbytes)
|
||||
```
|
||||
#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
|
||||
|
||||
@@ -128,7 +131,7 @@ Z = np.ones((5,5))
|
||||
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
|
||||
print(Z)
|
||||
|
||||
# Using fancy indexing
|
||||
# Not a solution to this problem but good to know: using fancy indexing for in-place edit
|
||||
Z[:, [0, -1]] = 0
|
||||
Z[[0, -1], :] = 0
|
||||
print(Z)
|
||||
@@ -545,7 +548,7 @@ s = StringIO('''1, 2, 3, 4, 5
|
||||
|
||||
, , 9,10,11
|
||||
''')
|
||||
Z = np.genfromtxt(s, delimiter=",", dtype=np.int)
|
||||
Z = np.genfromtxt(s, delimiter=",", dtype = int, filling_values = 0)
|
||||
print(Z)
|
||||
```
|
||||
#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)
|
||||
@@ -993,7 +996,8 @@ print(sliding_window_view(Z, window_shape=4))
|
||||
|
||||
Z = np.random.uniform(0,1,(10,10))
|
||||
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
|
||||
rank = np.sum(S > 1e-10)
|
||||
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
|
||||
rank = np.sum(S > threshold)
|
||||
print(rank)
|
||||
|
||||
# alternative solution:
|
||||
@@ -1086,11 +1090,8 @@ k = 4
|
||||
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
||||
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
||||
|
||||
# Author: Jeff Luo (@Jeff1999)
|
||||
|
||||
Z = np.ones((16, 16))
|
||||
k = 4
|
||||
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
|
||||
# alternative solution (by @Gattocrucco)
|
||||
S = Z.reshape(4, 4, 4, 4).sum((1, 3))
|
||||
```
|
||||
#### 88. How to implement the Game of Life using numpy arrays? (★★★)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "738eba3f",
|
||||
"id": "a3747e86",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 100 numpy exercises\n",
|
||||
@@ -18,7 +18,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f65f901e",
|
||||
"id": "8b9ef9a4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"File automatically generated. See the documentation to update questions/answers/hints programmatically."
|
||||
@@ -26,17 +26,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "15045647",
|
||||
"id": "792c1217",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n",
|
||||
"Run the `initialise.py` module, then call a random question with `pick()` an hint towards its solution with\n",
|
||||
"`hint(n)` and the answer with `answer(n)`, where n is the number of the picked question."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0d23aa5b",
|
||||
"id": "541f746c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -46,7 +46,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4a6a613b",
|
||||
"id": "d1743adb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
||||
@@ -37,6 +37,9 @@ hint: size, itemsize
|
||||
Z = np.zeros((10,10))
|
||||
print("%d bytes" % (Z.size * Z.itemsize))
|
||||
|
||||
# Simpler alternative
|
||||
print("%d bytes" % Z.nbytes)
|
||||
|
||||
< q5
|
||||
How to get the documentation of the numpy add function from the command line? (★☆☆)
|
||||
|
||||
@@ -162,7 +165,7 @@ Z = np.ones((5,5))
|
||||
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
|
||||
print(Z)
|
||||
|
||||
# Using fancy indexing
|
||||
# Not a solution to this problem but good to know: using fancy indexing for in-place edit
|
||||
Z[:, [0, -1]] = 0
|
||||
Z[[0, -1], :] = 0
|
||||
print(Z)
|
||||
@@ -693,7 +696,7 @@ s = StringIO('''1, 2, 3, 4, 5
|
||||
|
||||
, , 9,10,11
|
||||
''')
|
||||
Z = np.genfromtxt(s, delimiter=",", dtype=np.int)
|
||||
Z = np.genfromtxt(s, delimiter=",", dtype = int, filling_values = 0)
|
||||
print(Z)
|
||||
|
||||
< q55
|
||||
@@ -1225,7 +1228,8 @@ hint: np.linalg.svd, np.linalg.matrix_rank
|
||||
|
||||
Z = np.random.uniform(0,1,(10,10))
|
||||
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
|
||||
rank = np.sum(S > 1e-10)
|
||||
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
|
||||
rank = np.sum(S > threshold)
|
||||
print(rank)
|
||||
|
||||
# alternative solution:
|
||||
@@ -1333,11 +1337,8 @@ k = 4
|
||||
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
||||
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
||||
|
||||
# Author: Jeff Luo (@Jeff1999)
|
||||
|
||||
Z = np.ones((16, 16))
|
||||
k = 4
|
||||
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
|
||||
# alternative solution (by @Gattocrucco)
|
||||
S = Z.reshape(4, 4, 4, 4).sum((1, 3))
|
||||
|
||||
< q88
|
||||
How to implement the Game of Life using numpy arrays? (★★★)
|
||||
|
||||
Reference in New Issue
Block a user