当将x和y值指定为numpy数组时,找到所有局部的Maxima和Minima [英] Find all local Maxima and Minima when x and y values are given as numpy arrays

查看:143
本文介绍了当将x和y值指定为numpy数组时,找到所有局部的Maxima和Minima的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组xy作为:

x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3, 5, 3, 9, 8, 10, 7])

我正在找到局部最小值和最大值的索引,如下所示:

I am finding index of local minima and maxima as follows:

sortId = np.argsort(x)
x = x[sortId]
y = y[sortId]
minm = np.array([])
maxm = np.array([])
while i < y.size-1:
   while(y[i+1] >= y[i]):
      i = i + 1

   maxm = np.insert(maxm, 0, i)
   i++
   while(y[i+1] <= y[i]):
      i = i + 1

   minm = np.insert(minm, 0, i)
   i++

此代码中的问题是什么? 答案应该是minima = [2, 5, 7]的索引 和maxima = [1, 3, 6].

What is the problem in this code? The answer should be index of minima = [2, 5, 7] and that of maxima = [1, 3, 6].

推荐答案

您根本不需要此while循环.下面的代码将为您提供所需的输出;它找到所有局部最小值和所有局部最大值,并将它们分别存储在minmmaxm中.请注意:将其应用于大型数据集时,请确保先平滑信号;否则,请确保平滑.否则,您将陷入无数的极端.

You do not need this while loop at all. The code below will give you the output you want; it finds all local minima and all local maxima and stores them in minm and maxm, respectively. Please note: When you apply this to large datasets, make sure to smooth the signals first; otherwise you will end up with tons of extrema.

import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt

x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3 ,5 ,3 ,9 ,8, 10, 7])

# sort the data in x and rearrange y accordingly
sortId = np.argsort(x)
x = x[sortId]
y = y[sortId]

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.show()
maxm = argrelextrema(y, np.greater)  # (array([1, 3, 6]),)
minm = argrelextrema(y, np.less)  # (array([2, 5, 7]),)

这应该比上面的while循环高效得多.

This should be far more efficient than the above while loop.

情节看起来像这样;我移动了x值,使其与minmmaxm)中返回的索引相对应:

The plot looks like this; I shifted the x-values so that they correspond to the returned indices in minm and maxm):

从SciPy 1.1版开始,您还可以使用 find_peaks :

As of SciPy version 1.1, you can also use find_peaks:

from scipy.signal import find_peaks

peaks, _ = find_peaks(y)

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.plot(peaks, y[peaks], "x")
plt.show()

这产生了

令人高兴的是,您现在还可以轻松设置最小峰高(例如8):

The nice thing is, that you can now also easily also set a minimum peak height (e.g. 8):

peaks, _ = find_peaks(y, height=8)

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.plot(peaks, y[peaks], "x")
plt.show() 

请注意,现在排除了第一个峰,因为其高度低于8.

Note that now the first peak is excluded as its height is below 8.

此外,您还可以设置峰之间的最小距离(例如5个):

Furthermore, you can set also the minimal distance between peaks (e.g. 5):

peaks, _ = find_peaks(y, distance=5)

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.plot(peaks, y[peaks], "x")
plt.show()

现在排除了中间峰,因为它与其他两个峰的距离小于5.

Now the middle peak is excluded as its distance to the other two peaks is less than 5.

这篇关于当将x和y值指定为numpy数组时,找到所有局部的Maxima和Minima的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆