在数据中找到多个最大值(图)

问题描述 投票:1回答:2

我有一个Y值列表。如果我绘制它我得到这个:

有三个主要峰值可见。

问题:在Python中,有没有办法在数据列表中找到第n个最主要的尖峰并打印它们的x位置(在列表中)?

我需要考虑彼此距离很小的尖刺。例如,第一个左大尖峰实际上是双尖峰(它是来自日光光谱的钠双线)。

python list plot max
2个回答
1
投票

编辑

我删除了一切。使用这两种新方法:

def FindMax( listOfY , indexes, numOfMax):
    'This method finds the maximum values in the list of peaks'
    listMax = []
    xList = []
    reconstructedList = []
    for c in range(0,numOfMax):
        listMax.append(max(listOfY))
        index = listOfY.index(max(listOfY))
        xList.append(indexes[index])
        listOfY.pop(index)
    return listMax, xList

def FindPeaks(listY):
    'This method finds the peaks from the list with the Y values'
    peaks = []
    indexes = []
    count = 0
    m2 = 0 #Old slope: starts with 0
    for value in range(1,len(listY)):
        m1 = listY[value] - listY[value-1] #New slope
        if( m2 > 0 and m1 < 0 ):
            peaks.append(listY[value-1])
            indexes.append( value-1 )
        m2 = m1 #Old slope is assigned
    return peaks, indexes

#Test
list = [1,3,55,5,76,26,77,88,4,96,1,5,2,7,3,100,100,100,76,25]
peaksList = FindPeaks(list) #List: (['peaks'],['indexes'])
print ("List: " , list)
print ("Peaks list: ",peaksList[0])
print ("Peaks indexes: ",peaksList[1])
maxList = FindMax(peaksList[0],peaksList[1],3)  
print ("Max: ", maxList[0])
print ("Max indexes: ", maxList[1])
peaksList = FindPeaks(list)
print ("Peaks: ",peaksList)

FindPeaks()方法将使用您的列表将Y值作为参数并且不会对其进行修改,它还会返回一个2D列表,其中该列表的第一个索引是峰值列表,第二个列表在“列表”中列出它们的索引。之后,您将所需的peaksList [0],peaksList1和最大峰值数作为参数传递给方法FindMax(),返回2D列表。这个新列表在索引'0'中包含最大峰值的列表,按降序排列,在peakList的索引'1'处是您在列表'列表'中找到它们的索引,这是您的真实列表。

在评论#Test下方,您可以找到我运行的所有测试。正如您所看到的,此方法无法检测平峰,但您可以将整个列表作为参数传递给方法FindMax(),并且上面描述了所有参数,您将获得平峰中的所有值,它看起来像一个相同值的序列([...',100,100,100'...])。但是FindPeaks()方法在找到所有其他峰值时非常有用(如我所描述的那样不是平峰),因此如果你有一个平峰,这个方法就不会返回它。

输出命令行窗口enter image description here

我希望你发现这对你有用。


-1
投票

我在制作你可以在我的github上找到的统计数据库时遇到了同样的问题,但回答你的问题;创建一个字典,其中X值为键,Y值为值。使用以下命令查找具有最大值的键:

max(dict, key=dict.get)

您可以将其用作字典的索引以获取该键的值。您要做的是初始化一个新字典,以防其他键共享相同的最大值:

new_dict = {max(dict, key=dict.get):dict[max(dict, key=dict.get)]}

最后,按键,值对遍历字典,使用任何键值对更新新词典,其中值等于初始最大键值,值对的值:

def mode(list):
    list = sorted(list)
    dict = {}

    '''iterates through list and gives key value pair
    key being the instance in the list, value being 
    number of times that instance appears in the list'''
    for i in list:
        #n will be the indicator of the frequency i shows up
        n = 0
        for e in list:
            if e == i:
                #n increases by one every time i shows up in the list again
                n+=1
        dict.update({i:n})

    '''initial list of max key, value pair. Will update if another key 
    shares same max value'''
    new_dict = {max(dict, key=dict.get):dict[max(dict, key=dict.get)]}

    for key, value in dict.iteritems():
    '''if value of instance == value of key, value pair in new_dict, it 
    shares the max value so add to new dictionary''''
        if value == new_dict.get(new_dict.keys()[0]):
            new_dict.update({key:value})

    return new_dict
© www.soinside.com 2019 - 2024. All rights reserved.