分佈

Python 中級

Hugo Bowne-Anderson

Data Scientist at DataCamp

分佈

Python 中級

隨機漫步

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)
Python 中級

執行 100 次

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]
Python 中級

直方圖,100 次

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()
Python 中級

直方圖,100 次

Python 中級

直方圖,1,000 次

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()
Python 中級

直方圖,1,000 次

Python 中級

直方圖,10,000 次

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()
Python 中級

直方圖,10,000 次

Python 中級

一起來練習吧!

Python 中級

Preparing Video For Download...