issue158:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue158:python [2020/07/02 07:42] – d52fr | issue158:python [2020/07/03 14:54] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
• The Law of Truly Large Numbers states that "with a large enough sample of data, many odd ' | • The Law of Truly Large Numbers states that "with a large enough sample of data, many odd ' | ||
- | Pour ce mois-ci, j'avais décidé de continuer notre présentation de la gestion des données. Cette fois, nous regarderons la « Loi » des très grands nombres. | + | Pour ce mois-ci, j'ai décidé de continuer notre présentation de la gestion des données. Cette fois, nous regarderons la « Loi » des Vraiment Grands Nombres. |
Pourquoi ai-je décidé de mettre Loi entre guillemets ? Parce que ce n'est pas réellement une loi : | Pourquoi ai-je décidé de mettre Loi entre guillemets ? Parce que ce n'est pas réellement une loi : | ||
- | ••C' | + | ••Il EXISTE |
- | ••Le Loi des Très Grands Nombres soutient | + | ••La Loi des Vraiment |
**This month, we will experiment to see if we can experience either of these two " | **This month, we will experiment to see if we can experience either of these two " | ||
Ligne 19: | Ligne 19: | ||
Probability theory says, basically, that if you have two outcomes that are equally likely to occur (the heads of a coin in this case), there is an equally likely chance that either will occur, or in the case of a coin toss, 50% that it will end up with heads and 50% that it will end up with tails.** | Probability theory says, basically, that if you have two outcomes that are equally likely to occur (the heads of a coin in this case), there is an equally likely chance that either will occur, or in the case of a coin toss, 50% that it will end up with heads and 50% that it will end up with tails.** | ||
- | Ce mois-ci, nous ferons des expériences pour voir si nous pouvons vérifier ' | + | Ce mois-ci, nous ferons des expériences pour voir si nous pouvons vérifier |
- | D' | + | D' |
- | Un nombre aléatoire est un nombre qui est indépendant | + | Un nombre aléatoire est un nombre qui est indépendant, sans aucune corrélation avec aucune suite de nombres. |
- | La théorie des probabilités dit, en gros, que, si vous avez deux résultats qui ont des chances égales d' | + | La théorie des probabilités dit, en gros, que, si vous avez deux résultats qui ont des chances égales d' |
**Michael Crichton' | **Michael Crichton' | ||
Ligne 31: | Ligne 31: | ||
Now, let's create a VERY simple Python program to check this out. We will use the numpy library for the random number generator, rather than the built-in Python random number generator. While both are pretty much the same thing, the numpy library has some additional options that make it a better choice for future work. It's not good enough for serious cryptography use, but for what we need, it's fine. Because of the f-string formatting, you will need to use Python 3.7 or greater.** | Now, let's create a VERY simple Python program to check this out. We will use the numpy library for the random number generator, rather than the built-in Python random number generator. While both are pretty much the same thing, the numpy library has some additional options that make it a better choice for future work. It's not good enough for serious cryptography use, but for what we need, it's fine. Because of the f-string formatting, you will need to use Python 3.7 or greater.** | ||
- | Jurrasic Park de Michael Crichton (que ce soit le livre ou e film, mais, à mon avis, le film est plus amusant) contient une bonne (mais simplifiée) présentation de la Théorie du Chaos où Ian Malcolm (joué par Jeff Goldblum) décrit la direction que prendra une goutte d'eau glissant le long de la main du docteur Elie Sattler (joué par Laura Dern).On peut dire la même chose au sujet d'une pièce tombant sur le sol ou dans le creux de votre main. L'un ou l' | + | Jurrasic Park de Michael Crichton (que ce soit le livre ou le film, mais, à mon avis, le film est plus amusant) contient une bonne (mais simplifiée) présentation de la Théorie du Chaos où Ian Malcolm (joué par Jeff Goldblum) décrit la direction que prendra une goutte d'eau glissant le long de la main du docteur Elie Sattler (joué par Laura Dern). On peut dire la même chose au sujet d'une pièce tombant sur le sol ou dans le creux de votre main. L'une ou l' |
- | Maintenant créons un programme | + | Maintenant créons un programme |
**from numpy.random import seed | **from numpy.random import seed | ||
Ligne 47: | Ligne 47: | ||
from numpy.random import randint | from numpy.random import randint | ||
- | Bien sûr, nous commençons par les imports. Dans la ligne de code suivante, nous positionnons la valeur semence du générateur aléatoire à la valeur un. Si vous faites cela, vous aurez les mêmes valeurs que moi. Pour être indpendant | + | Bien sûr, nous commençons par les imports. Dans la ligne de code suivante, nous positionnons la valeur |
- | Maintenant, nous lançons la boucle | + | Maintenant, nous lançons la boucle dix fois et générons |
**Now step through the list of returned numbers and count the number of zeros and ones. | **Now step through the list of returned numbers and count the number of zeros and ones. | ||
Ligne 66: | Ligne 66: | ||
It’s not what you would expect to be. You would expect a 50% number of Heads each time. Take a coin and try it. You will find a similar result. It won’t be 50% each time. Remember the Chaos Theory?** | It’s not what you would expect to be. You would expect a 50% number of Heads each time. Take a coin and try it. You will find a similar result. It won’t be 50% each time. Remember the Chaos Theory?** | ||
- | Maintenant, passons à la liste des nombres retournés et comptons le nombre de zéros | + | Maintenant, passons à la liste des nombres retournés et comptons le nombre de 0 et de 1. |
- | appelez | + | Appelez |
$ python cointoss.py | $ python cointoss.py | ||
Ligne 94: | Ligne 94: | ||
Percentage of Heads: 49.943%** | Percentage of Heads: 49.943%** | ||
- | Maintenant, | + | Il s' |
- | Je vais réduire la sortie (montrée ci-dessous) pour économiser la place, mais voici ce que vous pouvez voir... | + | Je vais réduire la sortie (montrée ci-dessous) pour économiser la place, mais voici ce que vous pouvez voir : |
- | Cette fois-ci les rélsutats | + | Cette fois-ci les résultats |
[1 1 0 ... 0 0 0] | [1 1 0 ... 0 0 0] | ||
Ligne 111: | Ligne 111: | ||
But what about the “Law of Truly Large Numbers”? One of the examples that is often used to explain this would be (http:// | But what about the “Law of Truly Large Numbers”? One of the examples that is often used to explain this would be (http:// | ||
“In July 1975, a taxi in Hamilton, Bermuda knocked Erskine Lawrence Ebbin from his moped, killing him. The year before, his brother Neville Ebbin had been killed by the same driver driving the same taxi and carrying the same passenger while riding the same moped on the same street.”** | “In July 1975, a taxi in Hamilton, Bermuda knocked Erskine Lawrence Ebbin from his moped, killing him. The year before, his brother Neville Ebbin had been killed by the same driver driving the same taxi and carrying the same passenger while riding the same moped on the same street.”** | ||
+ | |||
+ | Maintenant, nous sommes très près du résultat que nous attendons, suffisamment proches pour dire que, oui, nous avons presque obtenu une distribution à 50 %. En plus, nous avons maintenant vu la Loi des Grands Nombres devenir réalité. | ||
+ | |||
+ | Mais alors, la « Loi des Vraiment Grands Nombres » ? Un des exemples qui sont souvent utilisés pour l' | ||
**In another example, | **In another example, | ||
Ligne 119: | Ligne 123: | ||
# seed random number generator | # seed random number generator | ||
seed(1)** | seed(1)** | ||
+ | |||
+ | Un autre exemple : « Lors d'un match de foot avec 50 000 supporters, la plupart des supporters partagent probablement leur date de naissance avec 135 autres dans l' | ||
+ | |||
+ | from numpy.random import seed | ||
+ | from numpy.random import randint | ||
+ | import datetime | ||
+ | # seed random number generator | ||
+ | seed(1) | ||
**Again, we start off with our imports (we added datetime for this example) and set the seed value. Next we set the number of random numbers in our list to be 50000 and create an empty list. | **Again, we start off with our imports (we added datetime for this example) and set the seed value. Next we set the number of random numbers in our list to be 50000 and create an empty list. | ||
Ligne 128: | Ligne 140: | ||
Finally, we create a date (I picked my son’s birthday) to see if it is in the list and print the number of times it occurred, if in fact it did.** | Finally, we create a date (I picked my son’s birthday) to see if it is in the list and print the number of times it occurred, if in fact it did.** | ||
+ | |||
+ | À nouveau, nous débutons par les imports (nous avons ajouté dateline dans cet exemple) et réglons la valeur de la semence. Ensuite, nous fixons à 50 000 le chiffre des nombres aléatoires de notre liste et créons une liste vide : | ||
+ | |||
+ | todo = 50000 | ||
+ | dates = [] | ||
+ | |||
+ | Nous bouclons alors dans une série de déclarations qui récupèrent les dates valides aléatoirement. (J' | ||
+ | |||
+ | Enfin, nous créons une date (j'ai pris la date de naissance de mon fils) pour voir si elle est dans la liste et imprimer le nombre de fois où je la trouve, si elle y est, en fait. | ||
**datetocheck = datetime.date(1986, | **datetocheck = datetime.date(1986, | ||
Ligne 139: | Ligne 160: | ||
We could even modify the code to do this a number of times, keep track of the results and at the end, provide an average of the occurrences. I named this “birthdays2.py”** | We could even modify the code to do this a number of times, keep track of the results and at the end, provide an average of the occurrences. I named this “birthdays2.py”** | ||
+ | |||
+ | datetocheck = datetime.date(1986, | ||
+ | |||
+ | print(f' | ||
+ | |||
+ | Vous ne devriez pas être surpris qu'il y ait quelques occurrences. | ||
+ | |||
+ | $python birthdays.py | ||
+ | Found 3 occurrences # 3 occurrences trouvées | ||
+ | |||
+ | Vous pouvez même modifier le code pour le faire un certain nombre de fois, en gardant une trace des résultats et, à la fin, réaliser une moyenne des occurrences. Je l'ai appelé « birthdays2.py ». | ||
**Here’s the result (shortened of course): | **Here’s the result (shortened of course): | ||
Ligne 150: | Ligne 182: | ||
I hope that this has given you an appreciation of Large Numbers and Truly Large Numbers and random numbers in general.** | I hope that this has given you an appreciation of Large Numbers and Truly Large Numbers and random numbers in general.** | ||
+ | |||
+ | Voici le résultat (abrégé, bien sûr) : | ||
+ | |||
+ | $ python birthdays2.py | ||
+ | Found 4 occurrences | ||
+ | ... | ||
+ | Found 4 occurrences | ||
+ | Results: [4, 3, 5, 5, 6, 3, 1, 3, 3, 4, 0, 2, 1, 0, 5, 2, 3, 3, 3, 3, 4, 3, 3, 6, 5, 3, 3, 1, 3, 2, 4, 4, 2, 4, 2, 2, 2, 4, 0, 1] | ||
+ | Average is 2.96 # La moyenne est 2.96 | ||
**I’ve put the code files up on PasteBin: | **I’ve put the code files up on PasteBin: | ||
Ligne 160: | Ligne 201: | ||
Until next time; stay safe, healthy, positive and creative!** | Until next time; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | J'ai mis les fichiers de code sur PasteBin : | ||
+ | Cointoss.py | ||
+ | https:// | ||
+ | Birthdays.py | ||
+ | https:// | ||
+ | Birthdays2.py | ||
+ | https:// | ||
+ | |||
+ | Jusqu' | ||
issue158/python.txt · Dernière modification : 2020/07/03 14:54 de andre_domenech