2014-03-02 71 views
1

我有一個問題,我不知道如何解決這個問題。使用不同窗口大小的移動平均函數

我已經取得了一些代碼誰繪製圖表:

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() 

現在我來回答這個問題在頂部: 問題:平滑使用不同的窗口大小的移動平均函數的趨勢。

我不是專家,所以有人請告訴我他們對此有何意義?

回答

0

A 移動平均函數平均值最後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 
+0

好了,我不明白你在做什麼.. 我必須使iphone_data和android_data的一些avage?我該怎麼做? – Raaydk

+0

@Raaydk是的,您需要將移動平均函數(如我發佈的函數)應用於您的數據集(iphone_data和android_data)。你有什麼特別是在我的代碼中理解有困難? –

相關問題