【问题标题】:calculating rolling average using python使用python计算滚动平均值
【发布时间】:2013-05-07 00:18:15
【问题描述】:

我刚开始学习python。我正在用它来编写一个脚本来计算盐流入滚动平均值。我有这样的数据

Date    A4260502_Flow   A4261051_Flow   A4260502_EC A4261051_EC
25/02/1970  1304    0   411 0   1304            
26/02/1970  1331    0   391 0   1331            
27/02/1970  0   0   420 411 0           
28/02/1970  0   0   400 391 0           
1/03/1970   0   0   0   420 0           
2/03/1970   1351    1304    405 400 1327.5      
3/03/1970   2819    1331    415 405 2075        
4/03/1970   2816    0   413 0   2816            
5/03/1970   0   1351    0   415 1351            
6/03/1970   0   0   0   0   0           
7/03/1970   0   2819    0   413 2819            
8/03/1970   0   0   0   0   0           
9/03/1970   0   2816    0   412 2816

我的脚本是

inputfilename = "output.csv"
outputfilename = "SI_calculation.csv"

# Open files
infile = open(inputfilename,"r+")
outfile = open(outputfilename,'w')

# Initialise variables  
EC_conversion = 0.000525
rolling_avg = 5
flow_avg_list = []
SI_list = []
SI_ra_list = []
SI_ra1 = []

# Import module
import csv
import numpy                 #L20


table = []
reader = csv.reader(infile)         #read
for row in csv.reader(infile):
    table.append(row)
infile.close()

for r in range(1,len(table)):        
    for c in range(1,len(row)): #l30
        table[r][c] = float(table[r][c])


#Calculating flow average
for r in range(1,len(table)):

    flow1 = table[r][1]
    flow2 = table[r][2]
    if flow1 == 0.0:                 
        flow_avg = flow2            #l40
    elif flow2 == 0.0:
        flow_avg = flow1
    else:
        flow_avg = (flow1+flow2)/2
    flow_avg_list.append(flow_avg)

#Calculating salt inflow
for r in range(1,len(table)):
    s1 = table[r][3]                               
    s2 = table[r][4]        #l50
    if s1 == 0.0 or s2 == 0.0 or flow_avg_list[r-1] == 0.0:
        SI = 0.0
    else:
        SI = EC_conversion*flow_avg_list[r-1]*(s2-s1)
    SI_list.append(SI)
print SI_list    

#Calculating rolling average salt inflow
for r in range(1,len(table)):                 
    if r < 5:       #rolling-avg = 5
        for i in range(0,r+5):      #l60
            S = SI_list[i]
            SI_ra1.append(S)
        SI_ra = numpy.mean(SI_ra1)
        SI_ra_list.append(SI_ra)
    elif r > (len(table) - 4):
        for i in range(r-5,len(table)-1):
            S = SI_list[i]
            SI_ra1.append(S)
        SI_ra = numpy.mean(SI_ra1)
        SI_ra_list.append(SI_ra)    #l70
    else:
        for i in range(r-5,r+5):
            S = SI_list[i]       #Line 73
            SI_ra1.append(S)
        SI_ra = numpy.mean(SI_ra1)
        SI_ra_list.append(SI_ra)
print SI_ra_list

当我运行脚本时,它给了我错误:Line 73: list index out of range. 有谁知道错误可能是什么?抱歉,这是一个很长的脚本。我还不知道如何缩短它。

【问题讨论】:

  • 知道哪一行是第 73 行会有所帮助。
  • 我刚刚用#Line73 标记了第73 行。这是代码的最后一点。

标签: python


【解决方案1】:

在第 65 行,将条件更改为:

elif r > (len(table) - 5):

问题是当您在第 73 行迭代到列表末尾时,您试图获取列表中的下 5 个数据点,但列表中只剩下 4 个数据点,因此您的索引超出了长度列表,因此抛出异常。

【讨论】:

  • 如果您确实解释了为什么要这样做,并且最重要的是,在计算滚动平均值的上下文中这意味着什么,那么该答案可能会很有用。
【解决方案2】:

请丢弃您的代码,并以此问题的答案为基础重新开始: Rolling Average to calculate rainfall intensity

并不是说你的代码不能工作,而是没有必要用 Python 编写 Fortan 代码。我链接到的问题可以更好地利用 Python 功能,如果您解决了这个问题,包括使用 Interpolate 类Linear Interpolation - Python 跟进问题的链接 那么你会为自己节省数小时的挣扎。

没有必要自己犯所有的错误。从模仿大师开始,然后根据您的需要定制他们的技术,几年后,您也将成为 Python 大师。

【讨论】:

  • 我同意这个原则,但我认为基于pandas 的解决方案比任何一个链接的答案都更强大、更容易。
  • 我同意,但这是堆栈溢出,人们来学习如何编写代码。一旦 Amylee 让一些 Python 程序运行起来,那么 NumPy、PANDAS、HDF5 等的奇迹将值得探索。每个人都需要从某个地方开始,但在 2013 年使用 Python 时,该开始应该包括列表解析、with 关键字等。
  • 谢谢大家。我会试一试。这确实需要时间,因为我也是编程语言的新手,而不仅仅是 python。上周才开始。
猜你喜欢
  • 1970-01-01
  • 2021-06-04
  • 2012-12-28
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多