@@ -18,6 +18,7 @@ description: |
18
18
Il est donc indispensable de revoir quelques notions sur ce package avant
19
19
d'aller plus loin.
20
20
image : scatter_numpy.png
21
+ echo : false
21
22
---
22
23
23
24
@@ -45,17 +46,19 @@ la [cheatsheet numpy](https://www.datacamp.com/community/blog/python-numpy-cheat
45
46
sur une fonction.
46
47
47
48
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
49
50
manière suivante :
50
51
51
52
``` {python import np}
53
+ #| echo: true
52
54
import numpy as np
53
55
```
54
56
55
57
Nous allons également fixer la racine du générateur aléatoire de nombres
56
58
afin d'avoir des résultats reproductibles :
57
59
58
60
``` {python}
61
+ #| echo: true
59
62
np.random.seed(12345)
60
63
```
61
64
@@ -132,12 +135,14 @@ On peut créer un `array` de plusieurs manières. Pour créer un `array` à part
132
135
il suffit d'utiliser la méthode ` array ` :
133
136
134
137
``` {python}
138
+ #| echo: true
135
139
np.array([1,2,5])
136
140
```
137
141
138
142
Il est possible d'ajouter un argument ` dtype ` pour contraindre le type du * array* :
139
143
140
144
``` {python}
145
+ #| echo: true
141
146
np.array([["a","z","e"],["r","t"],["y"]], dtype="object")
142
147
```
143
148
@@ -149,32 +154,47 @@ Il existe aussi des méthodes pratiques pour créer des array:
149
154
* séquences aléatoires: fonctions de génération de nombres aléatoires: ` np.rand.uniform ` , ` np.rand.normal ` , etc.
150
155
* tableau sous forme de matrice identité: ` np.eye `
151
156
157
+ Ceci donne ainsi, pour les séquences logiques:
152
158
153
159
``` {python}
160
+ #| echo: true
154
161
np.arange(0,10)
155
162
```
156
163
157
164
``` {python}
165
+ #| echo: true
158
166
np.arange(0,10,3)
159
167
```
160
168
161
169
``` {python}
170
+ #| echo: true
162
171
np.linspace(0, 1, 5)
163
172
```
164
173
174
+ Pour un _ array_ initialisé à 0:
175
+
165
176
``` {python}
177
+ #| echo: true
166
178
np.zeros(10, dtype=int)
167
179
```
168
180
181
+ ou initialisé à 1:
182
+
169
183
``` {python}
184
+ #| echo: true
170
185
np.ones((3, 5), dtype=float)
171
186
```
172
187
188
+ ou encore initialisé à 3.14:
189
+
173
190
``` {python}
174
191
np.full((3, 5), 3.14)
175
192
```
176
193
194
+ Enfin, pour créer la matrice $I_3$:
195
+
177
196
``` {python}
197
+ #| echo: true
178
198
np.eye(3)
179
199
```
180
200
@@ -198,9 +218,7 @@ Générer:
198
218
199
219
200
220
``` {python}
201
- #| include: false
202
-
203
- # Correction
221
+ #| output: false
204
222
X = np.random.uniform(0,1,1000)
205
223
Y = np.random.normal(0,np.sqrt(2),1000)
206
224
@@ -214,6 +232,7 @@ np.var(Y)
214
232
La structure la plus simple est l'_ array_ unidimensionnel:
215
233
216
234
``` {python}
235
+ #| echo: true
217
236
x = np.arange(10)
218
237
print(x)
219
238
```
@@ -245,6 +264,7 @@ x[K-1]
245
264
Pour sélectionner uniquement un élément, on fera ainsi:
246
265
247
266
``` {python}
267
+ #| echo: true
248
268
x = np.arange(10)
249
269
x[2]
250
270
```
@@ -273,7 +293,7 @@ Prenez `x = np.arange(10)` et...
273
293
274
294
275
295
``` {python}
276
- #| include : false
296
+ #| output : false
277
297
278
298
# Correction
279
299
x[[0,3,5]]
@@ -315,13 +335,15 @@ Ces comparaisons fonctionnent aussi sur les tableaux multidimensionnels grâce a
315
335
* broadcasting* sur lequel nous reviendrons :
316
336
317
337
``` {python}
338
+ #| echo: true
318
339
x = np.arange(10)
319
340
x2 = np.array([[-1,1,-2],[-3,2,0]])
320
341
print(x)
321
342
print(x2)
322
343
```
323
344
324
345
``` {python}
346
+ #| echo: true
325
347
x==2
326
348
x2<0
327
349
```
@@ -356,7 +378,7 @@ et rapportez les à la somme des valeurs de `x` (en valeur absolue)
356
378
357
379
358
380
``` {python}
359
- #| include : false
381
+ #| output : false
360
382
361
383
# Correction
362
384
x = np.random.normal(size=10000)
@@ -381,12 +403,14 @@ Parmi elles, on peut retrouver:
381
403
Soit
382
404
383
405
``` {python}
406
+ #| echo: true
384
407
x = np.random.normal(0, size=(3, 4))
385
408
```
386
409
387
410
un * array* multidimensionnel et
388
411
389
412
``` {python}
413
+ #| echo: true
390
414
y = np.array([np.nan, 0, 1])
391
415
```
392
416
@@ -413,7 +437,7 @@ Note : Jetez un oeil à ce que correspond le paramètre `axis` dans `numpy` en v
413
437
414
438
415
439
``` {python}
416
- #| include : false
440
+ #| output : false
417
441
418
442
# Correction
419
443
x = np.random.normal(0, size=(3, 4))
@@ -438,13 +462,14 @@ print(np.any(x>0, axis = 1))
438
462
Dans cette section, on utilisera un array multidimensionnel:
439
463
440
464
``` {python}
465
+ #| echo: true
441
466
x = np.random.normal(0, size=(3, 4))
442
467
```
443
468
444
- ### Statistiques sur un array
469
+ ### Statistiques sur un _ array _
445
470
446
471
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,
448
473
qui peuvent être combinées avec l'argument ` axis `
449
474
450
475
::: {.cell .markdown}
@@ -467,7 +492,7 @@ L'appliquer sur `x` en jouant avec l'argument axis
467
492
468
493
469
494
``` {python}
470
- #| include : false
495
+ #| output : false
471
496
472
497
# Correction
473
498
print(x)
@@ -480,7 +505,7 @@ print(x.sum(axis = 1).sum())
480
505
```
481
506
482
507
``` {python}
483
- #| include : false
508
+ #| output : false
484
509
485
510
# Correction
486
511
@@ -524,12 +549,14 @@ les fonctions `np.concatenate`, `np.vstack` ou la méthode `.r_` (concaténation
524
549
` np.hstack ` ou la méthode ` .column_stack ` ou ` .c_ ` (concaténation * column-wise* )
525
550
526
551
``` {python}
552
+ #| echo: true
527
553
x = np.random.normal(size = 10)
528
554
```
529
555
530
556
Pour ordonner un array, on utilise ` np.sort `
531
557
532
558
``` {python}
559
+ #| echo: true
533
560
x = np.array([7, 2, 3, 1, 6, 5, 4])
534
561
535
562
np.sort(x)
@@ -538,6 +565,7 @@ np.sort(x)
538
565
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 ` :
539
566
540
567
``` {python}
568
+ #| echo: true
541
569
np.partition(x, 3)
542
570
```
543
571
@@ -553,6 +581,7 @@ La différence peut être comprise à partir de l'exemple suivant. Le *broadcast
553
581
de transformer le scalaire ` 5 ` en * array* de dimension 3:
554
582
555
583
``` {python}
584
+ #| echo: true
556
585
a = np.array([0, 1, 2])
557
586
558
587
b = np.array([5, 5, 5])
@@ -628,8 +657,7 @@ for i in range(X.shape[0]):
628
657
629
658
630
659
``` {python}
631
- #| include: false
632
- #| echo: false
660
+ #| output: false
633
661
634
662
# Correction
635
663
@@ -645,8 +673,7 @@ plt.scatter(X[:, 0], X[:, 1], s=100)
645
673
646
674
647
675
``` {python}
648
- #| include: false
649
- #| echo: false
676
+ #| output: false
650
677
651
678
fig = plt.figure()
652
679
plt.scatter(X[:, 0], X[:, 1], s=100)
@@ -659,8 +686,7 @@ Pour la question 2, vous devriez obtenir un graphique ayant cet aspect :
659
686
![ ] ( scatter_numpy.png )
660
687
661
688
``` {python}
662
- #| include: false
663
- #| echo: false
689
+ #| output: false
664
690
665
691
# 3. Construire la matrice des distances euclidiennes
666
692
print(X.shape)
@@ -753,7 +779,7 @@ sur votre matrice.
753
779
:::
754
780
755
781
``` {python}
756
- #| echo : false
782
+ #| output : false
757
783
758
784
# Question 1
759
785
M = np.array([[0, 0, 0, 0, 1],
@@ -764,7 +790,6 @@ M = np.array([[0, 0, 0, 0, 1],
764
790
```
765
791
766
792
``` {python}
767
- #| echo: false
768
793
import networkx as nx
769
794
770
795
G = nx.from_numpy_array(M)
@@ -775,8 +800,6 @@ plt.savefig("scatter_numpy.png", bbox_inches = "tight")
775
800
776
801
777
802
``` {python}
778
- #| echo: false
779
-
780
803
"""PageRank algorithm with explicit number of iterations.
781
804
782
805
Returns
@@ -822,7 +845,6 @@ Le site 1 est assez central car il est référencé 2 fois. Le site
822
845
5 est lui également central puisqu'il est référencé par le site 1.
823
846
824
847
``` {python}
825
- #| echo: false
826
848
v
827
849
```
828
850
0 commit comments