- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正确执行加法模型有一些问题。
我有那个数据框:
当我运行此代码时:
import statsmodels as sm
import statsmodels.api as sm
decomposition = sm.tsa.seasonal_decompose(df, model = 'additive')
fig = decomposition.plot()
matplotlib.rcParams['figure.figsize'] = [9.0,5.0]
我收到了这条消息:
最佳答案
具有相同的 ValueError,这只是我自己的一些测试和少量研究的结果,并没有声称它是完整或专业的。请评论或回答任何发现错误的人。
当然,您的数据应该按索引值的正确顺序排列,您可以使用 df.sort_index(inplace=True)
来保证这一点。 ,正如您在回答中所述。这本身并没有错,尽管错误消息与排序顺序无关,而且我已经检查过这一点:在我对手头的巨大数据集的索引进行排序时,错误不会消失。是的,我也必须对 df.index 进行排序,但是分解()也可以处理未排序的数据以及项目在此处和那里及时跳转的情况:然后您只需从左到右再向后得到很多蓝线,直到整个图形都充满它。更重要的是,通常,排序已经是正确的顺序了。就我而言,排序无助于修复错误。因此,我也怀疑索引排序是否修复了您的情况下的错误,因为:错误实际上说明了什么?
值错误:您必须指定:
Period of the series. Must be used if x is not a pandas object or ifthe index of x does not have a frequency. Overrides defaultperiodicity of x if x is a pandas object with a timeseries index.
period = len(df_nested)
(= 周期数)以获得季节性和趋势的最佳分割。如果每个周期的元素随时间变化,其他值可能会更好。
len(df_unnested)/2
.
period = 10
.
period=20
相反,这会引发以下错误:
ValueError: x must have 2 complete cycles requires 40observations. x only has 20 observation(s)
period = 1
应该已经把它拿走了,但是对于时间序列分析,“=1”并没有揭示任何新东西,每个周期只有1个项目,趋势与原始数据相同,季节性为0,残差始终为0.
df_test = pd.DataFrame({'timestamp': [1462352000000000000, 1462352100000000000, 1462352200000000000, 1462352300000000000],
'listData': [[1,2,1,9], [2,2,3,0], [1,3,3,0], [1,1,3,9]],
'duration_sec': [3.0, 3.0, 3.0, 3.0]})
tdi = pd.DatetimeIndex(df_test.timestamp)
df_test.set_index(tdi, inplace=True)
df_test.drop(columns='timestamp', inplace=True)
df_test.index.name = 'datetimeindex'
df_test = df_test.explode('listData')
sizes = df_test.groupby(level=0)['listData'].transform('size').sub(1)
duration = df_test['duration_sec'].div(sizes)
df_test.index += pd.to_timedelta(df_test.groupby(level=0).cumcount() * duration, unit='s')
结果 df_test['listData'] 如下所示:
2016-05-04 08:53:20 1
2016-05-04 08:53:21 2
2016-05-04 08:53:22 1
2016-05-04 08:53:23 9
2016-05-04 08:55:00 2
2016-05-04 08:55:01 2
2016-05-04 08:55:02 3
2016-05-04 08:55:03 0
2016-05-04 08:56:40 1
2016-05-04 08:56:41 3
2016-05-04 08:56:42 3
2016-05-04 08:56:43 0
2016-05-04 08:58:20 1
2016-05-04 08:58:21 1
2016-05-04 08:58:22 3
2016-05-04 08:58:23 9
现在看看不同时期的整数值。
period = 1
:
result_add = seasonal_decompose(x=df_test['listData'], model='additive', extrapolate_trend='freq', period=1)
plt.rcParams.update({'figure.figsize': (5,5)})
result_add.plot().suptitle('Additive Decompose', fontsize=22)
plt.show()
period = 2
:
result_add = seasonal_decompose(x=df_test['listData'], model='additive', extrapolate_trend='freq', period=2)
plt.rcParams.update({'figure.figsize': (5,5)})
result_add.plot().suptitle('Additive Decompose', fontsize=22)
plt.show()
period = 4
:
result_add = seasonal_decompose(x=df_test['listData'], model='additive', extrapolate_trend='freq', period=int(len(df_test)/4))
plt.rcParams.update({'figure.figsize': (5,5)})
result_add.plot().suptitle('Additive Decompose', fontsize=22)
plt.show()
period = 8
:
result_add = seasonal_decompose(x=df_test['listData'], model='additive', extrapolate_trend='freq', period=int(len(df_test)/2))
plt.rcParams.update({'figure.figsize': (5,5)})
result_add.plot().suptitle('Additive Decompose', fontsize=22)
plt.show()
sm.tsa.seasonal_decompose(df, model = 'additive', period = int(len(df)/2))
2. 的详细信息:“... 或 x 必须是一个带有 DatetimeIndex 且频率未设置为 None 的 Pandas 对象”
sm.tsa.seasonal_decompose(df.asfreq('MS'), model = 'additive')
见
How to set frequency with pd.to_datetime()?有关更多详细信息,以及有关如何处理差距的信息。
df_test.asfreq('s')
的输出(=频率以秒为单位):
2016-05-04 08:53:20 1
2016-05-04 08:53:21 2
2016-05-04 08:53:22 1
2016-05-04 08:53:23 9
2016-05-04 08:53:24 NaN
...
2016-05-04 08:58:19 NaN
2016-05-04 08:58:20 1
2016-05-04 08:58:21 1
2016-05-04 08:58:22 3
2016-05-04 08:58:23 9
Freq: S, Name: listData, Length: 304, dtype: object
你在这里看到,虽然我的数据只有 16 行,但引入以秒为单位的频率迫使 df 为 304 行,只能从“08:53:20”到“08:58:23”,这里造成 288 个间隙.更重要的是,在这里你必须打准确的时间。如果您将 0.1 甚至 0.12314 秒作为您的实际频率,则您将不会命中大多数带有索引的项目。
df_test.asfreq('min')
:
2016-05-04 08:53:20 1
2016-05-04 08:54:20 NaN
2016-05-04 08:55:20 NaN
2016-05-04 08:56:20 NaN
2016-05-04 08:57:20 NaN
2016-05-04 08:58:20 1
我们看到只有第一分钟和最后一分钟被填满,其余的没有被击中。
df_test.asfreq('d')
:
2016-05-04 08:53:20 1
我们看到您只得到第一行作为结果 df,因为只有一天覆盖。它会给你找到的第一个项目,其余的被丢弃。
关于python - 分解()时间序列 : ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60017052/
我正在尝试并行运行具有循环返回值的函数。但它似乎停留在 results = pool.map(algorithm_file.foo, population) 在 for 循环的第二次迭代中 r
Serving Flask 应用程序“服务器”(延迟加载) 环境:生产警告:这是一个开发服务器。不要在生产部署中使用它。请改用生产 WSGI 服务器。 Debug模式:开启 在 http://0.0.
我使用“product.pricelist”模型中的 get_product_price_rule() 函数。我的代码是: price = self._get_display_price(produ
我收到以下错误: Traceback (most recent call last): File "/home/odroid/trackAndFollow/getPositions.py", line
我正在尝试采用机器学习方法,但遇到了一些问题。这是我的代码: import sys import scipy import numpy import matplotlib import pandas
我尝试使用 tensorflow 1.4.0 对我的原始记录进行分类。过程如下。 拳头:读取图片和标签,输出“tfrecord”格式的文件。第二:读取tf记录和训练 编写tfrecord脚本是 !/u
我是新手,所以需要任何帮助,当我要求一个例子时,我的教授给我了这段代码,我希望有一个工作模型...... from numpy import loadtxt import numpy as np fr
我无法弄清楚为什么会出现此 ValueError...为了提供一些上下文,我正在使用 requests、BeautifulSoup 和 json 与 python 来抓取站点 json 数据。 我不确
我已经尝试使用这两个循环以及列表理解。即使我正在尝试将数字转换为列表中的整型,两者都无法解析整数。
我已经尝试使用这两个循环以及列表理解。即使我正在尝试将数字转换为列表中的整型,两者都无法解析整数。
我只有四个星期的 Python 经验。使用 Tkinter 创建一个工具,将新的公司 Logo 粘贴到现有图像之上。 下面的方法是获取给定目录中的所有图像并将新 Logo 粘贴到初始级别。现有图像、编
我只有四个星期的 Python 经验。使用 Tkinter 创建一个工具,将新的公司 Logo 粘贴到现有图像之上。 下面的方法是获取给定目录中的所有图像并将新 Logo 粘贴到初始级别。现有图像、编
我在尝试在 Keras 2.0.8、Python 3.6.1 和 Tensorflow 后端中训练模型时遇到问题。 错误消息: ValueError: Error when checking targ
我已经尝试使用这两个循环以及列表理解。即使我正在尝试将数字转换为列表中的整型,两者都无法解析整数。
我有这段代码: while True: try: start = int(input("Starting number: ")) fin = int(i
我是 python 的初学者编码员,试图制作一个“模具滚筒”,您可以在其中选择模具的大小,它在我的代码的第 20 行返回此错误 import sys import random import geto
我有以下代码: import fxcmpy import pandas as pd from pandas import datetime from pandas import DataFrame a
我正在尝试使用 django 和 python 制作一个博客应用程序。我也在尝试使用 s3 存储桶进行存储,使用 heroku 进行部署。我正在学习 coreymschafer 的在线教程。我正在按照
我创建了一个 numpy 数组(考虑输入数据)并想更改顺序(一些数值运算后的输出数据)。在使用转换后的数组时,我遇到错误并找到了根本原因。请在下面找到详细信息并使用 numpy 版本 1.19.1 i
我已经引用了之前的查询 All arguments should have the same length plotly但仍然没有得到我的问题的答案。 我有一个黄金价格数据集。 Date
我是一名优秀的程序员,十分优秀!