2014-03-03 292 views
0

我做了一些代碼,我需要做在我的數據persed移動平均蟒蛇繪圖均線蟒蛇錯誤

import numpy as np 
import csv 
import datetime 
import matplotlib.pyplot as plt 

#Open Data/File 
data = open('iphonevsandroid.csv', 'r') 
reader = csv.reader(data, delimiter=',') 

#Define lists 
iphone_data = [] 
android_data = [] 
dateTime = [] 
stringdates = [] 
#iphone_data_average = [] 
#android_data_average = [] 


for row in reader: 

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

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

    if row[2] != 'android': 
     android_data.append(int(row[2])) 

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

for item in stringdates: 

    dateTime.append(datetime.datetime.strptime(item, '%Y-%m-%d'))   

def movingaverage(values,window): 
    weigths = np.repeat(1.0, window)/window 
    #including valid will REQUIRE there to be enough datapoints. 
    #for example, if you take out valid, it will start @ point one, 
    #not having any prior points, so itll be 1+0+0 = 1 /3 = .3333 
    smas = np.convolve(values, weigths, 'valid') 
    return smas # as a numpy array 

movingaverage(iphone_data,3) 
movingaverage(android_data,3) 

plt.ylabel('Indsæt y label') 
plt.xlabel('Indsæt x label') 

plt.plot(dateTime,movingaverage(iphone_data,3)+2) 
plt.plot(dateTime,movingaverage(android_data,3)+2) 
plt.show() 

我的問題是,我得到這個錯誤一個情節:ValueError異常:x和y必須具有相同的第一維度。

我知道是因爲價值觀的LEN它,

如果我打印的LEN:

print len(dateTime) 
print len(movingaverage(iphone_data,3)) 
print len(movingaverage(android_data,3)) 

我得到:我如何獲得日期時間526 ???

回答

0
smas = np.convolve(values, weigths, 'valid') 

應該

smas = np.convolve(values, weigths, 'same') 

,如果你不想要的邊界值,那麼你就會有自己對它們刪除,這是奇怪的窗口長度:

smas = np.convolve(values, weigths, 'valid')[(window-1)/2:-(window-1)/2] 

請注意,您還必須從android_dataiphone_data中刪除這些值。

+0

非常感謝..現在作品:D – Raaydk