I'm using kernel density estimation in order to compute probability density function for item occurrence.

Using this output, i want to find all the local minims and maxims. I'm interested in different methods for local-min/max extraction.


1 Answer 1


One way to do it is to calculate first derivative (difference in discrete domain) and find where there is a change in the sign. That indicates the existence of a local minimum or maximum. For example:

from numpy import diff, sign, cos, pi, arange
import matplotlib.pyplot as plt
t = arange(0,2,0.01)
x = cos(2*pi*t)
# Find derivative of x
first_derivative = diff(x)

# Calc. sign difference
sign_diff = sign(first_derivative[1:]) - sign(first_derivative[:-1])

# Find local min and max
local_max_index = [i for i,k in enumerate(sign_diff) if k == -2]
local_min_index = [i for i,k in enumerate(sign_diff) if k == 2]

# plot results
plt.plot(t[local_max_index],x[local_max_index], 'ro')
plt.plot(t[local_min_index],x[local_min_index], 'ro')

enter image description here

Hope it helps!

  • $\begingroup$ This is only if you have a function right? What if you don't? $\endgroup$ Jan 15, 2020 at 10:39
  • $\begingroup$ Could you explain more what do you mean "when you have a funtion"? This works with descrete data points, you do not need to know the function that the data came from, if this is what you meant. $\endgroup$ Jan 15, 2020 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.