标签:python arrays for-loop time-series iteration
我有一个随机的时间序列数据,有四列,如:年,月,日,降水.我想计算不同法术长度的干/湿法术.我正在寻找一种更方便的方法来做到这一点,而目前正在做一些丑陋的代码,如下所示:
import numpy as np
data = np.loadtxt('Data Series.txt', usecols=(1,3))
dry = np.zeros(12)
wet = np.zeros(12)
rows,cols = data.shape #reading number of rows and columns into variables
for i in xrange (0,rows):
for m in xrange(0,12):
if data[i,1] == 0 and data[i-1,1] == 0 and data[i-2,1] == 0:
if data[i,0] == m+1:
dry[m] += 1.0
if data[i,1] > 0 and data[i-1,1] > 0 and data[i-2,1] > 0:
if data[i,0] == m+1:
wet[m] += 1.0
print '3 Days Dry Spell\n', dry
print '3 Days Wet Spell\n', wet
现在,如果我想计算4,5,6天法术,那么“如果数据[i,1] == 0和数据[i-1,1] == 0 ……”变成一个巨大的法术.任何人都可以帮助我,以便我可以只给出法术长度而不是这条长丑线吗?
解决方法:
您可能想尝试这样的事情:
# first extract precipitation data for later use
precipitation = [data[i][1] for i in xrange(0, rows)]
# then test the range (i, i+m)
all_dry = all([(data==0) for data in precipitation[i:i+m]])
all_wet = not any([(data==0) for data in precipitation[i:i+m]])
# of course you can also use
all_wet = all([(data>0) for data in precipitation[i:i+m]])
但请注意,此方法在测试相邻日期时会引入冗余计算,因此可能不适合处理大量数据.
编辑:
好的,这一次让我们寻找一种更有效的方法.
# still extract precipitation data for later use first
precipitation = [data[i][1] for i in xrange(0, rows)]
# let's start our calculations by counting the longest consecutive dry days
consecutive_dry = [1 if data == 0 else 0 for data in precipitation]
for i in xrange(1, len(consecutive_dry))
if consecutive_dry[i] == 1:
consecutive_dry[i] += consecutive_dry[i - 1]
# then you will see, if till day i there're m consecutive dry days, then:
consecutive_dry[i] >= m # here is the test
# ...and it would be same for wet day testings.
这显然比上述方法更有效:为了测试具有M个连续范围的总共N天,前一个需要O(N * M)个操作来计算,并且这个操作需要O(N).
再次编辑:
这是原始代码的编辑版本.由于您的代码可以运行,这也应该在您的PC上运行或者运行.
import numpy as np
data = np.loadtxt('Data Series.txt', usecols=(1,3))
dry = np.zeros(12)
wet = np.zeros(12)
rows,cols = data.shape #reading number of rows and columns into variables
# prepare
precipitation = [data[i][1] for i in xrange(0, rows)]
# collecting data for consecutive dry days
consecutive_dry = [1 if data == 0 else 1 for data in precipitation]
for i in xrange(1, len(consecutive_dry))
if consecutive_dry[i] == 1:
consecutive_dry[i] += consecutive_dry[i - 1]
# ...and for wet days
consecutive_wet = [1 if data > 0 else 0 for data in precipitation]
for i in xrange(1, len(consecutive_wet))
if consecutive_wet[i] == 1:
consecutive_wet[i] += consecutive_wet[i - 1]
# set your day range here.
day_range = 3
for i in xrange (0,rows):
if consecutive_dry[i] >= day_range:
month_id = data[i,0]
dry[month_id - 1] += 1
if consecutive_wet[i] >= day_range:
month_id = data[i,0]
wet[month_id - 1] += 1
print '3 Days Dry Spell\n', dry
print '3 Days Wet Spell\n', wet
请试试这个,如果有任何问题请告诉我.
标签:python,arrays,for-loop,time-series,iteration 来源: https://codeday.me/bug/20190831/1775287.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。