Python 低于阈值的连续事件
我有长度为324的SPI时间序列,值范围为-3到+3。我想得到3个或更多连续时间步低于阈值-1的地方的索引 我已经彻底地搜索了这个网站和其他地方,但没有取得重大成功,例如,但没有完全按照我的要求去做Python 低于阈值的连续事件,python,pandas,numpy,Python,Pandas,Numpy,我有长度为324的SPI时间序列,值范围为-3到+3。我想得到3个或更多连续时间步低于阈值-1的地方的索引 我已经彻底地搜索了这个网站和其他地方,但没有取得重大成功,例如,但没有完全按照我的要求去做 Assuming this is what I want to do th = -1 # threshold a = [-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4] # my data a[x]<th and a[x+1]<th and a[x
Assuming this is what I want to do
th = -1 # threshold
a = [-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4] # my data
a[x]<th and a[x+1]<th and a[x+2]<th # find 3 or more consecutive
time below threshold
in this case that will be event e1=[-1,-2,-5] and e2=[-3,-3,-1,-2]
next I want to count this
counts = [len(e1),len(e2)] = [3,4]
Sum = [np.sum(e1),np.sum(e2)] = [-8, -9]
假设这是我想要做的
th=-1#阈值
a=[-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4]#我的数据
带有np.logical\u和.reduce的[x],检查低于阈值的连续行。然后通过groupby获得您需要的所有聚合:
import numpy as np
import pandas as pd
def get_grps(s, thresh=-1, Nmin=3):
"""
Nmin : int > 0
Min number of consecutive values below threshold.
"""
m = np.logical_and.reduce([s.shift(-i).le(thresh) for i in range(Nmin)])
if Nmin > 1:
m = pd.Series(m, index=s.index).replace({False: np.NaN}).ffill(limit=Nmin-1).fillna(False)
else:
m = pd.Series(m, index=s.index)
# Form consecutive groups
gps = m.ne(m.shift(1)).cumsum().where(m)
# Return None if no groups, else the aggregations
if gps.isnull().all():
return None
else:
return s.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)
这是一个有注释的一步一步的配方
a = [-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4]
th = -1
a = np.array(a)
# create mask of events; find indices where mask switches
intervals = np.where(np.diff(a<=th, prepend=0, append=0))[0].reshape(-1,2)
# discard short stretches
intervals = intervals[np.subtract(*intervals.T) <= -3]
intervals
# array([[ 3, 6],
# [10, 14]])
# get corresponding data
stretches = np.split(a, intervals.reshape(-1))[1::2]
stretches
# [array([-1, -2, -5]), array([-3, -3, -1, -2])]
# count events
-np.subtract(*intervals.T)
# array([3, 4])
# sum events
np.add.reduceat(a, intervals.reshape(-1))[::2]
# array([-8, -9])
a=[-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4]
th=-1
a=np.数组(a)
#创造事件的面具;查找掩码开关所在的索引
间隔=np.where(np.diff(a),因为您标记了熊猫:
s = pd.Series([-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4])
# thresholding
a = (s<1)
# blocks
b = (a!=a.shift()).cumsum()
# groupby
df = s[a].groupby(b).agg([list,'size','sum'])
df = df[df.size>=3]
请问,我是否可以在这段代码中应用滚动窗口功能:我想在60时间步长的滚动窗口中使用它,我如何修改它以使用3d数组?
s = pd.Series([-3,4,5,-1,-2,-5,1,4,6,9,-3,-3,-1,-2,4,1,4])
# thresholding
a = (s<1)
# blocks
b = (a!=a.shift()).cumsum()
# groupby
df = s[a].groupby(b).agg([list,'size','sum'])
df = df[df.size>=3]
list size sum
3 [-1, -2, -5] 3 -8
5 [-3, -3, -1, -2] 4 -9