solutions update from 7ed0c61

This commit is contained in:
rougier
2026-04-29 06:34:30 +00:00
committed by github-actions[bot]
parent 7ed0c61ccd
commit 6cedcb2ad9
4 changed files with 231 additions and 215 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -565,10 +565,18 @@ for index in np.ndindex(Z.shape):
`hint: np.meshgrid, np.exp` `hint: np.meshgrid, np.exp`
```python ```python
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0 sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) G = np.exp(-((X - mu) ** 2 + (Y - mu) ** 2) / (2.0 * sigma**2))
print(G)
# Another solution using the fact that a 2D Gaussian is separable
# Author: Nin17
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
gx = np.exp(-((x - mu) ** 2 / (2.0 * sigma**2)))
gy = np.exp(-((y - mu) ** 2 / (2.0 * sigma**2)))
G = gy[:, None] * gx[None, :]
print(G) print(G)
``` ```
#### 57. How to randomly place p elements in a 2D array? (★★☆) #### 57. How to randomly place p elements in a 2D array? (★★☆)

View File

@@ -565,10 +565,18 @@ for index in np.ndindex(Z.shape):
```python ```python
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0 sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) G = np.exp(-((X - mu) ** 2 + (Y - mu) ** 2) / (2.0 * sigma**2))
print(G)
# Another solution using the fact that a 2D Gaussian is separable
# Author: Nin17
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
gx = np.exp(-((x - mu) ** 2 / (2.0 * sigma**2)))
gy = np.exp(-((y - mu) ** 2 / (2.0 * sigma**2)))
G = gy[:, None] * gx[None, :]
print(G) print(G)
``` ```
#### 57. How to randomly place p elements in a 2D array? (★★☆) #### 57. How to randomly place p elements in a 2D array? (★★☆)

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "a3747e86", "id": "94b10be0",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 100 numpy exercises\n", "# 100 numpy exercises\n",
@@ -18,7 +18,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "8b9ef9a4", "id": "1d0b9900",
"metadata": {}, "metadata": {},
"source": [ "source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically." "File automatically generated. See the documentation to update questions/answers/hints programmatically."
@@ -26,7 +26,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "792c1217", "id": "21313ae9",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Run the `initialise.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",
@@ -36,7 +36,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "541f746c", "id": "c4ad3a14",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -46,7 +46,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "d1743adb", "id": "13bdf6d0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [