首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当x和y值以数值数组形式给出时,查找所有局部最大值和最小值

当x和y值以数值数组形式给出时,查找所有局部最大值和最小值
EN

Stack Overflow用户
提问于 2015-06-26 18:12:49
回答 3查看 44K关注 0票数 13

我有两个数组xy as:

代码语言:javascript
复制
x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3, 5, 3, 9, 8, 10, 7])

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

代码语言:javascript
复制
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]的索引。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-26 20:57:38

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

代码语言:javascript
复制
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循环效率高得多。

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

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

代码语言:javascript
复制
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):

代码语言:javascript
复制
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。

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

代码语言:javascript
复制
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。

票数 25
EN

Stack Overflow用户

发布于 2015-06-28 08:33:25

代码语言:javascript
复制
x=np.array([6,3,5,2,1,4,9,7,8])
y=np.array([2,1,3,5,7,9,8,10,7])

sort_idx = np.argsort(x)
y=y[sort_idx]
x=x[sort_idx]
minm=np.array([],dtype=int)
maxm=np.array([],dtype=int)
length = y.size
i=0

while i < length-1:
    if i < length - 1:
        while i < length-1 and y[i+1] >= y[i]:
            i+=1

        if i != 0 and i < length-1:
            maxm = np.append(maxm,i)

        i+=1

    if i < length - 1:
        while i < length-1 and y[i+1] <= y[i]:
            i+=1

        if i < length-1:
            minm = np.append(minm,i)
        i+=1


print minm
print maxm

minmmaxm分别包含最小值和最大值的索引。

票数 1
EN

Stack Overflow用户

发布于 2015-06-26 18:23:50

这将工作得很好。

Python使用+=而不是++

在while循环中使用i之前,您必须指定一些值-在本例中为0-,这样可以初始化它以避免错误。

代码语言:javascript
复制
import numpy as np

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


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

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

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

print minm, maxm
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31070563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档