Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Created January 12, 2020 15:38
Show Gist options
  • Save jamescalam/dbbe781e4b47c07155a9a5605a00bd8b to your computer and use it in GitHub Desktop.
Save jamescalam/dbbe781e4b47c07155a9a5605a00bd8b to your computer and use it in GitHub Desktop.
Example code snippet for Naive Bayes fundamentals article
import numpy as np
def choose(): # here we setup our fruit picker script
if np.random.randint(0, 10) < 4:
# we have chosen bag A (40% probability)
if np.random.randint(0, 10) < 4:
# we have chosen an apple from bag A
return ('A', 'Apple')
else:
# we have chosen an orange from bag A
return ('A', 'Orange')
else:
# we have chosen bag B (60% probability)
if np.random.randint(0, 10) < 7:
# we have chosen an apple from bag B
return ('B', 'Apple')
else:
# we have chosen an orange from bag B
return ('B', 'Orange')
# now lets count our apples
outcome = [] # initialise our list of outcomes
for _ in range(10000):
outcome.append( # calculate number of times we get an apple from 100 turns
sum([1 if choose()[1] == 'Apple' else 0 for _ in range(100)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment