-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathARIMA.py
60 lines (48 loc) · 1.86 KB
/
ARIMA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import util
import eval
import numpy as np
import pyflux as pf
if __name__ == '__main__':
p = 6
q = 4
h_test = 1
lag = 24
ts, data = util.load_data("./data/NSW2013.csv", columnName="TOTALDEMAND")
# ts, data = util.load_data("./data/bike_hour.csv", columnName="cnt")
# ts, data = util.load_data("./data/TAS2016.csv", columnName="TOTALDEMAND")
# ts, data = util.load_data("./data/traffic_data_in_bits.csv", columnName="value")
# ts, data = util.load_data("./data/beijing_pm25.csv", columnName="pm2.5")
# ts, data = util.load_data("./data/pollution.csv", columnName="Ozone")
train, test = util.divideTrainTest(data)
testX, testY = util.createSamples(test, lookBack=24, RNN=False)
testX = testX[:3]
print("train shape is", train.shape)
print("test shape is", test.shape)
train_data = [x[0] for x in train]
predictions = []
realTestY = []
for t in range(len(testX)):
add_test = [x for x in testX[t]]
raw_train_data = train_data
train_data.extend(add_test)
model = pf.ARIMA(data=np.array(train_data), ar=p, ma=q, family=pf.Normal())
model.fit(method="MLE")
output = model.predict(h_test, intervals=False)
yhat = output.values[0][0]
predictions.append(yhat)
obs = test[t]
realTestY.append(obs)
train_data = raw_train_data
print('t:%d, predicted=%f, expected=%f' % (t, yhat, obs))
realTestY = np.array(test).reshape(-1, 1)
predictions = np.array(predictions).reshape(-1, 1)
MAE = eval.calcMAE(realTestY, predictions)
RMSE = eval.calcRMSE(realTestY, predictions)
MAPE = eval.calcSMAPE(realTestY, predictions)
print('Test MAE: %.8f' % MAE)
print('Test RMSE: %.8f' % RMSE)
print('Test MAPE: %.8f' % MAPE)
# plot
# pyplot.plot(test)
# pyplot.plot(predictions, color='red')
# pyplot.show()