a = np.array([1, 3, 7, 1, 2, 6, 0, 1])
- 直接上代码:
import numpy as np a = np.array([1, 3, 7, 1, 2, 6, 0, 1]) b1 = np.diff(a) b2 = np.sign(b1) b3 = np.diff(b2) print(b1) # [ 2 4 -6 1 4 -6 1] print(b2) # [ 1 1 -1 1 1 -1 1] print(b3) # [ 0 -2 2 0 -2 2] index = np.where(np.equal(b3, -2))[0] + 1 print(index) # [2 5]
-
np.where(condition,x,y)
-
np.where(condition,x,y) 当where内有三个参数时,第一个参数表示条件,当条件成立时where方法返回x,当条件不成立时where返回y。
-
np.where(condition) 当where内只有一个参数时,那个参数表示条件,当条件成立时,where返回的是每个符合condition条件元素的坐标,返回的是以元组的形式。
-
np.diff(a,n=1,axis = -1)
- 沿着指定轴计算第N维的离散差值:
- a:输入矩阵
- n:可选,代表要执行几次差值
- axis:默认是最后一个
- 示例:
-
In [29]: x = np.arange(1 , 16).reshape((3 , 5))^M ...: print(x) ...: ...: [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]] In [30]: np.diff(x,axis=1) #默认axis=1 Out[30]: array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) In [31]: np.diff(x,n=2,axis=1) #在原来的基础上再计算一遍 Out[31]: array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) In [33]: np.diff(x,axis=0) #换了轴 Out[33]: array([[5, 5, 5, 5, 5], [5, 5, 5, 5, 5]])
找到一个一维数字数组a中的所有峰值。峰顶是两边被较小数值包围的点。
最新推荐文章于 2024-08-08 14:28:23 发布