Skip to content

Commit a63319a

Browse files
authored
Correction du TP numpy (#419)
* clean file * Remove contextily
1 parent 5ab34aa commit a63319a

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

content/manipulation/01_numpy.qmd

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ description: |
1818
Il est donc indispensable de revoir quelques notions sur ce package avant
1919
d'aller plus loin.
2020
image: scatter_numpy.png
21+
echo: false
2122
---
2223

2324

@@ -45,17 +46,19 @@ la [cheatsheet numpy](https://www.datacamp.com/community/blog/python-numpy-cheat
4546
sur une fonction.
4647

4748
Dans ce chapitre, on ne dérogera pas à la convention qui s'est imposée
48-
d'importer `numpy` de la
49+
d'importer `Numpy` de la
4950
manière suivante :
5051

5152
```{python import np}
53+
#| echo: true
5254
import numpy as np
5355
```
5456

5557
Nous allons également fixer la racine du générateur aléatoire de nombres
5658
afin d'avoir des résultats reproductibles :
5759

5860
```{python}
61+
#| echo: true
5962
np.random.seed(12345)
6063
```
6164

@@ -132,12 +135,14 @@ On peut créer un `array` de plusieurs manières. Pour créer un `array` à part
132135
il suffit d'utiliser la méthode `array`:
133136

134137
```{python}
138+
#| echo: true
135139
np.array([1,2,5])
136140
```
137141

138142
Il est possible d'ajouter un argument `dtype` pour contraindre le type du *array*:
139143

140144
```{python}
145+
#| echo: true
141146
np.array([["a","z","e"],["r","t"],["y"]], dtype="object")
142147
```
143148

@@ -149,32 +154,47 @@ Il existe aussi des méthodes pratiques pour créer des array:
149154
* séquences aléatoires: fonctions de génération de nombres aléatoires: `np.rand.uniform`, `np.rand.normal`, etc.
150155
* tableau sous forme de matrice identité: `np.eye`
151156

157+
Ceci donne ainsi, pour les séquences logiques:
152158

153159
```{python}
160+
#| echo: true
154161
np.arange(0,10)
155162
```
156163

157164
```{python}
165+
#| echo: true
158166
np.arange(0,10,3)
159167
```
160168

161169
```{python}
170+
#| echo: true
162171
np.linspace(0, 1, 5)
163172
```
164173

174+
Pour un _array_ initialisé à 0:
175+
165176
```{python}
177+
#| echo: true
166178
np.zeros(10, dtype=int)
167179
```
168180

181+
ou initialisé à 1:
182+
169183
```{python}
184+
#| echo: true
170185
np.ones((3, 5), dtype=float)
171186
```
172187

188+
ou encore initialisé à 3.14:
189+
173190
```{python}
174191
np.full((3, 5), 3.14)
175192
```
176193

194+
Enfin, pour créer la matrice $I_3$:
195+
177196
```{python}
197+
#| echo: true
178198
np.eye(3)
179199
```
180200

@@ -198,9 +218,7 @@ Générer:
198218

199219

200220
```{python}
201-
#| include: false
202-
203-
# Correction
221+
#| output: false
204222
X = np.random.uniform(0,1,1000)
205223
Y = np.random.normal(0,np.sqrt(2),1000)
206224
@@ -214,6 +232,7 @@ np.var(Y)
214232
La structure la plus simple est l'_array_ unidimensionnel:
215233

216234
```{python}
235+
#| echo: true
217236
x = np.arange(10)
218237
print(x)
219238
```
@@ -245,6 +264,7 @@ x[K-1]
245264
Pour sélectionner uniquement un élément, on fera ainsi:
246265

247266
```{python}
267+
#| echo: true
248268
x = np.arange(10)
249269
x[2]
250270
```
@@ -273,7 +293,7 @@ Prenez `x = np.arange(10)` et...
273293

274294

275295
```{python}
276-
#| include: false
296+
#| output: false
277297
278298
# Correction
279299
x[[0,3,5]]
@@ -315,13 +335,15 @@ Ces comparaisons fonctionnent aussi sur les tableaux multidimensionnels grâce a
315335
*broadcasting* sur lequel nous reviendrons :
316336

317337
```{python}
338+
#| echo: true
318339
x = np.arange(10)
319340
x2 = np.array([[-1,1,-2],[-3,2,0]])
320341
print(x)
321342
print(x2)
322343
```
323344

324345
```{python}
346+
#| echo: true
325347
x==2
326348
x2<0
327349
```
@@ -356,7 +378,7 @@ et rapportez les à la somme des valeurs de `x` (en valeur absolue)
356378

357379

358380
```{python}
359-
#| include: false
381+
#| output: false
360382
361383
# Correction
362384
x = np.random.normal(size=10000)
@@ -381,12 +403,14 @@ Parmi elles, on peut retrouver:
381403
Soit
382404

383405
```{python}
406+
#| echo: true
384407
x = np.random.normal(0, size=(3, 4))
385408
```
386409

387410
un *array* multidimensionnel et
388411

389412
```{python}
413+
#| echo: true
390414
y = np.array([np.nan, 0, 1])
391415
```
392416

@@ -413,7 +437,7 @@ Note : Jetez un oeil à ce que correspond le paramètre `axis` dans `numpy` en v
413437

414438

415439
```{python}
416-
#| include: false
440+
#| output: false
417441
418442
# Correction
419443
x = np.random.normal(0, size=(3, 4))
@@ -438,13 +462,14 @@ print(np.any(x>0, axis = 1))
438462
Dans cette section, on utilisera un array multidimensionnel:
439463

440464
```{python}
465+
#| echo: true
441466
x = np.random.normal(0, size=(3, 4))
442467
```
443468

444-
### Statistiques sur un array
469+
### Statistiques sur un _array_
445470

446471
Pour les statistiques descriptives classiques,
447-
`numpy` propose un certain nombre de fonctions déjà implémentées,
472+
`Numpy` propose un certain nombre de fonctions déjà implémentées,
448473
qui peuvent être combinées avec l'argument `axis`
449474

450475
::: {.cell .markdown}
@@ -467,7 +492,7 @@ L'appliquer sur `x` en jouant avec l'argument axis
467492

468493

469494
```{python}
470-
#| include: false
495+
#| output: false
471496
472497
# Correction
473498
print(x)
@@ -480,7 +505,7 @@ print(x.sum(axis = 1).sum())
480505
```
481506

482507
```{python}
483-
#| include: false
508+
#| output: false
484509
485510
# Correction
486511
@@ -524,12 +549,14 @@ les fonctions `np.concatenate`, `np.vstack` ou la méthode `.r_` (concaténation
524549
`np.hstack` ou la méthode `.column_stack` ou `.c_` (concaténation *column-wise*)
525550

526551
```{python}
552+
#| echo: true
527553
x = np.random.normal(size = 10)
528554
```
529555

530556
Pour ordonner un array, on utilise `np.sort`
531557

532558
```{python}
559+
#| echo: true
533560
x = np.array([7, 2, 3, 1, 6, 5, 4])
534561
535562
np.sort(x)
@@ -538,6 +565,7 @@ np.sort(x)
538565
Si on désire faire un ré-ordonnement partiel pour trouver les _k_ valeurs les plus petites d'un `array` sans les ordonner, on utilise `partition`:
539566

540567
```{python}
568+
#| echo: true
541569
np.partition(x, 3)
542570
```
543571

@@ -553,6 +581,7 @@ La différence peut être comprise à partir de l'exemple suivant. Le *broadcast
553581
de transformer le scalaire `5` en *array* de dimension 3:
554582

555583
```{python}
584+
#| echo: true
556585
a = np.array([0, 1, 2])
557586
558587
b = np.array([5, 5, 5])
@@ -628,8 +657,7 @@ for i in range(X.shape[0]):
628657

629658

630659
```{python}
631-
#| include: false
632-
#| echo: false
660+
#| output: false
633661
634662
# Correction
635663
@@ -645,8 +673,7 @@ plt.scatter(X[:, 0], X[:, 1], s=100)
645673

646674

647675
```{python}
648-
#| include: false
649-
#| echo: false
676+
#| output: false
650677
651678
fig = plt.figure()
652679
plt.scatter(X[:, 0], X[:, 1], s=100)
@@ -659,8 +686,7 @@ Pour la question 2, vous devriez obtenir un graphique ayant cet aspect :
659686
![](scatter_numpy.png)
660687

661688
```{python}
662-
#| include: false
663-
#| echo: false
689+
#| output: false
664690
665691
# 3. Construire la matrice des distances euclidiennes
666692
print(X.shape)
@@ -753,7 +779,7 @@ sur votre matrice.
753779
:::
754780

755781
```{python}
756-
#| echo: false
782+
#| output: false
757783
758784
# Question 1
759785
M = np.array([[0, 0, 0, 0, 1],
@@ -764,7 +790,6 @@ M = np.array([[0, 0, 0, 0, 1],
764790
```
765791

766792
```{python}
767-
#| echo: false
768793
import networkx as nx
769794
770795
G = nx.from_numpy_array(M)
@@ -775,8 +800,6 @@ plt.savefig("scatter_numpy.png", bbox_inches = "tight")
775800

776801

777802
```{python}
778-
#| echo: false
779-
780803
"""PageRank algorithm with explicit number of iterations.
781804
782805
Returns
@@ -822,7 +845,6 @@ Le site 1 est assez central car il est référencé 2 fois. Le site
822845
5 est lui également central puisqu'il est référencé par le site 1.
823846

824847
```{python}
825-
#| echo: false
826848
v
827849
```
828850

content/manipulation/04c_API_TP.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ import contextily as ctx
834834
fig,ax = plt.subplots(figsize=(10, 10))
835835
circle.to_crs("EPSG:3857").plot(ax = ax, color = 'red')
836836
pal.to_crs("EPSG:3857").plot(ax = ax, color = 'green')
837-
ctx.add_basemap(ax, source = ctx.providers.Stamen.Toner)
837+
#ctx.add_basemap(ax, source = ctx.providers.Stamen.Toner)
838838
ax
839839
```
840840

@@ -846,7 +846,7 @@ import contextily as ctx
846846
fig,ax = plt.subplots(figsize=(10, 10))
847847
circle.to_crs("EPSG:3857").plot(ax = ax, color = 'red')
848848
pal.to_crs("EPSG:3857").plot(ax = ax, color = 'green')
849-
ctx.add_basemap(ax, source = ctx.providers.Stamen.Toner)
849+
#ctx.add_basemap(ax, source = ctx.providers.Stamen.Toner)
850850
ax
851851
852852
plt.savefig('map_buffer.png', bbox_inches='tight')

0 commit comments

Comments
 (0)