识别图形上升趋势或下降趋势 [英] identify graph uptrend or downtrend

查看:175
本文介绍了识别图形上升趋势或下降趋势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取数据并使用python(标准线图)将它们绘制到图上。有人可以建议我如何分类图中的某些点是以编程方式呈上升趋势还是下跌趋势?这将是实现这一目标的最佳方式?这确实是一个已解决的问题,并且有一个数学方程来确定这一点吗?

这里有一些示例数据带有一些上涨趋势和下降趋势

  x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24,25,26,27,28,29,30] 
y = [2,5,7,9,10,13,16,18, 21,22,21,20,19,18,17,14,10,9,7,5,7,9,10,12,13,15,16,17,22,27]

提前致谢

解决方案

一个简单的方法是查看y关于x的变化率,即导数。这对于连续(平滑)函数通常效果更好,因此您可以按照已经建议的方式使用n阶多项式对数据进行插值,从而实现数据。一个简单的实现看起来像这样:

  import numpy as np 
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.misc import derivative

x = np.array([1,2,3,4,5,6,7,8,9,10 ,11,12,13,14,15,\\
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30])
y = np.array([2,5,7,9,10,13,16,18,21,22,21,20,19,18,\
17,14,10,9, 7,5,7,9,10,12,13,15,16,17,22,27])

#x和y的简单插值
f = interp1d(x,y )
x_fake = np.arange(1.1,30,0.1)

相对于x的导数b
df_dx =导数(f,x_fake,dx = 1e-6 )

#Plot
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.errorbar(x,y,fmt =o,color =blue,label ='Input data')
ax1.errorbar(x_fake,f(x_fake),label =Interpolated data,lw = 2)
ax1.set_xlabel(x)
ax1.set_ylabel(y)

ax2.errorbar(x_fake,df_dx,lw = 2)
ax2.errorbar(x_fake,np.array([0 for x_fake]),ls = - ,lw = 2)
ax2.set_xlabel(x)
ax2.set_ylabel(dy / dx)

leg = ax1.legend(loc = 2,numpoints = 1,scatterpoints = 1)
leg.draw_frame(False)



http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.derivative.html



http://docs.scipy.org/doc/ scipy / reference / tutorial / interpolate.html



NumPy的差异/渐变也可以工作,并且不需要插值,但是我展示了上面所以你可以明白了。有关微分/微积分的完整数学描述,请查看维基百科。


I am attempting to read in data and plot them on to a graph using python (standard line graph). Can someone please advise on how I can classify whether certain points in a graph are uptrends or downtrends programmatically? Which would be the most optimal way to achieve this? Surely this is a solved problem and a mathematical equation exists to identify this?

here is some sample data with some up trends and downtrends

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
y = [2,5,7,9,10,13,16,18,21,22,21,20,19,18,17,14,10,9,7,5,7,9,10,12,13,15,16,17,22,27]

thanks in advance

解决方案

A simple way would be to look at the 'rate in change of y with respect to x', known as the derivative. This usually works better with continuous (smooth) functions, and so you could implement it with your data by interpolating your data with an n-th order polynomial as already suggested. A simple implementation would look something like this:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.misc import derivative

x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,\
              16,17,18,19,20,21,22,23,24,25,26,27,28,29,30])
y = np.array([2,5,7,9,10,13,16,18,21,22,21,20,19,18,\
              17,14,10,9,7,5,7,9,10,12,13,15,16,17,22,27])

# Simple interpolation of x and y    
f = interp1d(x, y)
x_fake = np.arange(1.1, 30, 0.1)

# derivative of y with respect to x
df_dx = derivative(f, x_fake, dx=1e-6)

# Plot
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.errorbar(x, y, fmt="o", color="blue", label='Input data')
ax1.errorbar(x_fake, f(x_fake), label="Interpolated data", lw=2)
ax1.set_xlabel("x")
ax1.set_ylabel("y")

ax2.errorbar(x_fake, df_dx, lw=2)
ax2.errorbar(x_fake, np.array([0 for i in x_fake]), ls="--", lw=2)
ax2.set_xlabel("x")
ax2.set_ylabel("dy/dx")

leg = ax1.legend(loc=2, numpoints=1,scatterpoints=1)
leg.draw_frame(False)

You see that when the plot transitions from an 'upwards trend' (positive gradient) to a 'downwards trend' (negative gradient) the derivative (dy/dx) goes from positive to negative. The transition of this happens at dy/dx = 0, which is shown by the green dashed line. For the scipy routines you can look at:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.derivative.html

http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

NumPy's diff/gradient should also work, and not require the interpolation, but I showed the above so you could get the idea. For a complete mathemetical description of differentiation/calculus, look at wikipedia.

这篇关于识别图形上升趋势或下降趋势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆