bts:python:echantillonnage
Ceci est une ancienne révision du document !
Warning: Undefined array key 1 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 172
Warning: Undefined array key 1 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 172
Warning: Undefined array key 1 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 172
Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214
Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214
Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214
Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214
Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214
Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214
Table des matières
Échantillonnage
On part d'une loi de probabilité quelconque. On souhaite observer ce qui se passe quand on prélève des échantillons et qu'on calcule des moyennes.
Loi quelconque
Dans le code ci-dessous, on définit une table DATA qui donne une certaine répartition. Vous voyez qu'il n'y a pas de 9, qu'il y a beaucoup de 12… cela définit la probabilités d'obtenir les différentes valeurs.
# echantillonnage.py
from random import choice
DATA = (1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 10, 10, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 19, 19)
def alea():
"""
renvoie un nombre aléatoire entre 0 et 20 selon la répartition de DATA
"""
return choice(DATA)
Échantillon de taille n
On veut une relève un échantillon de taille n.
def echantillon(n:int):
"""
renvoie un tableau contenant n valeur prise aléatoirement
"""
# à vous
Beaucoup d'échantillons
On va prélever beaucoup d'échantillons d'une certaine taille n, pour chaque échantillon on va calculer la moyenne, puis on va représenter sur histogramme les moyennes trouvées.
import matplotlib.pyplot as plt
import statistics
n = 10 # choix de n
resultats = []
for i in range(1000):
e = echantillon(n)
m = statistics.mean(e)
resultats.append(m)
plt.hist(resultats, density = True)
plt.show()
Vous pouvez changer la valeur de n pour voir l'effet sur le résultat.
bts/python/echantillonnage.1681252289.txt.gz · Dernière modification : de goupillwiki
