
[英]Reading the content of a VISIO file from top to bottom in a sequence using python
[英]Find the point on the top or bottom of a sequence of numbers
我有这样的问题,我想写一段代码来解决这个问题。
[1,2,3,2], [1,3,2], [1,3,2,1]
-> 我想输出 3(最大值),因为序列增加到 3 然后再次减少[3,2,1,2], [3,1,2], [3,1,2,3]
的序列 -> 我想输出 1(最小值),因为序列减少到 1 然后再次增加关于如何自动执行此操作的任何想法?
尝试获取局部最大值和/或局部最小值:
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()]
这将返回局部最大值和最小值。 如果对您的用例更好,您可以以某种方式分别标记它们。 根据您的问题,我认为它可能只是一个极值。 此外 - 根据您的数据,您可能会考虑分别使用np.less_equal
或np.greater_equal
而不是np.less
或np.greater
。
我发现在 Python 3 中实现这个算法很有趣。基本思想实际上是找到给定数字序列的最小值和最大值。 然而,一个序列可以有几个最大值点和几个最小值点需要考虑。 然而,这是我实现的算法。 我希望它有用。
sequence = [1,2,3,4,5,4,3,2,8,10,1]
index = 1
max_points = [] #A sequence may have multiple points of relatives max
relative_maximum = sequence[0]
for element in range(len(sequence)):
if(index == len(sequence)):
break
if(sequence[element] < sequence[index]):
relative_maximum = sequence[index]
change_direction = True
else:
if(change_direction == True):
max_points.append(relative_maximum)
change_direction = False
index = index + 1
index = 1
min_points = [] #A sequence may have multiple points of relatives min
relative_minimum = sequence[0]
for element in range(len(sequence)):
if(index == len(sequence)):
break
if(sequence[element] > sequence[index]):
relative_minimum = sequence[index]
change_direction = True
else:
if(change_direction == True):
min_points.append(relative_minimum)
change_direction = False
index = index + 1
print("The max points: " + str(max_points))
print("The min points: " + str(min_points))
结果:最大点数:[5, 10] 最小点数:[2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.