2

我正在尝试创建一种方法,使我拥有的风和需求数组在 1 年内以 15 分钟的间隔在一天中平均。我想将其转化为 365 天中每一天的日平均值。这是我到目前为止所拥有的:

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

2 回答 2

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,因为切片的结尾不包括指定的结尾。

于 2013-08-27T03:27:21.320 回答
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
于 2013-08-27T03:01:49.337 回答