【问题标题】:Use statsmodels model fit on another dataset在另一个数据集上使用 statsmodels 模型拟合
【发布时间】:2023-04-04 19:04:01
【问题描述】:

假设我使用来自statsmodels.tsa.statespace.sarimax 的 SARIMAX 在数据集 dataset1 上拟合模型 - 然后是否可以使用此拟合对另一个数据集 dataset2 进行预测?

也就是说,考虑以下几点:

from statsmodels.tsa.statespace.sarimax import SARIMAX
import pandas as pd
import numpy as np

# generate example data
n=90
idx = pd.PeriodIndex(pd.date_range(start = '2015-01-02',end='2015-04-01',freq='D'))
dat = np.sin(np.linspace(0,12*np.pi,n)) + np.random.randn(n)/10
dataset1 = pd.Series(dat, index = idx)

# fit model
fit = SARIMAX(dataset1, order = (1,0,1)).fit()
# make 30 day forecast on dataset1
fit.forecast(30)

我将如何使用fitdataset2 进行预测?

dat = np.sin(np.linspace(0,12*np.pi,n)) + np.random.randn(n)/10
dataset2 = pd.Series(dat, index = idx)

理想情况下,它应该是类似于fit(dataset2).forecast(30) 的超级简单的东西,但显然不是这样。

我知道我可以提取估计的参数fit.params,但没有经历这个繁琐的过程,有没有内置的方法或黑客来使用现有的fit 实例?

【问题讨论】:

    标签: python machine-learning statsmodels forecasting


    【解决方案1】:

    您可以使用apply结果方法:

    from statsmodels.tsa.statespace.sarimax import SARIMAX
    import pandas as pd
    import numpy as np
    
    # generate example data
    n=90
    idx = pd.PeriodIndex(pd.date_range(start = '2015-01-02',end='2015-04-01',freq='D'))
    dat = np.sin(np.linspace(0,12*np.pi,n)) + np.random.randn(n)/10
    dataset1 = pd.Series(dat, index = idx)
    
    # fit model
    fit = SARIMAX(dataset1, order = (1,0,1)).fit()
    # make 30 day forecast on dataset1
    fit.forecast(30)
    
    # ------------------------------------
    
    # get the new dataset
    dat = np.sin(np.linspace(0,12*np.pi,n)) + np.random.randn(n)/10
    dataset2 = pd.Series(dat, index = idx)
    
    # apply the parameters from `fit` to the new dataset
    fit2 = fit.apply(dataset2)
    # make 30 day forecast on dataset2
    fit2.forecast(30)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 2021-08-12
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多