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

How can I detect if trend is increasing or decreasing in time series?(如何检测时间序列中趋势是增加还是减少?)
本文介绍了如何检测时间序列中趋势是增加还是减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)