Python Tutorial : Let’s flip a coin in Python

Pubblicato il: 12 marzo 2020
sul canale di: DataCamp
5,555
25

Want to learn more? Take the full course at https://learn.datacamp.com/courses/fo... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---

Hi, I'm Alexander Ramirez, the CEO at Synergy Vision, which focuses on financial data science.

This course is intended for people with basic knowledge about Python and familiarity with statistics.

At the end of the course, you'll have an understanding of basic probability concepts like random variables, calculations, probability distributions, and important results like the law of large numbers.

We'll illustrate these concepts using simulations with Python.

Probability is the foundation of many of the methods and models in data science.

Statistical inference methods use data to understand the underlying processes behind the data.

With probability, we can study the regularities that arise in random phenomena.

Let's try to gain some intuition about probability by starting with the classic random experiment, a coin flip.

We'll start simulating with Python and learn to calculate probabilities and work with probability distributions.

If you flip a coin there are two possible outcomes: heads or tails. This random experiment is called a Bernoulli trial, after Jacob Bernoulli.

A Bernoulli trial is one where the possible outcomes are binary: they can be modeled as success or failure, yes or no, or on or off.

In this case, the experiment is the flip of a coin; the possible outcomes are success (getting heads) or failure (getting tails). Each outcome is called an event.

You assign probabilities to events. With a fair coin, you have a 50% chance of getting heads and a 50% chance of getting tails for each event.


To simulate the coin flips, we will use the bernoulli object from the Python library scipy dot stats.

We can call bernoulli dot rvs, specifying two arguments, to generate random variates. The first argument is p, the probability of success, which in this case is 0.5; the second is size, which is the number of coin flips, in this case 1.

The result of the first call was 0, or tails.

After executing the call a second time we got 1, or heads.

For 10 coin flips, size equals 10.

We can use sum to know how many heads we got.

The first time, we got 5 heads and 5 tails.

After another draw, we got 2 heads and 8 tails. So, we can appreciate the randomness.

A sequence of independent Bernoulli trials follows the binomial distribution.

So, instead of using the bernoulli object and adding the outcomes, we can use the binomial distribution and the binom object.

We'll use three arguments: n for the number of coin flips, p for the probability of success, and size for the number of draws of the same experiment.

On the first execution, we got 7 heads out of 10 flips. Let's try 10 more times.

As we can see, 5 is the result that repeats most often for a fair coin.

We can also change the probability of getting heads to 0.3, and see how that affects the results.

As you may notice already, we can observe different outcomes using biased coins.

We use random number generators to simulate the outcome of random experiments.

If you run the same command with the same random seed, you will always get the same result.

In Python, we need to set a seed for the generator to produce similar outcomes in each experiment. Then we can check if the results are what we expected.

We have two options to configure the generator: using the random_state parameter of the rvs function or using numpy dot random dot seed.

For convenience, we will mostly use the numpy method throughout this course.

We can do some more experiments like this to get a sense about the outcomes that are most likely. So, let's practice flipping coins in Python!

#DataCamp #PythonTutorial #FoundationsofProbabilityinPython


In questa pagina del sito puoi guardare il video online Python Tutorial : Let’s flip a coin in Python della durata di ore minuti seconda in buona qualità , che l'utente ha caricato DataCamp 12 marzo 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 5,555 volte e gli è piaciuto 25 spettatori. Buona visione!