python - 通过宽数据格式的多列循环python auto_arima

标签 python pandas loops time-series arima

首先我要说我绝不是 Python 专家,但我当前的项目要求它是用 Python 编程的,所以任何帮助都将不胜感激。 我拥有的是一个转换后的时间序列,其中包含每月数据(30 个月)和 1000 多个项目。

我希望为这些列中的每一列运行 arima。他们彼此不依赖。从本质上讲,这就像运行 1000 个独立的 Arima 分析。

我通过为每个项目创建一个数据框列表并使用 R 的 auto arima 函数循环遍历该列表,在 R 中编写了此功能。它缓慢而笨拙,但完成了工作。

在 Python 中完成我没有找到创建此结构并使其可行的方法。 相反,我找到了一些代码并试图从中创建一个循环。 现在,auto_arima 在此运行,但它覆盖了结果,我真的不知道如何使它可行。

我需要运行 auto_arima,因为这些项目具有各自的最佳 P、D、Q 参数。

X是数据,结构是:index,item1,item2,item3...itemn

dict_org = {}
dict_pred = {}

for col in X:
    size = int(len(X) * 0.70)
    train, testdata = X[0:size], X[size:len(X)]
    history = [x for x in train[column]]
    predictions = list()

    for column in testdata:
        model = pm.auto_arima(history, start_p=1, start_q=1,
                      test='adf',       # use adftest to find optimal 'd'
                      max_p=3, max_q=3, # maximum p and q
                      m=1,              # frequency of series
                      d=None,           # let model determine 'd'
                      seasonal=False,   # No Seasonality
                      start_P=0, 
                      D=0, 
                      trace=True,
                      error_action='ignore',  
                      suppress_warnings=True, 
                      stepwise=True) # this works 

        output = model.predict()

        yhat = output[0]
        predictions.append(yhat)
        obs = testdata[column]
        history.append(obs)
        print("Predicted:%f, expected:%f" %(yhat, obs))

        error = mean_squared_error(testdata, predictions[:len(testdata)])
    print('Test MSE: %.3f' % error)

    dict_org.update({X[col]: testdata})
    dict_pred.update({X[col]: predictions})

    print("Item: ", X[col], "Test MSE:%f"% error)

我想要得到的是所有项目和预测的字典,类似于我通过将 R 的 auto arima 传递给数据框列表得到的结果。我现在不断更新 yhat 作为 1 个观察结果,我不知所措。

非常感谢您的帮助。

最佳答案

您现在可能已经找到了解决方案,但我会留下答案以防其他人偶然发现。

auto_arima 不是模型本身。这是一个帮助定位最佳模型订单的功能。在上述情况下,您要做的是为其分配一个变量并访问订单和季节性订单,以及最佳模型的 AIC。您可以创建一个小函数来执行这部分,然后将输出用于实际模型。

def find_orders(ts):

    stepwise_model = pm.auto_arima(history, start_p=1, start_q=1,
                      test='adf',       # use adftest to find optimal 'd'
                      max_p=3, max_q=3, # maximum p and q
                      m=1,              # frequency of series
                      d=None,           # let model determine 'd'
                      seasonal=False,   # No Seasonality
                      start_P=0, 
                      D=0, 
                      trace=True,
                      error_action='ignore',  
                      suppress_warnings=True, 
                      stepwise=True) # this works 

    return stepwise_model.order, stepwise_model.seasonal_order

然后,您可以为建模部分创建另一个函数 - 假设您将其称为 fit_arima - 并为循环中的每个时间序列传递模型中的订单和季节性订单。

for column in testdata:
        order, seasonal_order = find_orders(ts)
        fit_arimax(ts, order=order, seasonal_order=seasonal_order)

希望对您有所帮助!

关于python - 通过宽数据格式的多列循环python auto_arima,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57388352/

相关文章:

php - 使用 foreach 根据另一个查询更新 mysql 数据库中的字段

c - 如何遍历给定的整数输入,并给出从最大到最小数字的输出?

python - Python中多重处理后的后处理结果

javascript - 在 Flask 应用上获取日期时间数据时出错

python - 在 MacOS Catalina 升级后部署 Google Dataflow 模板时,Protobuf 的数据库错误中已存在文件

python - 如何在乘以 pandas DataFrames 时将 NaN 或非对齐值视为 1 或 0

javascript - 如何检查充满 Jquery 对象的数组的值

python - Scrapy - 数据库选择

python - 根据另一个数据帧 pandas 的列中的值替换一个数据帧的行中的值

python - matplotlib seaborn 长行名影响其他子图的轴