Navi - 9 months ago 72

Python Question

Can you suggest a module function from numpy/scipy that can find local maxima/minima in a 1D numpy array? Obviously the simplest approach ever is to have a look at the nearest neighbours, but I would like to have an accepted solution that is part of the numpy distro.

Answer

If you are looking for all entries in the 1d array `a`

smaller than their neighbors, you can try

```
numpy.r_[True, a[1:] < a[:-1]] & numpy.r_[a[:-1] < a[1:], True]
```

You could also smooth your array before this step using `numpy.convolve()`

.

I don't think there is a dedicated function for this.