我正在使用核密度估计来计算项目出现的概率密度函数。
使用这个输出,我想找到所有的局部最小值和最大值。我对本地最小/最大提取的不同方法感兴趣。
我正在使用核密度估计来计算项目出现的概率密度函数。
使用这个输出,我想找到所有的局部最小值和最大值。我对本地最小/最大提取的不同方法感兴趣。
一种方法是计算一阶导数(离散域中的差异)并找到符号发生变化的位置。这表明存在局部最小值或最大值。例如:
from numpy import diff, sign, cos, pi, arange
import matplotlib.pyplot as plt
t = arange(0,2,0.01)
x = cos(2*pi*t)
# Find derivative of x
first_derivative = diff(x)
# Calc. sign difference
sign_diff = sign(first_derivative[1:]) - sign(first_derivative[:-1])
# Find local min and max
local_max_index = [i for i,k in enumerate(sign_diff) if k == -2]
local_min_index = [i for i,k in enumerate(sign_diff) if k == 2]
# plot results
plt.figure()
plt.plot(t,x)
plt.plot(t[local_max_index],x[local_max_index], 'ro')
plt.plot(t[local_min_index],x[local_min_index], 'ro')
希望能帮助到你!