Skip to content Skip to sidebar Skip to footer

Identifying The Highest Peak, And Return The Values That Is Low To The Right And Left Of Highest Peak, By Keeping The Highest Peak As Reference

I have data where I need to return the values that are low by keeping the highest value or peak as a reference, there are many peaks, I need to return the lows of each peak and sto

Solution 1:

The zip() function is very useful when you are looking for patterns of relative values within a list. It allows you to obtain pairings (or triples) of values that are next to each other.

deffindHighPeaks(A,count):
    # positions of values that are top of hills or bottom of valleys
    pivots   = [i for a,(i,b),c inzip(A,enumerate(A[1:],1),A[2:])
                                if a>b and b<c or a<b and b>c]

    # merge pivot positions into triples# representing the low-high-low or high-low-high indexes
    iWaves = [(left,mid,right) for left,mid,right
                               inzip([0]+pivots,pivots,pivots[1:]+[len(A)-1])]
    
    # extract only peak values from patterns of hills and valleys
    peaks  = [(A[left],A[mid],A[right]) for left,mid,right in iWaves
                                        if A[left]<A[mid]]

    # find the highest peaks, and put them in a set
    highPeaks = set(sorted(peaks,key=lambda p:p[1])[-count:])

    # Return high peaks, in original orderreturn [p for p in peaks if p in highPeaks]

# A = big list
    
Q,R,S = zip(*findHighPeaks(A,6)) # large exampleprint(R) # (7237, 7288, 7215, 7242, 7368, 7469) peaksprint(Q) # (6420, 6475, 6388, 6381, 6502, 6659) Low on the leftprint(S) # (6076, 6180, 6452, 6018, 6176, 6337) Low on the right

A = ['22','21','22','23','24','25','26','27','28','27','24','15','23',
   '22','32','30','28','20','27','17','22','28','42','40','35','22','16']

Q,R,S = zip(*findHighPeaks(A,3)) # small exampleprint(R) # ('28', '32', '42') peaksprint(Q) # ('21', '22', '17') Low on the leftprint(S) # ('15', '20', '16') Low on the right

Here is a visual representation of the selection process, which is based on relative variations (increasing/decreasing).

                                  HI                                        
                                 '28''27''27'      HI    HI                     HI   
                           '26''24''23''32''42''25''15''22''30'     HI      '28''40''24'                   LO    LO     '28''27''22''35''23''20''17''22''22''22'                                          LO    LO                  '16''21'
            LO

peaks:                           2823322742||-------------------^^-------||--^^-||-^^------||-^^-||-------^^----------||left:       2115222017right:                                    1522201716

Post a Comment for "Identifying The Highest Peak, And Return The Values That Is Low To The Right And Left Of Highest Peak, By Keeping The Highest Peak As Reference"