如何检测时间序列中的趋势是增加还是减少? [英] How can I detect if trend is increasing or decreasing in time series?

查看:54
本文介绍了如何检测时间序列中的趋势是增加还是减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几周的销售数据

xs[weeks] = [1,2,3,4]ys['销售单位'] = [1043,6582,5452,7571]

从给定的系列中,我们可以看到,虽然从 xs[2] 到 xs[3] 有所下降,但总体上呈上升趋势.如何在小时间序列数据集中检测趋势.

找到一条直线的斜率是最好的方法吗?以及如何计算python中一条线的倾斜角?

解决方案

我遇到了您今天面临的同样问题.为了检测趋势,我找不到特定的函数来处理这种情况.

我发现了一个非常有用的函数,即numpy.polyfit():

numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)

[查看官方文档]

你可以像这样使用这个功能

def 趋势检测器(list_of_index,array_of_data,order=1):结果 = np.polyfit(list_of_index, list(array_of_data), order)斜率 = 结果[-2]返回浮点数(斜率)

此函数返回一个浮点值,指示您的数据趋势,您也可以通过类似的方式对其进行分析.

例如

<块引用>

如果斜率是 +ve 值 -->上升趋势

如果斜率是 -ve 值 -->下降趋势

如果斜率为零值-->没有趋势

玩这个函数并根据您的问题找出正确的阈值并将其作为条件.

解决方案的示例代码

将 numpy 导入为 npdef 趋势线(索引,数据,订单 = 1):coeffs = np.polyfit(index, list(data), order)斜率 = 系数 [-2]返回浮点数(斜率)索引=[1,2,3,4]列表=[1043,6582,5452,7571]结果=趋势线(索引,列表)打印(结果)

结果

<块引用>

1845.3999999999999

根据此输出,结果远大于零,因此表明您的数据正在稳步增加.

I have few weeks data with units sold given

xs[weeks] = [1,2,3,4]
ys['Units Sold'] = [1043,6582,5452,7571]

from the given series, we can see that although there is a drop from xs[2] to xs[3] but overall the trend is increasing. How to detect the trend in small time series dataset.

Is finding a slope for the line is the best way? And how to calculate slope angle of a line in python?

解决方案

I have gone through the same issue that you face today. In order to detect the trend, I couldn't find a specific function to handle the situation.

I found a really helpful function ie, numpy.polyfit():

numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False) 
                                                    

[Check this Official Documentation]

You can use the function like this

def trenddetector(list_of_index, array_of_data, order=1):
    result = np.polyfit(list_of_index, list(array_of_data), order)
    slope = result[-2]
    return float(slope)

This function returns a float value that indicates the trend of your data and also you can analyze it by something like this.

For example,

if the slope is a +ve value --> increasing trend

if the slope is a -ve value --> decreasing trend

if the slope is a zero value --> No trend

Play with this function and find out the correct threshold as per your problem and give it as a condition.

Example Code for your Solution

import numpy as np
def trendline(index,data, order=1):
    coeffs = np.polyfit(index, list(data), order)
    slope = coeffs[-2]
    return float(slope)

index=[1,2,3,4]
List=[1043,6582,5452,7571]
resultent=trendline(index,List)
print(resultent)  

RESULT

1845.3999999999999

As per this output, The result is much greater than zero so it shows your data is increasing steadily.

这篇关于如何检测时间序列中的趋势是增加还是减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆