擅长:python、mysql、java
<p>你的意思可能是你想把这些数字画在一张图上,然后在图上找到一条直线,在直线和数字之间的总距离是最小的?这叫做线性回归</p>
<pre><code>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
</code></pre>
<p>趋势线不太可能穿过原始点,但它将尽可能接近直线可以得到的原始点。使用该趋势线(a,b)的梯度和截距值,您将能够推断出超过数组末尾的线:</p>
<pre><code>extrapolatedtrendline=[a*index + b for index in range(20)] //replace 20 with desired trend length
</code></pre>