【发布时间】:2019-08-27 17:46:33
【问题描述】:
我试图在植被曲线中找到最小值、季节开始、生长旺季、最大生长、衰老、季节结束、最小值(即拐点)的位置(即 x 值)。我在这里以正态曲线为例。我确实遇到了一些代码来找到斜率和一阶/二阶导数的变化,但无法为我的案例实现它们。如果有任何相关示例,请指导我,并感谢您的帮助。谢谢!
## Version 2 code
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
x_min = 0.0
x_max = 16.0
mean = 8
std = 2
x = np.linspace(x_min, x_max, 100)
y = norm.pdf(x, mean, std)
# Slice the group in 3
def group_in_threes(slicable):
for i in range(len(slicable)-2):
yield slicable[i:i+3]
# Locate the change in slope
def turns(L):
for index, three in enumerate(group_in_threes(L)):
if (three[0] > three[1] < three[2]) or (three[0] < three[1] > three[2]):
yield index + 1
# 1st inflection point estimation
dy = np.diff(y, n=1) # first derivative
idx_max_dy = np.argmax(dy)
ix = list(turns(dy))
print(ix)
# All inflection point estimation
dy2 = np.diff(dy, n=2) # Second derivative?
idx_max_dy2 = np.argmax(dy2)
ix2 = list(turns(dy2))
print(ix2)
# Graph
plt.plot(x, y)
#plt.plot(x[ix], y[ix], 'or', label='estimated inflection point')
plt.plot(x[ix2], y[ix2], 'or', label='estimated inflection point - 2')
plt.xlabel('x'); plt.ylabel('y'); plt.legend(loc='best');
【问题讨论】:
-
我认为解决方案将取决于真实数据,例如噪声类型的水平......如果曲线看起来像高斯,为什么不拟合高斯(使用scipy.optimize.curve_fit),然后根据获得的均值和方差计算想要的特征?特征,即季节开始、生长旺季、最大生长、衰老、季节结束……是如何“数学”确定的?
-
谢谢@xdze2!我使用的曲线只是代表。我主要是在寻找曲线拐点之前的顶点列表。一种方法是使用二阶导数并寻找从
+ve到-ve的符号变化,反之亦然。我只是不知道该怎么做。
标签: python-3.x numpy scipy derivative