-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_MAIO.py
222 lines (175 loc) · 6.84 KB
/
12_MAIO.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import Sequential, Model
from tensorflow.keras.layers import Layer, Dense, Dropout, LSTM
from sklearn.utils import shuffle
from sklearn.preprocessing import MinMaxScaler
from tensorflow.compat.v1.keras.layers import CuDNNLSTM
import numpy as np
import random as rd
import tensorflow as tf
import pandas as pd
import io
from sklearn.model_selection import KFold
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.compat.v1.keras.layers import CuDNNLSTM
from tensorflow.keras import optimizers
df = pd.read_csv('Dataset_11Maio.csv', delimiter = ',',
encoding = 'ISO-8859-1')
# Ordenar por mês, dia e hora.
df.sort_values(['Month (number)', 'Day of month', 'Hour'],
ascending = [True, True, True], inplace = True)
# Separação por ruas.
df_1 = df[df['road_num'] == 2]
df_2 = df[df['road_num'] == 2]
df_3 = df[df['road_num'] == 3]
df_4 = df[df['road_num'] == 4]
df_1.drop('road_num', axis = 1, inplace = True)
'''
df_1.drop('Month (number)', axis = 1, inplace = True)
df_1.drop('Day of month', axis = 1, inplace = True)
df_1.drop('Hour', axis = 1, inplace = True)
df_1.drop('Day of week (name)', axis = 1, inplace = True)
df_1.drop('Distance', axis = 1, inplace = True)
df_1.drop('incident_category_desc', axis = 1, inplace = True)
'''
# Vamos tratar da rua 1.
#dataset = df_1_train.dropna(subset=["speed_diff"])
#dataset=dataset.reset_index(drop=True)
df_1=df_1.reset_index(drop=True)
'''training_set = df_1.iloc[:,4:5].values # Só contem valores do speed_diff
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_set)'''
'''
We will create a training set such that for every 7 days (7*24 hours) we will provide the next 24 hours
speed_diff as output. In other words, input for our RNN would be 7 days temperature data and
the output would be 1 day forecast of speed_diff
'''
x_train = []
y_train = []
n_future = 24 # next 4 days temperature forecast
n_past = 24*3 # Past 30 days
label = df_1['speed_diff']
for i in range(0,len(df_1)-n_past-n_future+1):
dias = df_1.iloc[i : i + n_past+24]
mes = dias.iloc[0]['Month (number)']
dia_1 = dias.iloc[0]['Day of month']
dia_168 = dias.iloc[24*3+1]['Day of month']
if i == 1020 or i==1900 :
print('Mes',dias.iloc[24*3+1]['Month (number)'])
print('DIA',dias.iloc[24*3+1]['Day of month'])
if (mes == 4 or mes == 6 or mes == 9 or mes == 11) and (dia_168 - dia_1 == 3 or dia_168 - dia_1 == -29):
a=df_1.iloc[i : i + n_past]
a.drop('Month (number)', axis = 1, inplace = True)
a.drop('Day of month', axis = 1, inplace = True)
a.drop('Hour', axis = 1, inplace = True)
a.drop('Day of week (name)', axis = 1, inplace = True)
a.drop('Distance', axis = 1, inplace = True)
a.drop('incident_category_desc', axis = 1, inplace = True)
x_train.append(a)
y_train.append(label.iloc[i + n_past : i + n_past + n_future ])
elif (mes == 1 or mes == 3 or mes == 5 or mes == 7 or mes == 8 or mes == 10 or mes == 12) and (dia_168 - dia_1 == 3 or dia_168 - dia_1 == -28):
a=df_1.iloc[i : i + n_past]
a.drop('Month (number)', axis = 1, inplace = True)
a.drop('Day of month', axis = 1, inplace = True)
a.drop('Hour', axis = 1, inplace = True)
a.drop('Day of week (name)', axis = 1, inplace = True)
a.drop('Distance', axis = 1, inplace = True)
a.drop('incident_category_desc', axis = 1, inplace = True)
x_train.append(a)
y_train.append(label.iloc[i + n_past : i + n_past + n_future ])
elif mes == 2 and (dia_168 - dia_1 == 3 or dia_168 - dia_1 == -26):
a=df_1.iloc[i : i + n_past]
a.drop('Month (number)', axis = 1, inplace = True)
a.drop('Day of month', axis = 1, inplace = True)
a.drop('Hour', axis = 1, inplace = True)
a.drop('Day of week (name)', axis = 1, inplace = True)
a.drop('Distance', axis = 1, inplace = True)
a.drop('incident_category_desc', axis = 1, inplace = True)
x_train.append(a)
y_train.append(label.iloc[i + n_past : i + n_past + n_future ])
'''x_train1=[]
y_train1=[]
for i in range(len(x_train)-1):
x_train1.append(x_train[i]+x_train[i+1])
y_train1.append(y_train[i]+y_train[i+1])
print(x_train1[0].size)
print(len(x_train1))
x_train=x_train1
y_train=y_train1'''
for i in range(len(x_train)):
x_train[i]=np.array(x_train[i])
for i in range(len(y_train)):
y_train[i]=np.array(y_train[i])
for i in range(len(x_train)):
x_train[i]=np.array(x_train[i])
for i in range(len(y_train)):
y_train[i]=np.array(y_train[i])
for i in range(len(x_train)):
x_train[i] = np.reshape(x_train[i], (x_train[0].shape[0],x_train[0].shape[1]) )
for i in range(len(y_train)):
y_train[i] = np.reshape(y_train[i], (y_train[0].shape[0]))
x_train=np.array(x_train)
y_train=np.array(y_train)
DADOS_TREINO=[]
DADOS_TESTE=[]
LABELS_TREINO=[]
LABELS_TESTE=[]
for i in range(len(x_train)):
if i >len(x_train)-8:
DADOS_TESTE.append(x_train[i])
LABELS_TESTE.append(y_train[i])
else:
DADOS_TREINO.append(x_train[i])
LABELS_TREINO.append(y_train[i])
x_train=DADOS_TREINO
y_train=LABELS_TREINO
x_test=DADOS_TESTE
y_test=LABELS_TESTE
x_train=np.array(x_train)
y_train=np.array(y_train)
x_test=np.array(x_test)
y_test=np.array(y_test)
print(x_train.shape)
scalers=[]
for i in range(11):
sc = MinMaxScaler(feature_range=(0,1))
x_train[:,i] = sc.fit_transform(x_train[:,i])
x_test[:,i] = sc.fit_transform(x_test[:,i])
scalers.append(sc)
sc1 = MinMaxScaler(feature_range=(0,1))
y_train = sc1.fit_transform(y_train)
def rmse(y_true, y_pred):
return tf.keras.backend.sqrt(tf.keras.backend.mean(tf.keras.backend.square(y_pred - y_true)))
regressor = Sequential()
regressor.add(CuDNNLSTM(units=24*3, return_sequences=True, input_shape = (24*3,11) ) )
regressor.add(Dropout(0.2))
regressor.add(CuDNNLSTM(24*3 , return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(CuDNNLSTM(24*3, return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(CuDNNLSTM(24*2))
regressor.add(Dropout(0.2))
regressor.add(Dense(24,activation='sigmoid'))
regressor.compile(optimizer='adam', loss='mean_squared_error',metrics=['acc'])
regressor.fit(x_train, y_train, epochs=1000 )
print('############################')
predicted_temperature = regressor.predict(x_test)
previstos=[]
for i in range(5):
k=predicted_temperature[i].reshape(1,24)
a = sc1.inverse_transform(k)
a = np.reshape(a,(a.shape[1],a.shape[0]))
previstos.append(a)
for i in range(5):
print('PREVISÃO DO DIA %d' %i)
for j in range(24):
print('Valor real na hora: ',j ,y_test[i][j],'Valor previsto: ',previstos[i][j][0])
print('-------------------------------------------------')
print('-------------------------------------------------')
print('-------------------------------------------------')