numpy.diff
一阶差分由 out[n] = a[n+1] - a[n] 给出
https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.diff.html
import numpy as np
data = [1, 2, 4, 5, 8, 7, 6, 4, 1]
data = np.array(data, dtype=float)
velocity = np.diff(data)
acceleration = np.diff(velocity)
jerk = np.diff(acceleration)
jounce = np.diff(jerk)
print data
print velocity
print acceleration
print jerk
print jounce
>>>
[ 1. 2. 4. 5. 8. 7. 6. 4. 1.]
# positive numbers = rising
[ 1. 2. 1. 3. -1. -1. -2. -3.]
# positive numbers = concave up
[ 1. -1. 2. -4. 0. -1. -1.]
# positive numbers = curling up
[-2. 3. -6. 4. -1. 0.]
# positive numbers = snapping up
[ 5. -9. 10. -5. 1.]
https://en.wikipedia.org/wiki/Velocity
https://en.wikipedia.org/wiki/Acceleration
https://en.wikipedia.org/wiki/Jerk_(物理学)
https://en.wikipedia.org/wiki/Jounce
我倾向于然后除一阶导数;移动平均速度乘以 100 以转换为 %ROC;有时加速也很重要;凹度......你得到的混蛋/跳跃越多,数据变得越随机/嘈杂
您还可以计算每个的平均值:
print np.mean(data)
print np.mean(velocity)
print np.mean(acceleration)
对形状进行概括,对于这个样本集:
>>>
4.22222222222 # average value
0.0 # generally sideways; no trend
-0.571428571429 # concave mostly down
然后是平均相对标准偏差
import numpy as np
data = [1, 2, 4, 5, 8, 7, 6, 4, 1]
coef_variance = np.std(data) / np.mean(data)
print coef_variance
>>>0.566859453383
我称之为“相当不稳定”;但不是极端的数量级;通常 >1 被认为是“高度变异”
https://en.wikipedia.org/wiki/Coefficient_of_variation
如果我们绘制:
import matplotlib.pyplot as plt
import numpy as np
data = [1, 2, 4, 5, 8, 7, 6, 4, 1]
x = range(9)
plt.plot(x,data,c='red',ms=2)
plt.show()
我们可以看到这是对我们发现的一般情况的良好描述:
没有整体上升/下降趋势,波动较大,下凹;意味着刚刚超过4
你也可以polyfit:
import matplotlib.pyplot as plt
import numpy as np
data = [1, 2, 4, 5, 8, 7, 6, 4, 1]
x = range(9)
plt.plot(x,data,c='red',ms=2)
poly = np.polyfit(x,data,2)
z = []
for x in range(9):
z.append(poly[0]*x*x + poly[1]*x + poly[2])
x = range(9)
plt.plot(x,z,c='blue',ms=2)
print poly
plt.show()
返回:
[-0.37445887 3.195671 -0.07272727]
换句话说:
-0.374x^2 + 3.195x - 0.072
其中情节:
从那里你可以计算平方和,看看你的模型有多准确
numpy/scipy 中的平方差总和 (SSD)
你可以迭代 polyfit 过程,每次都增加度数
np.polyfit(x,data,degree)
直到您获得足够低的 SSD 以满足您的需求;这会告诉您您的数据是否更多 x^2ish、x^3ish、x^4ish 等。
while ssd > your_desire:
poly_array = polyfit()
ssd = sum_squares(poly_array, data)
degree +=1