python - ARIMA 模型的逆平稳性

标签 python pandas matplotlib statsmodels

如何反转平稳性并将日期重新应用于数据以进行绘图?

源代码:

我正在尝试反转平稳性并获得预测图,特别是对于名为“app_1”和“app_2”的两列(下面的橙色和红色线)。

我从中提取的数据如下所示: plotted data set

print(u1.info())
u1.head()

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 15011 entries, 2017-08-28 11:00:00 to 2018-01-31 19:30:00
Freq: 15T
Data columns (total 10 columns):
 app_1        15011 non-null float64
 app_2        15011 non-null float64
user          15011 non-null object
 bar          15011 non-null float64
 grocers      15011 non-null float64
 home         15011 non-null float64
 lunch        15011 non-null float64
 park         15011 non-null float64
 relatives    15011 non-null float64
 work         15011 non-null float64
dtypes: float64(9), object(1)
memory usage: 1.3+ MB

app_1   app_2   user    bar grocers home    lunch   park    relatives   work
date                                        
2017-08-28 11:00:00 0.010000    0.0 user_1  0.0 0.0 0.0 0.0 0.0 0.0 0.0
2017-08-28 11:15:00 0.010125    0.0 user_1  0.0 0.0 0.0 0.0 0.0 0.0 0.0
2017-08-28 11:30:00 0.010250    0.0 user_1  0.0 0.0 0.0 0.0 0.0 0.0 0.0
2017-08-28 11:45:00 0.010375    0.0 user_1  0.0 0.0 0.0 0.0 0.0 0.0 0.0
2017-08-28 12:00:00 0.010500    0.0 user_1  0.0 0.0 0.0 0.0 0.0 0.0 0.0

位置列表示用户在给定时间所处的位置 - 在第一个“重大位置更改”事件之后,每次只有一列为 1。

我正在使用 VARIMAX 进行分析——使用 AR 的 statsmodels VARMAX 版本。:

from statsmodels.tsa.statespace.varmax import VARMAX
import pandas as pd
import numpy as np

%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt

from random import random
#...

columns = [ ' app_1', ' app_2', ' bar', ' grocers', ' home', ' lunch', ' work', ' park', ' relatives' ]
series = u1[columns]

# from: https://machinelearningmastery.com/make-predictions-time-series-forecasting-python/
# create a difference transform of the dataset
def difference(dataset):
    diff = list()
    for i in range(1, len(dataset)):
        value = dataset[i] - dataset[i - 1]
        diff.append(value)
    return np.array(diff)

# Make a prediction give regression coefficients and lag obs
def predict(coef, history):
    yhat = coef[0]
    for i in range(1, len(coef)):
        yhat += coef[i] * history[-i]
    return yhat

X = pd.DataFrame()
for column in columns:
    X[column] = difference(series[column].values)

size = (4*24)*54 # hoping
train, test = X[0:size], X[size:size+(14*4*24)]

train = train.loc[:, (train != train.iloc[0]).any()] # https://stackoverflow.com/questions/20209600/panda-dataframe-remove-constant-column
test = test.loc[:, (test != test.iloc[0]).any()] # https://stackoverflow.com/questions/20209600/panda-dataframe-remove-constant-column

#print(train.var(), X.info())

# train autoregression
model = VARMAX(train)
model_fit = model.fit(method='powell', disp=False)
#print(model_fit.mle_retvals)

##window = model_fit.k_ar
coef = model_fit.params

# walk forward over time steps in test
history = [train.iloc[i] for i in range(len(train))]
predictions = list()
for t in range(len(test)):
    yhat = predict(coef, history)
    obs = test.iloc[t]
    predictions.append(yhat)
    history.append(obs) 

print(mean_squared_error(test, predictions))

0.5594208989876831

scikitlearn 的mean_squared_error 并不可怕(事实上,它大约位于文档中显示的三个示例的中间)。这可能意味着数据具有预测性。我希望在情节中看到这一点。

# plot
plt.plot(test)
plt.plot(predictions, color='red')
plt.show()

plot of predictions

因此,这里发生的部分情况是数据是季节性的,因此必须对其应用平稳性。现在这些线都是垂直的,而不是时间的。

但我关心的另一件事是红色数据的规模。那是很多红色。无论如何,如何反转平稳性并将日期重新应用于数据以进行绘图?显然不应该是这样的。 :)

最佳答案

执行此操作的方法是,首先将其放入数据框:

predDf = pd.DataFrame(predictions)

关于python - ARIMA 模型的逆平稳性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525734/

相关文章:

python - 恢复 Latex 编译错误

Python (Matplotlib) - 三元图上的刻度线

python - 如何使子图的大小相等?

python - 从 .txt 文件加载页面内容会破坏 Django 模板系统

Python3 - 而 ids > 停止 : TypeError: unorderable types: str()> int()

python - Pandas 根据条件将列添加到具有另一行值的数据框

Python Pandas 垂直连接

python - numpy.array_split() 奇怪的行为

python - 如何使用 pandas 数据框获取日期范围箱线图

Python 获取 future (x) 天和剩余小时数的日期?