一些样本数据的简单办法是看“相对于X Y的变化率”,被称为衍生物。这对于连续(平滑)函数通常效果更好,因此您可以按照已经建议的方式使用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])
# 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)
你看,当从“上升趋势”(正斜率)的情节过渡到了“向下趋势”(负梯度)的衍生物(dy/dx)从正值变为负值。这个过渡发生在dy/dx = 0,这由绿色虚线表示。对于SciPy的程序,你可以看看:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.derivative.html
http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html
与NumPy的DIFF /梯度也应该工作,并且不需要插值,但是我发现上面的,所以你能明白我的意思。有关微分/微积分的完整数学描述,请看维基百科。
这听起来像你只是想[拟合一阶多项式](http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html),然后看看系数是否定的或积极的。这对于整个数据集是有效的,但是从问题还不清楚你需要什么。 – Greg
道歉我的无知,但多项式你的意思是最适合的线? – godzilla
是的一阶多项式只是一条最适合的直线。请注意,一般而言,最佳配合可适用于任何功能。你能发布一些你可能感兴趣的样本数据吗? – Greg