查找函数开始和结束的位置

时间:2016-04-07 11:22:52

标签: python points derivative

我有数据描述了这样的系列:

enter image description here

我有兴趣计算强减少开始和结束的点。像那样:

enter image description here

所以我计算了函数的二阶导数,得到负最小值(开始递减)和正最大值时的积分。

但我在大多数情况下获得的内容与此类似:

enter image description here

我哪里错了?

这里有我的python代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import InterpolatedUnivariateSpline

# set x and y
y = data_series
x = range(len(data_series)) 

# interpolation
f =  InterpolatedUnivariateSpline(x, y) 

# gen function points
new_x = np.linspace(min(x), max(x), num=1000, endpoint=True)
new_y = f(new_x)

# calculate second derivative
y_df1 = np.insert(np.diff(y), 0, 0)
y_df2 = np.insert(np.diff(y_df1), 0, 0) 

# points where the decrease starts and ends
# (where the second derivative is minimum and maximum)
x_dec = np.where(y_df2 == min(y_df2))[0][0]
x_inc = np.where(y_df2 == max(y_df2))[0][0]

# plot
plt.plot(new_x, new_y, 'b', lw=3, alpha=0.7)
plt.plot(x_dec, f(x_dec), 'ro', ms= 8)
plt.plot(x_inc, f(x_inc),'ro', ms=8)
plt.show()         

1 个答案:

答案 0 :(得分:3)

你没有做错任何事。图中的第一个点实际上是一阶导数停止递减的点,因此二阶导数(最接近)为零。