freq参数未在seasonal_decomposition函数中出现



假设我们有如下代码:

import yfinance as yf
import matplotlib.pyplot as plt
data =  yf.download(tickers="FB", start="2016-1-1",end="2022-05-15",progress=False)
data.drop(["Open","Low","Close","Adj Close","Volume"],axis=1,inplace=True)
data.sort_index(inplace=True)
print(data.head())
from statsmodels.tsa.seasonal import  seasonal_decompose
result =seasonal_decompose(data["High"],model='multiplicative',freq=36)
estimated_trend_add = result.trend
estimated_seasonal_add = result.seasonal
estimated_residual_add = result.resid
fig, axes = plt.subplots(4, 1)
fig.set_figheight(10)
fig.set_figwidth(15)
axes[0].plot(data['High'], label='Original')
axes[0].legend(loc='upper left')
axes[1].plot(estimated_trend_add, label='Trend')
axes[1].legend(loc='upper left')
axes[2].plot(estimated_seasonal_add, label='Cyclic')
axes[2].legend(loc='upper left')
axes[3].plot(estimated_residual_add, label='Residuals')
axes[3].legend(loc='upper left')
plt.show()

dataframe的结果如下:

High
Date                  
2015-12-31  106.169998
2016-01-04  102.239998
2016-01-05  103.709999
2016-01-06  103.769997
2016-01-07  101.430000

还返回以下错误:

Traceback (most recent call last):
File "C:UsersUserPycharmProjectsDataSciencefacebook_real_prices.py", line 8, in <module>
result =seasonal_decompose(data["High"],model='multiplicative',freq=36)
TypeError: seasonal_decompose() got an unexpected keyword argument 'freq'

我知道有关键字周期,它们是一样的吗?通常我们知道周期是1/频率,但在这种情况下,我该如何定义周期?或者如何创建频率索引?请帮帮我

seasonal_decomposition中的period参数是周期性,即事情开始重复多长时间后。

例如,频率为1小时的时间序列数据的周期为24。如果频率为3小时,周期为8 =(24/3)。

在你的例子中,频率似乎是工作日(星期一至星期五),这意味着周期为5。

是的,似乎周末和节假日都被取消了。但asfreq方法可用于前填/回填空白。指定周期为5意味着您希望值(例如星期一)与

有些相似。如果输入数据是带有日期时间索引的pandas.Series,则可能不需要指定周期:

data = data.asfreq(pd.offsets.BDay(), method="pad")

但要注意,statsmodels.tsa.tsatools.freq_to_periodseasonal_decompose内部使用的函数忽略频率刻度,即3H和H都被视为H。您可以使用以下命令检查:

from statsmodels.tsa.tsatools import freq_to_period
assert freq_to_period("H") == freq_to_period("3H") == 24
我建议直接在seasonal_decompose(本例中为5)中指定period参数。

最新更新