python:如何在密度曲线中找到最后一个极小值

2024-09-28 21:59:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我找不到简单的方法得到与密度曲线相关的极小值。 对于此类数据:

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import seaborn as sns
    test = [7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 8, 7, 7, 8, 7, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 8, 7, 4, 4, 7, 8, 7]
    sns.kdeplot(test)

enter image description here

我想得到与最后一个“峰值”相关的最后一个拐点,在这种情况下,它将是x=8附近的最小值。 我尝试了像get_peaks这样的函数,但这不起作用,因为我需要得到最后一个拐点之后的所有值,在某些情况下,例如:

test2 = [8, 7, 7, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 8, 12] 

get_peaks没有告诉我最后一个号码。 我的想法是得到最后一个极小值的x位置,然后得到所有高于极小值的数字

谢谢你的帮助


Tags: 数据方法fromtestimportgetas情况
1条回答
网友
1楼 · 发布于 2024-09-28 21:59:34

最好放置最新的find_peaks()实现。在这里,我尝试使用这个函数来获取它

import matplotlib.pyplot as plt
from scipy.signal import find_peaks
import seaborn as sns

test = [7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 8, 7, 7, 8, 7, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 8, 7, 4, 4, 7, 8, 7]
ax = sns.kdeplot(test)
x = ax.lines[0].get_xdata() # Get the x data of the distribution
y = ax.lines[0].get_ydata() # Get the y data of the distribution
xy = [[x[i], y[i]] for i in range(len(x))]

# find peak
peak_coord = [xy[i] for i in find_peaks(y)[0]]
sorted_peak = sorted(peak_coord, key=lambda x: x[1])

# sort peak based on its `y` coord
sorted_peak.reverse() # [[7.01539631380498, 0.7002187588539351], [7.921442255354041, 0.3685891140800782], [3.984828854140883, 0.043144074377544736]]

# second peak
second_peak = sorted_peak[1] # [7.921442255354041, 0.3685891140800782]

plt.show()

正如你所看到的,我可以得到第二个峰值是在coord[7.921442255354041, 0.3685891140800782]

相关问题 更多 >