如何使用峰值和谷值分割数据?

2024-05-29 04:29:39 发布

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

我使用了下面的代码(来自https://github.com/jbn/ZigZag/blob/master/zigzag_demo.ipynb)来查找股票价格的波峰和波谷,这是有效的,但是我喜欢做的是根据这些波峰和波谷来划分时间序列。我将放一张图片来解释我的意思:

from zigzag import peak_valley_pivots
from zigzag import pivots_to_modes
from zigzag import compute_segment_returns

X = dataset_train.iloc[:, 4]
pivots = peak_valley_pivots(X.values , 0.2, -0.2)
ts_pivots = pd.Series(X, index=X.index)
ts_pivots = ts_pivots[pivots != 0]
plt.figure(0)
X.plot()
ts_pivots.plot(style='g-o');
modes = pivots_to_modes(pivots)
pd.Series(X).pct_change().groupby(modes).describe().unstack()

compute_segment_returns(X, pivots)

enter image description here 在上面的pic中,我用peaks/valley手工将图表/数组切片到12个子数组中,但是我要做的是通过代码来实现这一点,并从原始数组中生成这12个子数组。我该怎么做?你知道吗


Tags: to代码fromimportsegment数组returnspd

热门问题