python - 找出函数开始和结束减少的位置

标签 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()         

最佳答案

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

关于python - 找出函数开始和结束减少的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36474725/

相关文章:

Python:将 wx.py.shell.Shell 插入单独的进程

java - 如果我们知道距离 x1, y1, y2 则计算 x2

python-2.7 - 在python中使用Abs查找函数的导数

ruby - 在 Ruby 中计算导数 ([i] - [i - 1])

math - 通过 3 点相关性对齐点云?

python - 如何使用 SymPy 加速长函数的符号导数?

python - 通过python脚本输入终端命令

python - 什么时候在 Python 中使用线程?

python - Pyplot - 当一个轴的列表长度不一致时,如何在同一个图表上绘制多条线?

python - 连 catch 体表面上的两点