![](/img/trans.png)
[英]Code seasonal_decompose from statsmodels with only native python
[英]Python seasonal_decompose function from Statsmodels library giving ValueError
我正在嘗試使用 Statsmodels 庫中的 seasonal_decompose function 來分解我的時間序列數據的季節性和趨勢,但我收到了 ValueError
數據:
A (Current average)
TS
2017-12-01 00:01:00 3.274965
2017-12-01 00:02:00 3.274083
2017-12-01 00:03:00 3.262563
2017-12-01 00:04:00 3.278352
2017-12-01 00:05:00 3.251769
數據索引:
ts_log.index
output:
DatetimeIndex(['2017-12-01 00:01:00', '2017-12-01 00:02:00',
'2017-12-01 00:03:00', '2017-12-01 00:04:00',
'2017-12-01 00:05:00', '2017-12-01 00:06:00',
'2017-12-01 00:07:00', '2017-12-01 00:08:00',
'2017-12-01 00:09:00', '2017-12-01 00:10:00',
...
'2018-01-04 23:26:00', '2018-01-04 23:27:00',
'2018-01-04 23:28:00', '2018-01-04 23:29:00',
'2018-01-04 23:30:00', '2018-01-04 23:31:00',
'2018-01-04 23:32:00', '2018-01-04 23:33:00',
'2018-01-04 23:34:00', '2018-01-04 23:35:00'],
dtype='datetime64[ns]', name='TS', length=50000, freq=None)
分解季節性
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(ts_log)
錯誤:
ValueError Traceback (most recent call last)
<ipython-input-79-7ca5a90bdbf8> in <module>()
1 from statsmodels.tsa.seasonal import seasonal_decompose
----> 2 decomposition = seasonal_decompose(ts_log)
3
4 trend = decomposition.trend
5 seasonal = decomposition.seasonal
C:\Users\Paras Mani\Anaconda2\envs\py3\lib\site-packages\statsmodels\tsa\seasonal.py in seasonal_decompose(x, model, filt, freq, two_sided)
82 freq = pfreq
83 else:
---> 84 raise ValueError("You must specify a freq or x must be a "
85 "pandas object with a timeseries index with"
86 "a freq not set to None")
ValueError: You must specify a freq or x must be a pandas object with a timeseries index witha freq not set to None
這里我使用時間序列索引,但頻率設置為無。 我怎樣才能改變頻率。
如果您有 pandas dataframe 和DateTimeIndex
,您可以檢查其頻率屬性。
print(df.index.freq)
如果這返回None
,您可以通過直接分配給df.index.freq
屬性或使用df.asfreq()
方法來設置頻率。 例如。
# Set df freq to 120 min
df.index.freq = '120t'
df.asfreq('120t')
使用df.asfreq()
將允許您輕松地在頻率之間重新采樣/轉換並處理重新采樣。 您可以在此處找到有效偏移別名列表(代碼的“t”部分)。 或者您可以運行pd.tseries.offsets.__all__
以僅獲取有效偏移量的列表(注意,這不會打印別名,因此您會看到Minute
而不是min
或t
)。
一旦您的 dataframe 具有有效時間,您就可以運行 seasonal_decompose()。 從statsmodels
0.12.2 中的實驗來看,頻率超過 1 小時即freq
< '60T'
導致ValueError: freq T not understood. Please report if you think this is in error.
ValueError: freq T not understood. Please report if you think this is in error.
被拋出。 如果這意味着您需要對df
進行下采樣,我建議您按照以下df.resample('1H').median()
方式使用 as 方法。
嘗試這個:
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(x, model='additive', filt=None, freq=52)
fig = decomposition.plot()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.