-2

I have a df such as the one below:

df[(df.Name==name)&(df.Sex==sex)]

The columns available except for the ones mentioned above are "births" and "year". I've plotted a "line" graph with this

df.plot('year', 'births', kind='line')

Visually, I can see that there are more than one peaks in the plot. How can I, using Python, find the exact number of peaks?

Tanmoy
  • 789
  • 7
  • 14
  • Possible duplicate of [Peak detection in a 2D array](https://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array) – DYZ Feb 21 '18 at 17:45
  • There are many other more or less relevant answers on SO and elsewhere. Di you try to google? – DYZ Feb 21 '18 at 17:45
  • @DyZ yes, ofcourse. Didnt come up with anything similar to what I was looking for. – Tanmoy Feb 22 '18 at 07:36

1 Answers1

0

If you only want positive peaks you can do:

for i in range(1, len(array)-1):
    if max(array[i-1], array[i], array[i+1]) == array[i]:
         print('Peak found at {}'.format(i))

if you want both positive and negative peaks:

for i in range(1, len(array)-1):
    if max(array[i-1], array[i], array[i+1]) == array[i] or
       min(array[i-1], array[i], array[i+1]) == array[i]:
         print('Peak found at {}'.format(i))

if you want to ensure there's a big difference:

minimal_ratio = 1.5 # or whatever you choose
for i in range(1, len(array)-1):
    if array[i] > minimal_ratio * array[i-1] and
       array[i] > minimal_ratio * array[i+1]:
         print('Peak found at {}'.format(i))
Nathan
  • 3,558
  • 1
  • 18
  • 38
  • This approach doesnt help. The above approach finds local maxima. When i ran the code of my df, it returned that there are 25 peaks. But, when I look at the graph, I see 4 major peaks. I don't think I can put in images to explain my point further. I hope you understand what I am trying to achieve. – Tanmoy Feb 22 '18 at 07:37
  • @Tanmoy What's your definition of a peak? Is it a certain percentage above the mean? How close can two peaks be to each other? – Nathan Feb 22 '18 at 07:42
  • It should be a peak, not small hills (bad pun!) – Tanmoy Feb 22 '18 at 07:54
  • @Tanmoy you'll have to play around with the variable 'minimal_ratio' to see what works for you, hope it helps :) – Nathan Feb 22 '18 at 10:14