在Python中泛化方法

2024-06-26 17:59:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个方法,我有一个数组的风和需求平均通过一天在15分钟的间隔为1年。我想把这变成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

到目前为止,我可以做到这一点,如果我写两次,但这不是好的编程。我想弄清楚如何在两个数据集上使用这个方法。谢谢。你知道吗


Tags: ofthe方法间隔is情况数组array
2条回答
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

您只需要将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

另外,我在两个位置将95替换为96,因为切片的结尾不包括指定的结尾。你知道吗

相关问题 更多 >