首页 > 解决方案 > 带有熊猫数据框的 ARIMA 模型

问题描述

我有以下数据集test_1

Date    Frequency
0   2020-01-20  10
1   2020-01-21  2
2   2020-01-22  1
3   2020-01-23  10
4   2020-01-24  6
... ... ...
74  2020-04-04  7
75  2020-04-05  9
76  2020-04-06  8
77  2020-04-07  6
78  2020-04-08  1

其中Frequency是按日期计算的用户频率列。

我想预测未来的趋势,为此我正在考虑使用 ARIMA 模型。我用过这段代码

# fit model
model = ARIMA(test_1, order=(5,1,0))
model_fit = model.fit(disp=0)
print(model_fit.summary())
# plot residual errors
residuals = DataFrame(model_fit.resid)
residuals.plot()
pyplot.show()
residuals.plot(kind='kde')
pyplot.show()
print(residuals.describe())

但我有这个错误:ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).

由于model = ARIMA(test_1, order=(5,1,0)).

你知道这意味着什么以及我该如何解决它吗?

标签: pythonpandastime-seriesarima

解决方案


此错误表明ARIMA需要一个类似数组的对象,但您已经传递了 a DataFrame

这可以通过传递test_1["Frequency"]而不是 just来解决test_1。另外,我将修复我在您的代码中遇到的其他一些问题:

import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
import matplotlib.pyplot as pyplot

# fit model
model = ARIMA(test_1["Frequency"], order=(5,1,0)) #<--- change this
model_fit = model.fit(disp=0)
print(model_fit.summary())
# plot residual errors
residuals = pd.DataFrame(model_fit.resid)
residuals.plot(kind='kde')
print(residuals.describe())
pyplot.show()

推荐阅读