-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_ARMA.py
71 lines (71 loc) · 3.31 KB
/
3_ARMA.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
# Модель авторегрессии — скользящего среднего
import csv
import numpy
import matplotlib.pylab as plt
from statsmodels.tsa.statespace.sarimax import SARIMAX
True_values = []
Predicted_values = []
Acc_list = []
# Выбор параметров p и q
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
# Исключение рядов длиной меньше 5
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
# Цикл с 4 итерациями для прогнозирования 4 значений
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')
# Модификация для интервала [0;1]
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.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)
# График фактических и прогнозируемых значений наблюдений
plt.plot(True_values, color='black', label = 'Original data')
plt.plot(Predicted_values, color='blue', label = 'Predicted data')
plt.legend(loc='best')
plt.title('Actual and predicted')
plt.show()