Skip to content

Instantly share code, notes, and snippets.

@danqing
Last active August 23, 2017 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danqing/19c0ddc3dcc66ab9978674e53f2a8eb5 to your computer and use it in GitHub Desktop.
Save danqing/19c0ddc3dcc66ab9978674e53f2a8eb5 to your computer and use it in GitHub Desktop.
Machine learning for humans - What is machine learning 02 - www.tinymind.ai
class FlipPredictor(object):
def __init__(self):
self.tries = 0 # Total tries so far.
self.guess = 5 # Current guess.
def predict(self, previous):
"""Predict # heads in the next 10 flips.
previous is # heads in the previous 10 flips. That's what we learn from!
"""
# The first guess is a blind guess. No previous flips yet!
if previous is None:
self.tries = 1
return self.guess
# Modify our guess by computing the weighted average of our existing
# guess and the # heads of the latest flips.
self.guess = (self.guess * self.tries + previous) / (self.tries + 1)
self.tries = self.tries + 1
return self.guess
predictor = FlipPredictor()
predictor.predict(None) # first guess is blind, predicts 5
predictor.predict(8) # 8 heads in last 10, predicts 6.5
predictor.predict(7) # 7 heads in last 10, predicts 6.67
predictor.predict(8) # 8 heads in last 10, predicts 7
predictor.predict(9) # 9 heads in last 10, predicts 7.4
predictor.predict(8) # 8 heads in last 10, predicts 7.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment