python numpy find local minima

Solutions on MaxInterview for python numpy find local minima by the best coders in the world

showing results for - "python numpy find local minima"
Giorgio
22 Aug 2018
1import numpy as np
2from scipy.signal import argrelextrema
3import matplotlib.pyplot as plt
4
5x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
6y = np.array([2, 1, 3 ,5 ,3 ,9 ,8, 10, 7])
7
8# sort the data in x and rearrange y accordingly
9sortId = np.argsort(x)
10x = x[sortId]
11y = y[sortId]
12
13# this way the x-axis corresponds to the index of x
14plt.plot(x-1, y)
15plt.show()
16maxm = argrelextrema(y, np.greater)  # (array([1, 3, 6]),)
17minm = argrelextrema(y, np.less)  # (array([2, 5, 7]),)
18