如何检测时间序列中趋势是增加还是减少?

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

问题描述

我有几个星期的销售单位数据

I have few weeks data with units sold given

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

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

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.

寻找直线的坡度是最好的方法吗?以及如何在python中计算直线的斜角?

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.

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

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

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

[查看本官方文档]

你可以这样使用函数

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.

例如,

如果斜率是 +ve 值 -->增加趋势

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

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

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.

解决方案示例代码

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)  

结果

1845.3999999999999

1845.3999999999999

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

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

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

上一篇:使用 LSTM 循环网络的 Pybrain 时间序列预测 下一篇:使用 statsmodels 进行 Holt-Winters 时间序列预测

相关文章

最新文章