Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

python-3.x - 在数字序列的顶部或底部找到点(Find the point on the top or bottom of a sequence of numbers)

I have a problem like this and i would like to write a snippet of code to solve this problem.

(我有这样的问题,我想写一段代码来解决这个问题。)

  1. Sequences like: [1,2,3,2], [1,3,2], [1,3,2,1] -> i want to output 3 (maximum) because the sequence increases to 3 and then decreases again

    (像[1,2,3,2], [1,3,2], [1,3,2,1]之类的序列->我想输出3(最大),因为序列增加到3,然后又减少)

  2. Sequences like [3,2,1,2], [3,1,2], [3,1,2,3] -> i want to output 1 (minimum) because the sequence decreases to 1 and then increases again

    (像[3,2,1,2], [3,1,2], [3,1,2,3]之类的序列->我想输出1(最小),因为该序列递减为1,然后又增加)

Any idea on how to do this automatically?

(关于如何自动执行此操作的任何想法?)

  ask by Tuyen Vo Quang translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try getting local maximas and/or local minimas:

(尝试获取局部最大值和/或局部最小值:)

import numpy as np
from scipy.signal import argrelextrema

a = np.array([3,2,1,3])

res=a[np.hstack([argrelextrema(a, np.greater),argrelextrema(a, np.less)]).ravel()]

This will return both local maximas and minimas.

(这将同时返回局部最大值和最小值。)

You can mark them somehow separately, if it's better for your use case.

(如果对您的用例更好,则可以单独标记它们。)

From your question I assumed it can be just one extremum.

(根据您的问题,我认为这可能只是一个极值。)

Also - depending on your data you might consider using np.less_equal or np.greater_equal instead of np.less or np.greater respectively.

(另外-根据您的数据,您可能会考虑分别使用np.less_equalnp.greater_equal而不是np.lessnp.greater 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...