python:如何找到密度曲线中的最后一个最小值
python : how to find last minima in density curve
我找不到简单的方法来获得与密度曲线相关的最小值。
有了这样的数据:
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)
我想获得与最后一个“峰值”相关的最后一个拐点,在这种情况下,它将是 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 位置,并且在能够获得高于最小值的所有数字之后。
感谢您的帮助!
最好放置最新的 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()
如你所见,我可以得到第二个峰是坐标 [7.921442255354041, 0.3685891140800782]
我找不到简单的方法来获得与密度曲线相关的最小值。 有了这样的数据:
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)
我想获得与最后一个“峰值”相关的最后一个拐点,在这种情况下,它将是 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 位置,并且在能够获得高于最小值的所有数字之后。
感谢您的帮助!
最好放置最新的 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()
如你所见,我可以得到第二个峰是坐标 [7.921442255354041, 0.3685891140800782]