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

2024-05-28 22:54:50 发布

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

我有一个Y值列表。如果我把它画出来,我会发现:

image of a plot

有三个主峰可见。在

Python在列表中最重要的位置是什么?在

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


Tags: 距离列表光谱尖峰我会双线日光主峰
2条回答

编辑

我删除了所有内容。使用这两种新方法:

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值的列表作为参数,并且不会对其进行修改,它还返回一个二维列表,其中该列表的第一个索引是峰值列表,第二个索引在“list”中列出它们的索引。之后,peaksList[0]、peaksList1和所需的最大峰值数作为参数传递给FindMax()方法,返回一个2D列表。此新列表在索引“0”中有一个包含最大峰值的列表(按降序排列),在peaksList的索引“1”处,您可以在列表“list”找到它们,这是您的专用列表。在

在下面的注释“Test”下,您可以找到我运行的所有测试。正如您所看到的,这个方法不能检测平顶,但是您可以将整个列表作为参数传递给FindMax()方法,以及上面描述的所有参数,您将得到平坦峰值中的所有值,它看起来像一个相同值的序列([…',100100100'…])。但是FindPeaks()方法在查找所有其他峰值(不是我所描述的平坦峰值)时将非常有用,因此,如果您有一个平坦的峰值,此方法将不会返回它。在

输出命令行窗口enter image description here

我希望你觉得这个对你有用。在

我在制作一个stats库时遇到了同样的问题,你可以在github上找到这个库,但是为了回答你的问题,请制作一个以X值为键,Y值为值的字典。使用以下方法查找具有最大值的键:

max(dict, key=dict.get)

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

^{pr2}$

最后,按键、值对迭代字典,用任何键、值对更新新字典,其中值等于初始max key,value对的值:

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

相关问题 更多 >

    热门问题