使用不同窗口大小的移动平均函数
我遇到了一个问题,不知道该怎么解决这个问题。
我写了一些代码来画图:
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 个回答
1
一个移动平均函数的作用是计算最近的n个数据样本的平均值。你可以在Python中通过把最近的n个样本存储在一个列表里,然后计算它们的平均值来实现这个功能:
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