0% found this document useful (0 votes)
145 views

Intermediate Python For Data Science: Random Numbers

This document discusses using Python and NumPy to generate random numbers and simulate random processes. It introduces pseudo-random number generators that can generate random integers or floats with a seed to make the numbers reproducible. Examples are given to simulate coin tosses and random walks by generating random integers and summing the results. The document explains how plotting the distribution of results from many simulations can provide insight into the probabilities of different outcomes.

Uploaded by

Pri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Intermediate Python For Data Science: Random Numbers

This document discusses using Python and NumPy to generate random numbers and simulate random processes. It introduces pseudo-random number generators that can generate random integers or floats with a seed to make the numbers reproducible. Examples are given to simulate coin tosses and random walks by generating random integers and summing the results. The document explains how plotting the distribution of results from many simulations can provide insight into the probabilities of different outcomes.

Uploaded by

Pri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

INTERMEDIATE PYTHON FOR DATA SCIENCE

Random Numbers
Intermediate Python for Data Science

100 x
-1

+1

+1 +2 +3 +4 +5 +6
Can’t go below step 0
0.1 % chance of falling down the stairs
Bet: you’ll reach step 60
Intermediate Python for Data Science

How to solve?
● Analytical
● Simulate the process
● Hacker statistics!
Intermediate Python for Data Science

Random Generators
In [1]: import numpy as np

In [2]: np.random.rand() Pseudo-random numbers


Out[2]: 0.9535543896720104 Mathematical formula
Starting from a seed
In [3]: np.random.seed(123)

In [4]: np.random.rand()
Out[4]: 0.6964691855978616
In [5]: np.random.rand()
Out[5]: 0.28613933495037946 Same seed: same random numbers!
Ensures "reproducibility"
In [6]: np.random.seed(123)

In [7]: np.random.rand()
Out[7]: 0.696469185597861
In [8]: np.random.rand()
Out[8]: 0.28613933495037946
Intermediate Python for Data Science

Coin Toss
! game.py

import numpy as np
np.random.seed(123)
coin = np.random.randint(0,2) Randomly generate 0 or 1
print(coin)

Output:
0
Intermediate Python for Data Science

Coin Toss
! game.py

import numpy as np
np.random.seed(123)
coin = np.random.randint(0,2)
print(coin)
if coin == 0:
print("heads")
else:
print("tails")

Output:
0
heads
INTERMEDIATE PYTHON FOR DATA SCIENCE

Let’s practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE

Random Walk
Intermediate Python for Data Science

Random Step
-1

+1

+1 +2 +3 +4 +5 +6
Intermediate Python for Data Science

Random Walk
Known in Science
100 x
-1 • Path of molecules
• Gambler’s financial status
+1

+1 +2 +3 +4 +5 +6
Intermediate Python for Data Science

Heads or Tails
! headtails.py

import numpy as np 0: heads


np.random.seed(123) 1: tails
outcomes = []
for x in range(10) :
coin = np.random.randint(0, 2)
if coin == 0 :
outcomes.append("heads")
else :
outcomes.append("tails")
print(outcomes)

Output:
['heads', 'tails', 'heads', 'heads', 'heads',
'heads', 'heads', 'tails', 'tails', 'heads']
Intermediate Python for Data Science

Heads or Tails: 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)

print(tails)

Output:
[0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]
Intermediate Python for Data Science

Step to Walk
outcomes

['heads', 'tails', 'heads', 'heads', 'heads',


'heads', 'heads', 'tails', 'tails', 'heads']

tails

Output:
[0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]
INTERMEDIATE PYTHON FOR DATA SCIENCE

Let’s practice!
INTERMEDIATE PYTHON FOR DATA SCIENCE

Distribution
Intermediate Python for Data Science

100 x Each random walk has an end point


-1
Simulate 10,000 times: 10,000 end points
Distribution!
+1
Calculate chances!

+1 +2 +3 +4 +5 +6
Intermediate Python for Data Science

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 for Data Science

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)

Output:
[3, 6, 4, 5, 4, 5, 3, 5, 4, 6, 6, 8, 6, 4, 7, 5, 7,
4, 3, 3, 4, 5, 8, 5, 6, 5, 7, 6, 4, 5, 8, 5, 8, 4,
6, 6, 3, 4, 5, 4, 7, 8, 9, 4, 3, 4, 5, 6, 4, 2, 6,
6, 5, 7, 5, 4, 5, 5, 6, 7, 6, 6, 6, 3, ..., 7]
Intermediate Python for Data Science

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 for Data Science

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 for Data Science

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 FOR DATA SCIENCE

Let’s practice!

You might also like