斜率法的原理就是使用最小二乘等方法对时序数据进行拟合,然后根据拟合成的直线的斜率k判断序列的数据走势,当k>0时,则代表趋势上升;当k<0时,则代表趋势下降。
代码
import numpy as np
def trendline(data):
order=1
index=[i for i in range(1,len(data)+1)]
coeffs = np.polyfit(index, list(data), order)
slope = coeffs[-2]
return float(slope)
打算用数值来表示一组数据的变化趋势,请问上面的斜率法适用吗,上面的代码怎么解读 data要什么类型的数值呢
import numpy as np
# data 传的是个list
def trendline(data):
order = 1
index = [i for i in range(1, len(data) + 1)]
coeffs = np.polyfit(index, list(data), order)
slope = coeffs[-2]
return float(slope)
# data 升序
a = [1, 2, 3, 4, 5]
# data 降序
b = [5, 4, 3, 2, 1]
c = trendline(a)
d = trendline(b)
print("升序斜率:" + str(c) + "\n降序斜率:" + str(d))