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将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
