Compare commits

...

23 Commits

Author SHA1 Message Date
rougier
e865a958ed solutions update from a4bf63e 2026-02-09 17:44:51 +00:00
Nicolas P. Rougier
a4bf63e6bc Merge pull request #250 from amin-sehati/master
Remove an incorrect solution (fancy indexing) from Numpy exercise No16
2026-02-09 18:44:25 +01:00
Amin Sehati
d421b9750c Added a fancy indexing solution for 16 2026-02-09 08:54:37 -05:00
Amin Sehati
4a3372ed11 Remove an incorrect solution (fancy indexing) from Numpy exercise No. 16 in the solutions files. 2026-02-08 16:58:53 -05:00
rougier
6447594ad6 solutions update from 9642cab 2025-11-06 16:13:10 +00:00
Nicolas P. Rougier
9642cab77c Merge pull request #249 from LegendTejas/fix-issue-247-ktx
Fix #247 : replaced deprecated np.int and corrected formatting in exercises100.ktx
2025-11-06 17:12:45 +01:00
Tejas Tp
4ccd5c0689 Format np.genfromtxt parameters for consistency 2025-11-06 18:49:43 +05:30
Tejas Tp
bfd5e4dbcb Update np.genfromtxt to use int and filling_values
Changed dtype from np.int to int and added filling_values parameter.
2025-11-06 18:45:05 +05:30
Nicolas P. Rougier
564a329151 Re-generated files 2025-08-26 11:15:53 +02:00
rougier
19dcbdd422 solutions update from 5c34811 2025-08-26 09:07:46 +00:00
Nicolas P. Rougier
5c3481111f Merge pull request #244 from Gattocrucco/alt-sol-87
Alternative solution to exercise 87
2025-08-26 11:07:22 +02:00
rougier
2db57d0e16 solutions update from 836eab7 2025-08-26 09:07:01 +00:00
Nicolas P. Rougier
836eab7578 Merge pull request #242 from Gattocrucco/alt-sol-4
Alternative solution to exercise 4
2025-08-26 11:06:42 +02:00
rougier
521b1c0c75 solutions update from 18a5097 2025-08-26 09:06:26 +00:00
Nicolas P. Rougier
18a5097890 Merge pull request #243 from Gattocrucco/improve-sol-82
Improve solution to exercise 82
2025-08-26 11:06:02 +02:00
Gattocrucco
5b6d050a52 reset derived files to original state 2025-08-26 10:56:55 +02:00
Gattocrucco
8ea76d1f91 reset derived files to original state 2025-08-26 10:56:42 +02:00
Gattocrucco
fc72693160 reset derived files to original state 2025-08-26 10:56:10 +02:00
Gattocrucco
301e0eb61e further simplification to the alt solution of 87
The initial formulation contained a redundant `swapaxes`.
2025-08-26 00:16:34 +02:00
Gattocrucco
59a403da66 alternative solution to exercise 87
Added a more elegant (but more cryptic) solution with reshape and
swapaxes.

Removed one of the existing alternative solutions because it was a
duplicate of a previous alternative solution.
2025-08-25 18:13:30 +02:00
Gattocrucco
fca297a6e1 improve solution to exercise 82
The current solution uses a fixed arbitrary threshold of 1e-10 on the
singular values, I replaced it with the common heuristic n*eps*max(s).
2025-08-25 18:06:25 +02:00
Giacomo Petrillo
ed0a067b01 re-generate markdown files 2025-08-25 17:57:43 +02:00
Giacomo Petrillo
ee15dec2c7 alt solution to exercise 4
Uses nbytes instead of itemsize * size
2025-08-25 17:53:21 +02:00
5 changed files with 238 additions and 235 deletions

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -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? (★★★)

View File

@@ -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": [

View File

@@ -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? (★★★)