首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检测熊猫的上升和下降趋势?

如何检测熊猫的上升和下降趋势?
EN

Stack Overflow用户
提问于 2019-05-18 05:08:40
回答 2查看 995关注 0票数 5

对于使用下面的代码生成的图,我希望获得通过pandas逻辑生成的信号。

当曲线比上一个局部最小值高(或更多) +3个点时,输出信号应从-4更改为-2。当曲线比上一个局部最大值低(或更少)2个点时,它应该从-2变回-4。

图1显示了由以下代码生成的曲线。图2大致显示了输出信号应该是什么样子。

图1:

图2:

代码:

代码语言:javascript
运行
复制
import matplotlib

matplotlib.use('QT5Agg')
import matplotlib.pyplot as plt
import numpy as np

a = np.arange(5)
b = np.arange(5, -4, -1)
c = np.arange(-4, 7, .5)
d = np.arange(7, 2, -1)
e = np.arange(2, 6, .2)
f = np.arange(6, -3, -1)
g = np.arange(-3, 2, .25)

r1 = np.append(a, b)
r2 = np.append(r1, c)
r3 = np.append(r2, d)
r4 = np.append(r3, e)
r5 = np.append(r4, f)
r6 = np.append(r5, g)

plt.rcParams['font.size'] = 6

fig, ax1 = plt.subplots()
ax1.plot(r6,'g-o',markersize=3)



plt.annotate('start upward', xy=(0,0), textcoords='data',)
plt.annotate('end upward', xy=(3,3), textcoords='data',)

plt.annotate('start downward', xy=(5,5), textcoords='data',)
plt.annotate('end downward', xy=(7,3), textcoords='data',)

plt.annotate('start upward', xy=(14,-4), textcoords='data',)
plt.annotate('end upward', xy=(20,-1), textcoords='data',)

plt.annotate('start downward', xy=(36,7), textcoords='data',)
plt.annotate('end downward', xy=(38,5), textcoords='data',)

plt.annotate('start upward', xy=(41,2), textcoords='data',)
plt.annotate('end upward', xy=(56,5), textcoords='data',)

plt.annotate('start downward', xy=(61,6), textcoords='data',)
plt.annotate('end downward', xy=(63,4), textcoords='data',)

plt.annotate('start upward', xy=(70,-3), textcoords='data',)
plt.annotate('end upward', xy=(82,0), textcoords='data',)

ax1.minorticks_on()
ax1.grid(b=True, which='major', color='g', linestyle='-')
ax1.grid(b=True, which='minor', color='y', linestyle='--')
plt.show()
EN

Stack Overflow用户

回答已采纳

发布于 2019-05-18 08:29:53

我想你想要的是:

代码语言:javascript
运行
复制
s = pd.Series(np.concatenate((a,b,c,d,e,f,g,)))

# is increasing
incr = s.diff().ge(0)

# shifted trend (local minima)
shifted = incr.ne(incr.shift())

# local max
local_max = shifted & (~incr)


# thresholding function
def thresh(x, threshold=3, step=2):
    ret = pd.Series([0]*len(x), index=x.index)
    t = x.min() + threshold
    ret.loc[x.gt(t)] = step
    return ret

signal = s.groupby(local_max.cumsum()).apply(thresh)
signal += s.min()

# draw
fig, ax = plt.subplots(figsize=(10,6))
s.plot(ax=ax)
signal.plot(drawstyle='steps', ax=ax)
plt.show()

输出:

票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56193600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档