【问题标题】:How to combine multiple trained models into one and use it to predict?如何将多个训练好的模型合并为一个并用它来预测?
【发布时间】:2022-11-19 02:31:35
【问题描述】:

我有一个包含 100 行和 1000 多列的时间序列数据框。这些列彼此独立。我在每一列上运行 ARIMA 模型。所以,这就像运行 1000+ ARIMA 分析。

我编写了一段代码,循环遍历训练集的列,并根据提供的 p、d、q 参数在每一列上拟合 ARIMA 模型。虽然,看起来随着模型进一步训练列,它忘记了之前学到的东西,只使用最后训练的列的训练结果来预测测试。 (这导致预测在测试集上过度拟合)。

有没有一种方法可以将所有经过训练的模型的学习结合在一起,并用它来对我的测试集进行预测?

示例数据框如下所示:

date                        Col 1     Col 2     Col 3      Col 4
2001-07-21 10:00:00+05:00    45          51       31         3  
2001-07-21 10:15:00+05:00    46          50       32         3
2001-07-21 10:30:00+05:00    47          51       34         7
2001-07-21 10:45:00+05:00    50          50       33         9
2001-07-21 11:00:00+05:00    55          51       32         8
2001-07-21 11:15:00+05:00    52          73       34         11
2001-07-21 11:30:00+05:00    51          72       30         14

我实现的代码是:

#training set inclues all columns except the last and test set includes only last column.
train = df.iloc[:, :-1]
test = df.iloc[:,-1:]


order = (1,2,3) # <- plug-in p, d, q here 

for col in train.columns:
  model = ARIMA(train[col], order = order)  #training every column in training set
  model = model.fit()
model.summary()

predictions = model.predict(len(test))

【问题讨论】:

    标签: python pandas machine-learning model


    【解决方案1】:

    每次循环时,您都会创建一个新的 ARIMA 模型并将其拟合到您的列中。在你的最后一个循环之后,你只适合你的最后一列,然后你在你的测试列上进行预测。

    您必须将预测放在 for 循环中。

    for col in train.columns:
      model = ARIMA(train[col], order = order)  #training every column in training set
      model = model.fit()
      model.summary()
      predictions = model.predict(len(test))
      print(predictions)
    

    这将根据每一列的拟合情况为您提供预测。您可以将您的预测添加到列表中以跟踪它们并按照您的需要处理结果。

    predictions_lst = []
    for col in train.columns:
      ...
      predictions_lst.append(predictions)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2017-11-11
      • 1970-01-01
      • 2015-08-09
      • 2017-10-29
      • 2020-01-15
      • 1970-01-01
      相关资源
      最近更新 更多