Last active
October 17, 2017 11:10
-
-
Save myrfy001/0fc47bac3c450227b8fc989c9d1fa812 to your computer and use it in GitHub Desktop.
迭代器风格的滑动平均数计算程序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author myrfy | |
def moving_average(seq_in, window_size): | |
sum = 0.0 | |
iter_ = iter(seq_in) | |
buf_q = deque(maxlen=window_size) | |
# build the init window | |
for i in islice(iter_, window_size): | |
buf_q.append(i) | |
sum += i | |
yield sum / len(buf_q) | |
for i in iter_: | |
sum -= buf_q.popleft() | |
sum += i | |
buf_q.append(i) | |
yield sum / window_size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment