Distribution

Intermediate Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Distribution

Intermediate Python

Random Walk

headtailsrw.py

import numpy as np
np.random.seed(123)
tails = [0]
for x in range(10) :
    coin = np.random.randint(0, 2)
    tails.append(tails[x] + coin)
Intermediate Python

100 runs

distribution.py

import numpy as np
np.random.seed(123)
final_tails = []

for x in range(100) : tails = [0] for x in range(10) : coin = np.random.randint(0, 2) tails.append(tails[x] + coin)
final_tails.append(tails[-1])
print(final_tails)
[3, 6, 4, 5, 4, 5, 3, 5, 4, 6, 6, 8, 6, 4, 7, 5, 7, 4, 3, 3, ..., 4]
Intermediate Python

Histogram, 100 runs

distribution.py

import numpy as np

import matplotlib.pyplot as plt
np.random.seed(123) final_tails = [] for x in range(100) : tails = [0] for x in range(10) : coin = np.random.randint(0, 2) tails.append(tails[x] + coin) final_tails.append(tails[-1])
plt.hist(final_tails, bins = 10) plt.show()
Intermediate Python

Histogram, 100 runs

Intermediate Python

Histogram, 1,000 runs

distribution.py

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(123) final_tails = [] for x in range(1000) : # <-- tails = [0] for x in range(10) : coin = np.random.randint(0, 2) tails.append(tails[x] + coin) final_tails.append(tails[-1])
plt.hist(final_tails, bins = 10) plt.show()
Intermediate Python

Histogram, 1,000 runs

Intermediate Python

Histogram, 10,000 runs

distribution.py

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(123) final_tails = [] for x in range(10000) : # <-- tails = [0] for x in range(10) : coin = np.random.randint(0, 2) tails.append(tails[x] + coin) final_tails.append(tails[-1])
plt.hist(final_tails, bins = 10) plt.show()
Intermediate Python

Histogram, 10,000 runs

Intermediate Python

Let's practice!

Intermediate Python

Preparing Video For Download...