可能你的意思是你想在图表上绘制这些数字并找到一条穿过它们的直线,其中直线和数字之间的总距离最小?这称为线性回归
def linreg(X, Y):
"""
return a,b in solution to y = ax + b such that root mean square distance between trend line and original points is minimized
"""
N = len(X)
Sx = Sy = Sxx = Syy = Sxy = 0.0
for x, y in zip(X, Y):
Sx = Sx + x
Sy = Sy + y
Sxx = Sxx + x*x
Syy = Syy + y*y
Sxy = Sxy + x*y
det = Sxx * N - Sx * Sx
return (Sxy * N - Sy * Sx)/det, (Sxx * Sy - Sx * Sxy)/det
x = [12, 34, 29, 38, 34, 51, 29, 34, 47, 34, 55, 94, 68, 81]
a,b = linreg(range(len(x)),x) //your x,y are switched from standard notation
趋势线不太可能通过你的原始点,但它会尽可能接近直线可以获得的原始点。使用此趋势线 (a,b) 的梯度和截距值,您将能够推断出超过数组末尾的线:
extrapolatedtrendline=[a*index + b for index in range(20)] //replace 20 with desired trend length