Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #By Tim Sheerman-Chase, 2016
- #CC0 license
- class RunningWeightedAverage(object):
- def __init__(self):
- self.val = 0.0
- self.weightTotal = 0.0
- def update(self, valIn, weight = 1.0):
- scaling = weight / float(self.weightTotal + weight)
- self.val = valIn * scaling + self.val * (1.0 - scaling)
- self.weightTotal += weight
- return self.val
- def get(self):
- return self.val
- def extend(self, vals, weights):
- for v, w in zip(vals, weights):
- self.update(v, w)
- return self.val
- if __name__ == "__main__":
- rwa = RunningWeightedAverage()
- print rwa.extend([5, 6, 2], [0.25, 0.25, 0.5])
- print "correct answer", 3.75
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement