2013-08-27 61 views
2

我試圖創建一個數組,我有風和需求的數組平均每隔15分鐘一天的時間間隔。我想在365天中的每一天將其變成日常平均值。這是我到目前爲止有:推廣Python中的方法

dailyAvg = [] # this creates the initial empty array for method below 
def createDailyAvg(p): # The method 
    i = 0 
    while i < 35140: # I have 35140 data points in my array 
     dailyAvg.append(np.mean(p[i:i+95])) #Creates the avg of the 96, 15 minute avg 
     i += 95 
    return dailyAvg 

dailyAvgWind = createDailyAvg(Wind) # Wind is the array of 15 minute avg's. 
dailyAvgDemand = createDailyAvg(M) # M is the array of demand avg's 

到目前爲止,我可以,如果我寫這兩次完成這件事,但是這不是良好的編程。我想弄清楚如何在兩個數據集上使用這一個方法。謝謝。

回答

4

您只需將dailyAvg作爲本地函數。通過這種方式,將每個函數執行時間初始化爲空列表(我敢打賭,問題是該函數的結果越做越大,生長,追加新的平均值,但不刪除以前的)

def createDailyAvg(p): # The method 
    dailyAvg = [] # this creates the initial empty array for this method below 
    i = 0 
    while i < 35140: # I have 35140 data points in my array 
     dailyAvg.append(np.mean(p[i:i+96])) #Creates the avg of the 96, 15 minute avg 
     i += 96 
    return dailyAvg 

dailyAvgWind = createDailyAvg(Wind) # Wind is the array of 15 minute avg's. 
dailyAvgDemand = createDailyAvg(M) # M is the array of demand avg's 

另外,我在兩個地方用96取代了95,因爲一個切片的結尾不包括指定的結尾。

1
def createDailyAvg(w,m): # The method 
    dailyAvg = [[],[]] # this creates the initial empty array for method below 
    i = 0 
    while i < 35140: # I have 35140 data points in my array 
     dailyAvg[0].append(np.mean(w[i:i+95])) #Creates the avg of the 96, 15 minute avg 
     dailyAvg[1].append(np.mean(m[i:i+95])) 
     i += 95 
    return dailyAvg 
dailyAvg = createDailyAvg(Wind,M) 
dailyAvgWind = dailyAvg[0] # Wind is the array of 15 minute avg's. 
dailyAvgDemand = dailyAvg[1] # M is the array of demand avg's