【问题标题】:Python - Find min, max, and inflection points of normal curvePython - 查找正态曲线的最小值、最大值和拐点
【发布时间】: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


【解决方案1】:

这是一种非常简单且不可靠的方法来找到非噪声曲线的拐点:

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)

# 1st inflection point estimation
dy = np.diff(y) # first derivative
idx_max_dy = np.argmax(dy)

# Graph
plt.plot(x, y)
plt.plot(x[idx_max_dy], y[idx_max_dy], 'or', label='estimated inflection point')
plt.xlabel('x'); plt.ylabel('y'); plt.legend();

对于高斯曲线,拐点的实际位置是x1 = mean - std

为了处理真实数据,必须在寻找最大值之前对其进行平滑处理,例如应用简单的移动平均线、gaussian filterSavitzky-Golay filter,它们可以直接输出二阶导数...选择合适的过滤器取决于数据

【讨论】:

  • 谢谢@xdze2!很高兴知道Savitzky-Golay filter 过滤器输出二阶导数,正如您所说,过滤取决于数据。这是我正在寻找的东西(上面的版本 2 代码),不确定我是否做了二阶导数部分?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2012-04-18
  • 2019-02-25
  • 2021-09-15
相关资源
最近更新 更多