Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- from collections import deque
- from collections import Counter
- import numpy as np
- import random
- import itertools
- Record = namedtuple('Record', 'easy medium nightmare')
- len_window = 1000
- def data_stream():
- random_generator = random.Random(42)
- easy = 0
- for _ in range(10000000):
- easy += random_generator.randint(0, 2)
- #medium = random_generator.randint(0, 256 - 1) # counter
- #nightmare = random_generator.randint(0, 1000000000 - 1) # deque
- yield Record(
- easy=easy,
- #medium=medium,
- #nightmare=nightmare
- )
- def easy_stream():
- for record in data_stream():
- yield record.easy
- def medium_stream():
- for record in data_stream():
- yield record.medium
- def nightmare_stream():
- for record in data_stream():
- yield record.nightmare
- def get_tuple_windows_mean_and_disp(stream):
- window = deque()
- mean = 0
- sum_sqrs = 0
- for val in itertools.islice(stream(),0,len_window):
- window.append(val)
- mean += val
- sum_sqrs += val*val
- mean /= len_window
- sum_sqrs /= len_window
- yield (mean, sum_sqrs - mean*mean)
- for next_value in stream():
- mean += (next_value - window[0]) / len_window
- sum_sqrs += (next_value*next_value - window[0]*window[0]) / len_window
- window.popleft()
- window.append(next_value)
- yield (mean, sum_sqrs - mean*mean)
- def get_tuple_windows_statistics(stream):
- if stream.__name__ == 'easy_stream':
- window = deque(itertools.islice(stream(),0,len_window))
- yield ( window[0], (window[len_window//2] + window[len_window//2 - 1])/2, window[-1])
- for next_value in stream():
- window.popleft()
- window.append(next_value)
- yield (window[0], (window[len_window//2] + window[len_window//2 - 1])/2, window[-1]
- # if stream.__name__ == 'medium_stream':
- # window =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement