简体   繁体   English

Statsmodels arima模型返回错误

[英]Statsmodels arima model returns error

I have started to learn statsmodels package and can't implement basic forecasting with arima. 我已经开始学习statsmodels包,无法用arima实现基本的预测。

Error is 错误是

ValueError: Given a pandas object and the index does not contain dates ValueError:给定一个pandas对象,索引不包含日期

I am trying this as a version: 我正在尝试这个版本:

df = make_df(filename_data)

y = []
x = []

# here I am preparing day by day sequence as that I have inconsistent data and I set 0 to NAN values

start_date = df[date_col].min()
end_date = df[date_col].max()



while start_date <= end_date:

    x.append(start_date)

    try:
        y.append(
            df[df[date_col] == start_date][rev_col].values[0])
    except:
        y.append(0)

    start_date += datetime.timedelta(days=1)

y = np.array(y)
x = np.array(x)

y = pd.TimeSeries(y, index=x)
print(y)
arma_mod = sm.tsa.ARMA(y, order=(2,2))
arma_res = arma_mod.fit(trend='nc', disp=-1)

Before that I tried 在此之前我尝试过

df = make_df(filename_data)

y = np.array(df[rev_col])
x = np.array(df[date_col])

y = pd.TimeSeries(y, index=x)

Why is it happening? 为什么会这样?

The date - revenue data looks OK: 日期 - 收入数据看起来不错:

2014-08-04      59477
2014-08-05      29989
2014-08-06      29989
2014-08-07     116116

You can simply transform your DataFrame with as_matrix(). 您可以使用as_matrix()简单地转换DataFrame。

Example working code: 工作代码示例:

from statsmodels.tsa.arima_model import ARIMA
import numpy as np

def plot_residuals(data, ord=(2, 0, 1)):
    model = ARIMA(endog=data, order=(ord[0], 0, ord[1])).fit()
    plt.plot(model.resid)
    plt.show()

data = np.log(data.values) - np.log(data.values.shift()).to_frame().dropna().as_matrix()
plot_residuals(data, (2, 0, 1))

As statsmodels has many unresolved issues, it will help you only temporarily. 由于statsmodels有许多未解决的问题,它只能暂时帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM