05/06/2020 python - How to calculate distance between highest value of function?
- Stack Overflow
How to calculate distance between highest value of function?
Asked 4 years, 11 months ago Active 4 years, 11 months ago Viewed 949 times
-1
Do you know if there is any algorithm or method for finding the distance between the highest points of a function, like that above (so the value of
A, B, C, D and more)?
For example, when you have array of values of this function and array of arguments of this function.
Or maybe thereour
By using is a built-in
site, function in Python
you acknowledge that you which does
have read this?
and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
https://stackoverflow.com/questions/31092125/how-to-calculate-distance-between-highest-value-of-function 1/6
05/06/2020 python - How to calculate distance between highest value of function? - Stack Overflow
y=np.array(radial)
x=np.arange(len(y))
m=argrelextrema(y, np.greater)[0]
z=[y[i] for i in m]
plt.plot(y)
plt.plot(m,z,'rs')
plt.show()
Where radial is a list of floats.
python numpy numerical-methods
edited Jun 27 '15 at 20:15 asked Jun 27 '15 at 18:31
wiedzminYo
392 2 8 20
If you have a list of values, say v, it is relatively easy to find indices i with the property that v(i-1), v(i+1) < v(i) - which would make v(i) a local max -- but in your
picture you are not simply computing the distances between successive local max but are ignoring some of them (the little spikes before the big spikes). You
need to find a criteria that would distinguish between the little spikes which you don't want and the big ones which you do. Perhaps some sort of moving average
will smooth the data to the point where it is no longer an issue. – John Coleman Jun 27 '15 at 18:41
1 Answer Active Oldest Votes
You can use scipy.signal.argrelextrema to find the index of the max values:
5 import numpy as np
from scipy.signal import argrelextrema
from matplotlib.pyplot import *
x = np.random.random(50) #data
m = argrelextrema(x, np.greater)[0] #array of indexes of the locals maxima
y = [x[i] for i in m] #array of max values
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
plot(x)
https://stackoverflow.com/questions/31092125/how-to-calculate-distance-between-highest-value-of-function 2/6
05/06/2020 python - How to calculate distance between highest value of function? - Stack Overflow
plot(m, y, 'rs')
show()
By using
After finding theour
maxsite, you acknowledge
values, that numpy.linalg.norm
you can use you have read and understand
to find theour Cookie Policy, Privacy Policy, and our Terms of Service.
distances:
https://stackoverflow.com/questions/31092125/how-to-calculate-distance-between-highest-value-of-function 3/6
05/06/2020 python - How to calculate distance between highest value of function? - Stack Overflow
distances = [np.linalg.norm(np.array([m[i], y[i-1]]) - np.array([m[i-1], y[i-1]])) for i
in np.arange(1, len(m))]
Or just subtract:
distances = [m[i] - m[i-1] for i in np.arange(1, len(m))]
Or even just use numpy.diff
distances = np.diff(m)
Adding this code for a nicer plot and test the result:
for i in range(len(distances)):
plot([m[i], m[i] + distances[i]], 2*[y[i]], color = 'g')
axvline(m[i], linestyle='--', color='0.75')
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
https://stackoverflow.com/questions/31092125/how-to-calculate-distance-between-highest-value-of-function 4/6
05/06/2020 python - How to calculate distance between highest value of function? - Stack Overflow
edited Jun 27 '15 at 19:30 answered Jun 27 '15 at 19:00
João Paulo
4,224 2 31 56
This is very helpful,but I have problem with finding index of max value.I have did excactly like you but my plot mark only 2 max value.Mayby you know why? –
wiedzminYo Jun 27 '15 at 20:13
can you post the source of your data? so I can see what could be the problem. – João Paulo Jun 27 '15 at 20:16
By That
Well... usingshould
our site,
haveyou acknowledge
worked... that you
Is the radial listhave read and
containing understand
the values our Cookie
you expect? Policy
– João , Privacy
Paulo Jun 27Policy
'15 at, 20:28
and our Terms of Service.
https://stackoverflow.com/questions/31092125/how-to-calculate-distance-between-highest-value-of-function 5/6
05/06/2020 python - How to calculate distance between highest value of function? - Stack Overflow
Ok I know why i have this problem.Becouse some of my max value are repeating.If you look closly at my chart you will see that most of my tops are flat. –
wiedzminYo Jun 27 '15 at 20:33
look the image for this data: data = np.array(list(map(lambda x: 1 if x%2 else 2, list(range(50))))) s10.postimg.org/p2m80woh5/flat.png , worked
fine. – João Paulo Jun 27 '15 at 20:41
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
https://stackoverflow.com/questions/31092125/how-to-calculate-distance-between-highest-value-of-function 6/6