-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-5.py
154 lines (154 loc) · 6.22 KB
/
3-5.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import csv
import numpy
import matplotlib.pylab as plt
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.vector_ar.var_model import VAR
True_values = []
Predicted_values_1 = []
Acc_list = []
p = 1
q = 1
for num in range(1146):
with open('Time_Series/item' + str(1213 + num) + '.csv') as f:
reader = csv.reader(f)
month_quantity = 0
for row in reader:
month_quantity += 1
if month_quantity > 4:
with open('Time_Series/item' + str(1213 + num) + '.csv') as f:
reader = csv.reader(f)
data = []
counter = 0
for row in reader:
if 0 < counter <= month_quantity - 1:
data.append(float(row[2]))
counter += 1
for i in [3, 2, 1, 0]:
try:
model = SARIMAX(data[0:len(data) - i - 1], order=(p, 0, q))
model_fit = model.fit(disp=False)
prediction = model_fit.predict(len(data[0:len(data) - i - 1]),
len(data[0:len(data) - i - 1]), typ='levels')
if prediction[0] > 1:
prediction[0] = float(1)
if prediction[0] < 0:
prediction[0] = float(0)
True_values.append(data[len(data) - i - 1])
Predicted_values_1.append(prediction[0])
if float(data[len(data) - i - 1]) == 0 and prediction == 0:
acc = 1
else:
acc = min(data[len(data) - i - 1], prediction) / max(data[len(data) - i - 1], prediction)
Acc_list.append(acc)
data[len(data) - i - 1] = prediction[0]
except ZeroDivisionError:
Acc_list.append(0)
continue
except numpy.linalg.LinAlgError:
Acc_list.append(0)
continue
except IndexError:
Acc_list.append(0)
continue
Average_Acc = sum(Acc_list)/len(Acc_list)
print(Average_Acc)
Predicted_values_2 = []
Acc_list = []
p = 0
d = 1
q = 3
for num in range(1146):
with open('Time_Series/item' + str(1213 + num) + '.csv') as f:
reader = csv.reader(f)
month_quantity = 0
for row in reader:
month_quantity += 1
if month_quantity > 4:
with open('Time_Series/item' + str(1213 + num) + '.csv') as f:
reader = csv.reader(f)
data = []
counter = 0
for row in reader:
if 0 < counter <= month_quantity - 1:
data.append(float(row[2]))
counter += 1
for i in [3, 2, 1, 0]:
try:
model = SARIMAX(data[0:len(data) - i - 1], order=(p, d, q))
model_fit = model.fit(disp=False)
prediction = model_fit.predict(len(data[0:len(data) - i - 1]),
len(data[0:len(data) - i - 1]), typ='levels')
if prediction[0] > 1:
prediction[0] = float(1)
if prediction[0] < 0:
prediction[0] = float(0)
Predicted_values_2.append(prediction[0])
if float(data[len(data) - i - 1]) == 0 and prediction == 0:
acc = 1
else:
acc = min(data[len(data) - i - 1], prediction) / max(data[len(data) - i - 1], prediction)
Acc_list.append(acc)
data[len(data) - i - 1] = prediction[0]
except ZeroDivisionError:
Acc_list.append(0)
continue
except numpy.linalg.LinAlgError:
Acc_list.append(0)
continue
except IndexError:
Acc_list.append(0)
continue
Average_Acc = sum(Acc_list)/len(Acc_list)
print(Average_Acc)
Predicted_values_3 = []
Acc_list = []
number_of_series = 2
p = 1
for num in range(1146):
with open('Time_Series/item' + str(1213 + num) + '.csv') as f:
reader = csv.reader(f)
month_quantity = 0
for row in reader:
month_quantity += 1
if month_quantity > 4:
with open('Time_Series/item' + str(1213 + num) + '.csv') as f:
reader = csv.reader(f)
data = []
counter = 0
for row in reader:
if 0 < counter <= month_quantity - 1:
x = []
for i in range(number_of_series):
x.append(float(row[2]))
data.append(x)
counter += 1
for i in [3, 2, 1, 0]:
try:
model = VAR(data[0:len(data) - i - 1])
model_fit = model.fit(maxlags=p)
prediction = model_fit.forecast(model_fit.y, steps=1)
if prediction[0][0] > 1:
prediction[0][0] = float(1)
if prediction[0][0] < 0:
prediction[0][0] = float(0)
Predicted_values_3.append(prediction[0][0])
if float(data[len(data) - i - 1][0]) == 0 and prediction[0][0] == 0:
acc = 1
else:
acc = min(data[len(data) - i - 1][0], prediction[0][0]) / max(data[len(data) - i - 1][0],
prediction[0][0])
Acc_list.append(acc)
for j in range(2):
data[len(data) - i - 1][j] = prediction[0][0]
except ValueError:
Acc_list.append(0)
continue
Average_Acc = sum(Acc_list)/len(Acc_list)
print(Average_Acc)
plt.plot(True_values[0:100], color='black', label = 'Original data')
plt.plot(Predicted_values_1[0:100], color='blue', label = 'ARMA')
plt.plot(Predicted_values_2[0:100], color='green', label = 'ARIMA')
plt.plot(Predicted_values_3[0:100], color='yellow', label = 'VAR')
plt.legend(loc='best')
plt.title('Actual and predicted')
plt.show()