在python中查找数组的转折点
例如,如果我有一个数组:在python中查找数组的转折点,python,arrays,python-2.7,minima,Python,Arrays,Python 2.7,Minima,例如,如果我有一个数组: A = (0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6) 可以看出,有4个转折点。(A[4]、A[6]、A[13]、A[17]) 如何使用python返回转折点的数量 import numpy as np import scipy.integrate as SP import math def turningpoints(A): print A N = 0 delta = 0 delta_prev =
A = (0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6)
可以看出,有4个转折点。(A[4]、A[6]、A[13]、A[17])
如何使用python返回转折点的数量
import numpy as np
import scipy.integrate as SP
import math
def turningpoints(A):
print A
N = 0
delta = 0
delta_prev = 0
for i in range(1,19):
delta = A[i-1]-A[i] #Change between elements
if delta < delta_prev: #if change has gotten smaller
N = N+1 #number of turning points increases
delta_prev = delta #set the change as the previous change
return N
if __name__ == "__main__":
A = np.array([0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6])
print turningpoints(A)
将numpy导入为np
导入scipy.integrate作为SP
输入数学
def转向点(A):
打印
N=0
增量=0
delta_prev=0
对于范围(1,19)内的i:
delta=A[i-1]-A[i]#元素之间的变化
如果delta
目前,这个系统有缺陷,当然也不是很优雅。有什么想法吗?>def turns(L):
>>> def turns(L):
... answer, delta = 0, -1 if L[1]<L[0] else 1
... i = 2
... while i < len(L):
... d = -1 if L[i]<L[i-1] else 1
... if d != delta:
... answer += 1
... delta = d
... i += 1
... return answer
...
>>> L = [0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6]
>>> turns(L)
4
... 如果L[1]>L=[0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,6],则答案为delta=0,-1
>>>转弯(L)
4.
你想得太多了。“转折点”是指高于或低于两侧的点
def turningpoints(x):
N=0
for i in range(1, len(x)-1):
if ((x[i-1] < x[i] and x[i+1] < x[i])
or (x[i-1] > x[i] and x[i+1] > x[i])):
N += 1
return N
>>> turningpoints([0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6])
4
def转折点(x):
N=0
对于范围(1,len(x)-1)内的i:
if((x[i-1]x[i]和x[i+1]>x[i]):
N+=1
返回N
>>>转折点([0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6])
4.
def三组(可滑动):
对于范围内的i(透镜(滑动电缆)-2):
屈服滑移线[i:i+3]
def转弯(L):
对于索引,枚举中的三(组中的三(L)):
如果(三[0]>三[1]<三[2])或(三[0]<三[1]>三[2]):
收益率指数+1
>>>列表(轮次([0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6]))
[4, 6, 13, 17]
>>>蓝
4.
如果您有numpy:
def turningpoints(lst):
dx = np.diff(lst)
return np.sum(dx[1:] * dx[:-1] < 0)
def转向点(lst):
dx=np.diff(lst)
返回np.sum(dx[1:]*dx[:-1]<0)
或非numpy等效版本:
def turningpoints(lst):
dx = [x - y for x, y in zip(lst[1:], lst[:-1])]
return sum(dx1 * dx2 < 0 for dx1, dx2 in zip(dx[1:], dx[:-1]))
def转向点(lst):
dx=[x-y代表x,y在zip中(lst[1:],lst[:-1])]
返回和(dx1*dx2<0表示dx1,zip中的dx2(dx[1:],dx[:-1]))
为了一句俏皮话的爱:
def turningpoints(lst):
return sum(x0*x1 + x1*x2 < x1*x1 + x0*x2 for x0, x1, x2 in zip(lst[2:], lst[1:-1], lst[:-2]))
def转向点(lst):
返回和(x0*x1+x1*x2
但是这个问题的可读性可能会降低:)我知道这是一个老问题,但我也遇到了同样的问题,正如卡丹在的评论中所说的,答案无法处理像
[1,2,3,4,4,4,3,2,1]这样具有相同值的连续点。我的实现可以处理这个问题
尽管如此,它返回两个列表,其中包含最小和最大转折点的索引
def turning_points(array):
''' turning_points(array) -> min_indices, max_indices
Finds the turning points within an 1D array and returns the indices of the minimum and
maximum turning points in two separate lists.
'''
idx_max, idx_min = [], []
if (len(array) < 3):
return idx_min, idx_max
NEUTRAL, RISING, FALLING = range(3)
def get_state(a, b):
if a < b: return RISING
if a > b: return FALLING
return NEUTRAL
ps = get_state(array[0], array[1])
begin = 1
for i in range(2, len(array)):
s = get_state(array[i - 1], array[i])
if s != NEUTRAL:
if ps != NEUTRAL and ps != s:
if s == FALLING:
idx_max.append((begin + i - 1) // 2)
else:
idx_min.append((begin + i - 1) // 2)
begin = i
ps = s
return idx_min, idx_max
例子
对于第二个:TypeError:“generator”对象不可下标
。大概dx
应该是一个列表。此解决方案不起作用。试试[5,6,7,7,7,6,5]@Cardin,你是说这里有两个转折点而不是零?那么[5,6,7,7,7,8,9]呢?有零个或两个(或其他)吗?如中所示,该序列有一个转折点,但您的算法无法检测到它,因为它假设方向必须立即发生变化。您新发布的序列没有任何转折点,因为它是一个固定点。@Cardin——我缺少一个正式的定义吗?
def turning_points(array):
''' turning_points(array) -> min_indices, max_indices
Finds the turning points within an 1D array and returns the indices of the minimum and
maximum turning points in two separate lists.
'''
idx_max, idx_min = [], []
if (len(array) < 3):
return idx_min, idx_max
NEUTRAL, RISING, FALLING = range(3)
def get_state(a, b):
if a < b: return RISING
if a > b: return FALLING
return NEUTRAL
ps = get_state(array[0], array[1])
begin = 1
for i in range(2, len(array)):
s = get_state(array[i - 1], array[i])
if s != NEUTRAL:
if ps != NEUTRAL and ps != s:
if s == FALLING:
idx_max.append((begin + i - 1) // 2)
else:
idx_min.append((begin + i - 1) // 2)
begin = i
ps = s
return idx_min, idx_max
sum(len(x) for x in turning_points(X))