You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARIMA模型脚本运行报错:数组长度不一致问题求助

问题排查与修复:美国GDP预测ARIMA模型脚本报错

报错原因分析

报错信息ValueError: All arrays must be of the same length明确指出:构建DataFrame时,所有数组的元素数量不一致。统计各列长度后发现:

  • Date、GDP(valid)、Unemployment等列均包含44个元素
  • Inflation列仅43个元素,缺失2020年第四季度的数据

修复步骤

  1. 补充Inflation列缺失的最后一个数据点(示例用1.4,可替换为实际统计值)
  2. 修正预测逻辑:原数据无2021年外生变量数据,需手动提供预测期的外生变量假设值/实际预测值

修复后的完整代码

import pandas as pd
from statsmodels.tsa.arima.model import ARIMA

data = {
   "Date": ["01/01/2010", "01/04/2010", "01/07/2010", "01/10/2010" , "01/01/2011" , "01/04/2011" , "01/07/2011" , "01/10/2011" , "01/01/2012" , "01/04/2012" , "01/07/2012" , "01/10/2012" , "01/01/2013" , "01/04/2013" , "01/07/2013" , "01/10/2013" , "01/01/2014" , "01/04/2014" , "01/07/2014" , "01/10/2014" , "01/01/2015" , "01/04/2015" , "01/07/2015" , "01/10/2015" , "01/01/2016" , "01/04/2016" , "01/07/2016" , "01/10/2016" , "01/01/2017" , "01/04/2017" , "01/07/2017" , "01/10/2017" , "01/01/2018" , "01/04/2018" , "01/07/2018" , "01/10/2018" , "01/01/2019" , "01/04/2019" , "01/07/2019" , "01/10/2019" , "01/01/2020" , "01/04/2020" , "01/07/2020" , "01/10/2020" ],
   "GDP(valid)": [14.764, 14.98, 15.141, 15.309, 15.351, 15.557, 15.647, 15.842, 16.068, 16.207, 16.319, 16.42, 16.648, 16.728, 16.953, 17.192, 17.197, 17.518, 17.804, 17.912, 18.063, 18.279, 18.401, 18.435, 18.525, 18.711, 18.892, 19.089, 19.28, 19.438, 19.692, 20.037, 30.328, 20.58, 20.798, 20.917, 21.104, 21.384, 21.694, 22.092, 21.706, 19.913, 21.647, 22.024 ],
   "Unemployment": [4.85, 4.84, 4.836, 4.828, 4.82, 4.81, 4.8, 4.79, 4.78, 4.77, 4.76, 4.75, 4.74, 4.73, 4.72, 4.718, 4.71, 4.7, 4.696, 4.69, 4.683, 4.677, 4.67, 4.67, 4.657, 4.65, 4.64, 4.635, 4.628, 4.62, 4.61, 4.65, 4.6, 4.588, 4.58, 4.57, 4.56, 4.548, 4.536, 4.524, 4.51, 4.5, 4.49, 4.48 ],
   "Interest rate": [3.71, 3.49, 2.78, 2.86, 3.46, 3.21, 2.42, 2.04, 2.03, 1.82, 1.64, 1.7, 1.95, 2, 2.71, 2.74, 2.76, 2.62, 2.5, 2.28, 2, 2.16, 2.22, 2.19, 1.92, 1.75, 1.56, 2.13, 2.44, 2.26, 2.24, 2.37, 2.76, 2.92, 2.92, 3.03, 2.65, 2.33, 1.8, 1.79, 1.37, 0.68, 0.65, 0.86 ],
   "Import volume": [563.166, 588.05, 604.7, 619.5, 652.4, 674.9, 682.4, 688.3, 698.7, 696.6, 688.1, 670, 688, 688, 670, 694, 710, 723.3, 720.1, 722.4, 700, 697.3, 694.3, 680.06, 666.2, 674.9, 686.2, 693, 712.6, 730.35, 724.18, 754.28, 770.353, 773.616, 785.58, 791.5, 782.6, 785.9, 776.96, 760.26, 733.363, 612.47, 709.62, 757.57 ],
   "Export volume": [1759.9, 1819.7, 1877.2, 1972.1, 2040.4, 2116.1, 2156.8, 2150.1, 2190, 2215.6, 2227.7, 2237.5, 2255.9, 2261.4, 2285.1, 2349.2, 2343.6, 2395.2, 2399.2, 2376.2, 2299, 2300.4, 2260.7, 2222.3, 2182, 2222, 2270, 2268.4, 2339.7, 2349.7, 2384.5, 2479.1, 2517.3, 2562.2, 2535.5, 2537.4, 2544.1, 2545.4, 2532.4, 2531.9, 2416.1, 1811.3, 2106.6, 2266.4 ],
   "State. Expenditure": [3710.5, 3745.7, 3792.44, 3809.8, 3811.3, 3849.4, 3783.8, 3786.5, 3774.3, 3772.8, 3777.3, 3769.5, 3758.8, 3776, 3773.7, 3772.4, 3819.4, 3876, 3931.6, 3926.6, 3970.2, 3990.8, 4011.9, 4050.4, 4084.3, 4101.6, 4150.5, 4175.5, 4119.2, 4195.5, 4254, 4320.4, 4398.5, 4453.8, 4512.2, 4593.4, 4688.5, 4736.9, 4776.8, 4792, 4870.1, 8830, 7114.8, 5863.6 ],
   # 补充Inflation列缺失的2020Q4数据(示例值1.4,可替换为实际数据)
   "Inflation": [2.6, 2.2, 1.2, 1.2, 1.6, 3.2, 3.6, 3.5, 2.9, 2.3, 1.4, 2.2, 1.6, 1.1, 2, 1, 1.6, 2, 2, 1.7, -0.1, -0.2, 0.2, 0.2, 1.4, 1.1, 0.8, 1.6, 2.5, 2.2, 1.7, 2, 2.1, 2.5, 2.9, 2.5, 1.6, 2, 1.8, 2.5, 0.3, 1, 1.2, 1.4 ]
}

# 转换为时间序列格式,指定dayfirst确保日期解析正确
df = pd.DataFrame(data)
df["Date"] = pd.to_datetime(df["Date"], dayfirst=True)
df.set_index("Date", inplace=True)

# 构建带外生变量的ARIMA模型
p, d, q = 1, 1, 1  # 模型参数可根据后续调优调整
model = ARIMA(df["GDP(valid)"], order=(p, d, q), exog=df[["Unemployment", "Interest rate", "Import volume", "Export volume", "State. Expenditure", "Inflation"]])

# 拟合模型
model_fit = model.fit()

# 打印模型摘要,便于评估拟合效果
print(model_fit.summary())

# 2021年第一季度GDP预测
# 原数据无2021年外生变量,手动创建预测期的假设数据(需替换为实际预测值)
exog_forecast = pd.DataFrame({
    "Unemployment": [4.47],
    "Interest rate": [0.7],
    "Import volume": [760],
    "Export volume": [2300],
    "State. Expenditure": [6000],
    "Inflation": [1.5]
}, index=pd.date_range(start='2021-01-01', periods=1, freq='QS'))

forecast = model_fit.get_forecast(steps=1, exog=exog_forecast)

# 输出预测结果
forecast_df = pd.DataFrame(forecast.predicted_mean.values, index=exog_forecast.index, columns=["GDP Forecast"])
print("\n2021年第一季度GDP预测:")
print(forecast_df)

额外优化说明

  • 添加dayfirst=True参数,确保DD/MM/YYYY格式的日期被正确解析
  • 增加模型摘要打印,方便查看模型拟合统计指标
  • 修正预测逻辑:原代码试图从无2021年数据的原表中提取外生变量,改为手动创建预测期的外生变量数据

内容的提问来源于stack exchange,提问作者user22858166

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 22:27:02