Python:在一组数字中找到趋势 [英] Python: Finding a trend in a set of numbers
问题描述
我在Python中有一个数字列表,如下所示:
I have a list of numbers in Python, like this:
x = [12, 34, 29, 38, 34, 51, 29, 34, 47, 34, 55, 94, 68, 81]
在这些数字中找到趋势的最佳方法是什么?我对预测下一个数字不感兴趣,我只想输出许多数字的趋势,以便可以比较趋势.
What's the best way to find the trend in these numbers? I'm not interested in predicting what the next number will be, I just want to output the trend for many sets of numbers so that I can compare the trends.
通过趋势,我的意思是我想用数字表示数字是增加还是减少以及以什么速率增加.我不是一个数学家,所以可能有个合适的名字!
By trend, I mean that I'd like a numerical representation of whether the numbers are increasing or decreasing and at what rate. I'm not massively mathematical, so there's probably a proper name for this!
看起来我真正想要的是线性最佳拟合的系数.在Python中获得此最佳方法是什么?
Edit 2: It looks like what I really want is the co-efficient of the linear best fit. What's the best way to get this in Python?
推荐答案
可能是您想在图表上绘制这些数字并找到一条直线,从而使这些直线和数字之间的总距离最小化?这称为线性回归
Possibly you mean you want to plot these numbers on a graph and find a straight line through them where the overall distance between the line and the numbers is minimized? This is called a linear regression
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)的梯度和截距值,您将可以推断出超出数组末尾的线:
The trend line is unlikely to pass through your original points, but it will be as close as possible to the original points that a straight line can get. Using the gradient and intercept values of this trend line (a,b) you will be able to extrapolate the line past the end of the array:
extrapolatedtrendline=[a*index + b for index in range(20)] //replace 20 with desired trend length
这篇关于Python:在一组数字中找到趋势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!