Vous êtes sur la page 1sur 9

Compte Rendu: Probabilité et Statistique

Nom: Doudech Prenom: Ahmed Amine Classe: LSI-A2-01

TP N°1:
Nous allons utiliser matplotlib pour tracer nos fonctions pour importer les modules
matplotlib et numpy, nous écrivons ces deux lignes de code :

1. import matplotlib.pyplot as plt


2. import numpy as np

pour tracer une fonction, vous pouvez utiliser plt.plot()

1. x = np.array([1, 2, 4, 5])
2. y = np.array([1, 2, 3, 4])
3. plt.figure(figsize=(8, 5))
4. plt.plot(x,x**2, lw=3, marker="o", color='red', ls="dotted", label="courbe 1")
5. plt.plot(y, np.sin(y) , lw=3, marker="o", color="green", label="courbe 2")
6. plt.legend(loc="upper left")
7. plt.title("Statistics of students joining ISSATSo")
8. plt.xlabel("Years")
9. plt.ylabel("Students")
10.
11. plt.savefig("stiisat")
12.
13. plt.show()
14.

On peut utiliser les attributs :

- lw : pour modifier l'épaisseur du trait


- marker : pour changer le marqueur du point de traçage
- color : pour changer la couleur du trait
- ls ou linestyle : pour changer le style du trait de traçage
- label : donner une étiquette à chaque courbe
- savefig : pour sauvegarder l’image de resultat

1
Exercice 1 :
Le tableau présente le prix des ordinateurs Lenovo et dell durant les cinq derniere
années :

1. import matplotlib.pyplot as plt


2.
3. annee = [2015, 2016, 2017, 2018, 2019, 2020]
4. lenovo = [1000, 1050, 1120, 1200, 1300, 1400]
5. dell = [1040, 1100, 1190, 1220, 1340, 1500]
6.
7. plt.plot(annee, lenovo, color="red", label="lenovo")
8. plt.plot(annee, dell, color="green", label="dell")
9. plt.legend(loc="upper left")
10. plt.ylabel("Prix des ordinateurs")
11. plt.xlabel("Les Années")
12. plt.title("L'évolution des prix des ordinateurs durant les cinq derniéres années")
13. plt.savefig("Lenovo-vs-Dell")
14. plt.show()

2
Exercice 2 :
1. import matplotlib.pyplot as plt
2. import numpy as np
3. sangammes = np.array(["O", "A", "B", "AB", "Total"])
4. effectifs = np.array([5, 10, 4, 6, 25])
5.
6. plt.bar(sangammes, effectifs, color="blue")
7. plt.title("La repartition des groupes sanguines des eleves")
8. plt.xlabel("Groupes sanguines")
9. plt.ylabel("Effectives")
10. plt.savefig("repart-sang")
11. plt.show()

3
Exercice 3:
1. import matplotlib.pyplot as plt
2. import numpy as np
3.
4. Labels = ["Action", "Aventure", "Sport", "Strategie", "Jeu de role"]
5. Colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral", "blue"]
6. Telechargement = np.array([1400, 600, 506, 1608, 730])
7.
8. plt.pie(Telechargement, labels=Labels, colors=Colors, autopct="%1.1f")
9. plt.legend()
10. plt.title("La repsentation des telechargement de jeux par genre")
11. plt.show()

Représenter ces données sur un histogramme en découpent l'axe des absc en train
intervalles selon le taux de de 60 à 100 et de 120 à 151

4
TP N°2 : Utilisation de module random
Ont peut utiliser random pour générer des valeur aléatoires, on a les fonctions
randint et randrange :

randint(x, y) : retourne un entier entre x et y, avec y inclus

randrange(x, y) : retourne un entier entre x et y, avec y exclus

1. import random
2. x = random.randint(1, 10); # variable x appartient à l’intervalle I=[1, 10]
3. y = random.randrange(10); # variable y appartient à l’intervalle I=[1, 10
5. print(x)
6. print(y)

random(): Retourne un nombre aléatoire compris entre 0 (inclus) et 1 (exclus)

uniform(x, y) : Retourne un nombre aléatoire compris entre 1 (inclus) et 10 (inclus)

1. x = random.random()
2. print(x)
3. y = random.uniform(1, 10)
4. print(y)

On peut utiliser math.floor(x) pour trouver la plus grande valeur entière inférieur ou
égale à un nombre donné x.

1. import math
2. math.floor(y)

Exemple: Ecrire la partie entière d'un nombre aléatoire dans l'intervalle [0, 16]

Méthode 1 : Méthode 2 :
1. import random 1. import random
2. random.randint(0, 16) 2. import math
3. math.floor(random.uniform(0, 17))

Application des méthodes de module random sur les listes :

1. a = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]


2.
3. print(random.choice(a))
4. random.shuffle(a) # shuffles the list
5. print(a)
6. print(random.choice(a))
7.
8. b = random.sample(a, 5) # a random sample from the list a
9. print(b)

5
choice(a): retourne un element aleatoire de la liste a

shuffle(a): modifie la liste directement et mélange aléatoirement les éléments de la


liste a

sample(a, n) : retourne une nouvelle liste contenant n éléments choisis de manière


aléatoire parmi les éléments de la liste a sans remplacement

Definition d’une fonction uniforme :

1. import random
2. import math
3. def uniforme(a, b):
4. return (a + math.floor(random.random() * ((b - a) + 1)))

Traçage d’une fonction uniforme:

1. import matplotlib.pyplot as plt


2.
3. a = 1
4. b = 6
5. n = b - a + 1
6.
7. x = list(range(a, b + 1))
8. y = [1 / n for _ in x]
9.
10. plt.plot(x,y, ".", marker="x") # "." means there's no curve in the plot (only points)
11.
12. plt.show()

6
Traçage de distribution de la loi binomiale:

1. import matplotlib.pyplot as plt


2. import math
3.
4. def binomfdp(n, p, k):
5. c = math.factorial(n)/math.factorial(k)*math.factorial(n-k)
6. x = c* p ** k * (1-p) **(n-k)
7. return x
8.
9. def binomfep(n, p, k):
10. s = binomfdp(n, p, 0)
11. for i in range(k):
12. s = s + binomfdp(n, p, i + 1)
13. return s
14. # Paramètres de la loi binomiale
15. n = 20 # Nombre d'essais
16. p = 0.5 # Probabilité
17.
18. # Calcul des probabilités pour chaque valeur de la variable aléatoire
19. x = [i for i in range(1, n+1)]
20. y = [binomfdp(n, p, k) for k in range(1, n + 1)]
21. # Tracer la loi binomiale
22. plt.bar(x, y, label='Loi binomiale (n=20, p=0.5)', alpha=0.7)
23. plt.title('Distribution de la loi binomiale')
24. plt.xlabel('Valeurs de la variable aléatoire')
25. plt.ylabel('Probabilité')
26. plt.legend()
27. plt.grid(True)
28. plt.show()

7
TP N°3:
Distribution binomiale pour n= 10, p=0.5

1. from math import factorial


2. import matplotlib.pyplot as plt
3. import random as rd
4. import numpy as np
5. import math
6. from math import factorial
7.
8. def binomfdp(n, p, k):
9. c = factorial(n) / (factorial(k) * factorial(n - k))
10. probability = c * (p ** k) * ((1 - p) ** (n - k))
11. return probability
12.
13. def binomfrp(n, p, k):
14. o = binomfdp(n, p, 0)
15. for i in range(k):
16. o += binomfdp(n, p, i + 1)
17. return o
18.
19. n = 10
20. p = 0.5
21. k = n
22.
23. x = np.arange(0, k + 1)
24. binomial_pmf = [binomfdp(n, p, i) for i in x]
25.
26. plt.bar(x, binomial_pmf, color="red")
27.
28. plt.xlabel('Number of Successes')
29. plt.ylabel('Probability')
30. plt.title(f'Binomial Distribution (n={n}, p={p})')
31. plt.show()

8
Distribution loi de poisson :

1. from functools import partial


2. import math
3. import matplotlib.pyplot as plt
4.
5. def poisson_pmf(k, lambda_parameter):
6. return (lambda_parameter**k * math.exp(-lambda_parameter)) / math.factorial(k)
7.
8. l= 5
9.
10. x = [i for i in range(20)]
11.
12. poisson_pmf1 = [poisson_pmf(i, l) for i in x]
13.
14. plt.bar(x, poisson_pmf1, color='red', label=f'Poisson λ={l}')
15. plt.title('Poisson Distribution')
16. plt.xlabel('X')
17. plt.ylabel('Probability')
18. plt.legend()
19. plt.show()

Vous aimerez peut-être aussi