python价格趋势软件_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.

Edit: 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!

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值