使用具有不同窗口大小的移动平均功能

时间:2014-03-02 23:53:37

标签: python python-2.7

我有一个问题,我不知道如何解决这个问题。

我制作了一些绘制图表的代码:

import csv
import datetime
import matplotlib.pyplot as plt


data = open('iphonevsandroid.csv', 'r')
reader = csv.reader(data, delimiter=',')

iphone_data = []
android_data = []
dateTime = []
stringdates = []

for row in reader:

    first_date_row = row[0]
    first_date = row[0][:-13]

    if row[1] != 'iphone':
        iphone_data.append(row[1])

    if row[1] != 'iphone':
        android_data.append(row[1])

    if row[0] != 'week':
        stringdates.append(row[0][:-13])


for item in stringdates:
    dateTime.append(datetime.datetime.strptime(item, '%Y-%m-%d'))

x = iphone_data 
y = androiddata  

plt.ylabel('Interesse over tid')
plt.plot(dateTime,x)
plt.plot(dateTime,y)
plt.show()

现在我必须回答顶部的问题: 问题:使用具有不同窗口大小的移动平均功能平滑趋势。

我不是专家,所以有人可以告诉我他们对此有何意义?

1 个答案:

答案 0 :(得分:0)

移动平均功能平均最后的 n 数据样本。您可以通过将最后的 n 样本存储在列表中并计算平均值来在Python中实现此目的:

data = range(100)
WINDOW_SIZE = 10
window = []
for i in data:
    window.append(i) # add the current data point into the window
    if len(window) > WINDOW_SIZE:
        window.pop(0) # remove the oldest sample from the window once it reaches the desired size
    avg = sum(window) / float(len(window)) # convert to float for python2.x
    print(i, window, avg)

这是前几行输出。您可以在左侧看到数据点,在中间看到窗口,在右侧看到平均值。请注意,一旦窗口达到10个项目,旧项目开始被丢弃以保持窗口的大小固定。

0 [0] 0.0
1 [0, 1] 0.5
2 [0, 1, 2] 1.0
3 [0, 1, 2, 3] 1.5
4 [0, 1, 2, 3, 4] 2.0
5 [0, 1, 2, 3, 4, 5] 2.5
6 [0, 1, 2, 3, 4, 5, 6] 3.0
7 [0, 1, 2, 3, 4, 5, 6, 7] 3.5
8 [0, 1, 2, 3, 4, 5, 6, 7, 8] 4.0
9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 4.5
10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5.5
11 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 6.5
12 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 7.5
13 [4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 8.5
14 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] 9.5
15 [6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 10.5
16 [7, 8, 9, 10, 11, 12, 13, 14, 15, 16] 11.5
17 [8, 9, 10, 11, 12, 13, 14, 15, 16, 17] 12.5
18 [9, 10, 11, 12, 13, 14, 15, 16, 17, 18] 13.5
19 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 14.5
20 [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 15.5
21 [12, 13, 14, 15, 16, 17, 18, 19, 20, 21] 16.5
22 [13, 14, 15, 16, 17, 18, 19, 20, 21, 22] 17.5