我制作了一些代码,我需要制作一个图表,其中我的数据持续到移动平均值
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))
我得到: 528 526 526 我怎么得到dateTime到526 ???
答案 0 :(得分: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_data
和iphone_data
中删除这些值。