Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. from collections import namedtuple
  2. from collections import deque
  3. from collections import Counter
  4. import numpy as np
  5. import random
  6. import itertools
  7.  
  8. Record = namedtuple('Record', 'easy medium nightmare')
  9. len_window = 1000
  10.  
  11. def data_stream():
  12.     random_generator = random.Random(42)
  13.     easy = 0
  14.     for _ in range(10000000):
  15.         easy += random_generator.randint(0, 2)
  16.         #medium = random_generator.randint(0, 256 - 1) # counter
  17.         #nightmare = random_generator.randint(0, 1000000000 - 1) # deque
  18.        
  19.         yield Record(
  20.             easy=easy,
  21.             #medium=medium,
  22.             #nightmare=nightmare
  23.         )
  24.        
  25. def easy_stream():
  26.     for record in data_stream():
  27.         yield record.easy
  28.        
  29. def medium_stream():
  30.     for record in data_stream():
  31.         yield record.medium
  32.        
  33. def nightmare_stream():
  34.     for record in data_stream():
  35.         yield record.nightmare
  36.  
  37.  
  38. def get_tuple_windows_mean_and_disp(stream):
  39.     window = deque()
  40.     mean = 0
  41.     sum_sqrs = 0
  42.     for val in itertools.islice(stream(),0,len_window):
  43.         window.append(val)
  44.         mean += val
  45.         sum_sqrs += val*val
  46.     mean /= len_window
  47.     sum_sqrs /= len_window
  48.     yield (mean, sum_sqrs - mean*mean)
  49.     for next_value in stream():
  50.         mean += (next_value - window[0]) / len_window
  51.         sum_sqrs += (next_value*next_value - window[0]*window[0]) / len_window
  52.         window.popleft()
  53.         window.append(next_value)
  54.         yield (mean, sum_sqrs - mean*mean)
  55.        
  56. def get_tuple_windows_statistics(stream):
  57.     if stream.__name__ == 'easy_stream':
  58.         window = deque(itertools.islice(stream(),0,len_window))
  59.         yield ( window[0], (window[len_window//2] + window[len_window//2 - 1])/2, window[-1])
  60.         for next_value in stream():
  61.             window.popleft()
  62.             window.append(next_value)
  63.             yield (window[0], (window[len_window//2] + window[len_window//2 - 1])/2, window[-1]
  64.  #   if stream.__name__ == 'medium_stream':
  65.  #       window =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement