2nguyenle3
commited on
Commit
•
83b406a
1
Parent(s):
f426d78
Server for using model
Browse files- aiair-server/.gitignore +2 -0
- aiair-server/README.md +13 -0
- aiair-server/app.py +19 -0
- aiair-server/controllers/PredictController.py +2035 -0
- aiair-server/datasets/models/lstm/bi-lstm.json +1 -0
- aiair-server/datasets/models/lstm/bi_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/co-lstm.json +1 -0
- aiair-server/datasets/models/lstm/co2-lstm.json +1 -0
- aiair-server/datasets/models/lstm/co2_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/co_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/humi-lstm.json +1 -0
- aiair-server/datasets/models/lstm/humi_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/pm25-lstm.json +1 -0
- aiair-server/datasets/models/lstm/pm25_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/temp-lstm.json +1 -0
- aiair-server/datasets/models/lstm/temp_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/temp_pc_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/temp_pc_lstm_weight.json +1 -0
- aiair-server/datasets/models/lstm/test-lstm.json +1 -0
- aiair-server/datasets/models/lstm/test_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/trick-lstm.json +1 -0
- aiair-server/datasets/models/lstm/trick_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/lstm/uv-lstm.json +1 -0
- aiair-server/datasets/models/lstm/uv_lstm_weight.h5 +3 -0
- aiair-server/datasets/models/prophet-lstm/temp-bpnn-model.h5 +3 -0
- aiair-server/datasets/models/prophet-lstm/temp-bpnn-model.json +1 -0
- aiair-server/requirements.txt +10 -0
- aiair-server/routes/Predict.py +49 -0
- aiair-server/routes/Router.py +6 -0
aiair-server/.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
__pycache__/
|
2 |
+
|
aiair-server/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Server: Web Monitoring Application
|
2 |
+
|
3 |
+
|
4 |
+
## General ##
|
5 |
+
|
6 |
+
- **Server** :
|
7 |
+
|
8 |
+
## Technologies ##
|
9 |
+
|
10 |
+
| Server |
|
11 |
+
| ------ |
|
12 |
+
| [plugins/flask/README.md](https://github.com/pallets/flask) |
|
13 |
+
| [plugins/pandas/README.md](https://github.com/pandas-dev/pandas) |
|
aiair-server/app.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask
|
2 |
+
from config import HOST, PORT, DEBUG
|
3 |
+
from routes.Router import Router
|
4 |
+
from flask import Flask
|
5 |
+
from flask_cors import CORS
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
cors = CORS(app, resources={r"/*": {"origins": "*"}})
|
9 |
+
|
10 |
+
|
11 |
+
@app.route('/')
|
12 |
+
def index():
|
13 |
+
return '<h1>REST API successfully running</h1>'
|
14 |
+
|
15 |
+
|
16 |
+
Router.run(app)
|
17 |
+
|
18 |
+
if __name__ == '__main__':
|
19 |
+
app.run(host='0.0.0.0', port='5000', debug=True)
|
aiair-server/controllers/PredictController.py
ADDED
@@ -0,0 +1,2035 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import request, jsonify
|
2 |
+
import numpy as np
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
|
6 |
+
from xgboost import XGBRegressor
|
7 |
+
from prophet import Prophet
|
8 |
+
from sklearn.preprocessing import MinMaxScaler
|
9 |
+
from sklearn.linear_model import LinearRegression
|
10 |
+
from keras.models import model_from_json
|
11 |
+
|
12 |
+
import warnings
|
13 |
+
warnings.filterwarnings('ignore')
|
14 |
+
|
15 |
+
RANDOM_SEED = 42
|
16 |
+
np.random.seed(RANDOM_SEED)
|
17 |
+
|
18 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
19 |
+
server_dir = os.path.dirname(os.path.dirname(script_dir))
|
20 |
+
|
21 |
+
p_gb = {'n_estimators': 500, 'max_depth': 10, 'min_samples_split': 2,'learning_rate': 0.09, 'loss': 'squared_error', 'random_state': RANDOM_SEED}
|
22 |
+
p_xgb = {'n_estimators': 700, 'max_depth': 12, 'learning_rate': 0.09, 'random_state': RANDOM_SEED}
|
23 |
+
p_rf = {'n_estimators': 1000, 'max_depth': 10, 'random_state': RANDOM_SEED}
|
24 |
+
p_knn = {'n_neighbors': 3}
|
25 |
+
|
26 |
+
class PredictController:
|
27 |
+
#-------------------Prophet-LSTM-------------------
|
28 |
+
def predictTempProphetLSTM():
|
29 |
+
if request.method == 'POST':
|
30 |
+
try:
|
31 |
+
data = request.json
|
32 |
+
objectFormat = data['dataTemp']
|
33 |
+
|
34 |
+
# push data to array
|
35 |
+
tempTime = []
|
36 |
+
for i in objectFormat['time']:
|
37 |
+
tempTime.append(i)
|
38 |
+
|
39 |
+
tempData = []
|
40 |
+
for i in objectFormat['value']:
|
41 |
+
tempData.append(i)
|
42 |
+
|
43 |
+
arrayData = np.array(tempData)
|
44 |
+
arrayTime = np.array(tempTime)
|
45 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
46 |
+
|
47 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
48 |
+
dataset = dataset.set_index('ds')
|
49 |
+
dataset = dataset.resample('5T').ffill()
|
50 |
+
dataset = dataset.dropna()
|
51 |
+
dataset = dataset.iloc[1:]
|
52 |
+
dataset.reset_index(inplace=True)
|
53 |
+
|
54 |
+
scaler = MinMaxScaler()
|
55 |
+
scaled_temp = scaler.fit_transform(dataset[['y']])
|
56 |
+
sequence_length = 12
|
57 |
+
if len(scaled_temp) < sequence_length:
|
58 |
+
padded_temp = np.pad(scaled_temp, ((sequence_length - len(scaled_temp), 0), (0, 0)), mode='constant')
|
59 |
+
else:
|
60 |
+
padded_temp = scaled_temp[-sequence_length:]
|
61 |
+
input_data = padded_temp.reshape((1, 1, sequence_length))
|
62 |
+
|
63 |
+
# Load model LSTM
|
64 |
+
temp_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/test-lstm.json')
|
65 |
+
temp_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/test_lstm_weight.h5')
|
66 |
+
with open(temp_lstm_json, 'r') as json_file:
|
67 |
+
loaded_model_json_lstm = json_file.read()
|
68 |
+
|
69 |
+
loaded_model_lstm = model_from_json(loaded_model_json_lstm)
|
70 |
+
loaded_model_lstm.load_weights(temp_lstm_weight)
|
71 |
+
|
72 |
+
# Load model BPNN (json and h5)
|
73 |
+
temp_bpnn_json = os.path.join(server_dir, 'aiair-server/datasets/models/prophet-lstm/temp-bpnn-model.json')
|
74 |
+
temp_bpnn_weight = os.path.join(server_dir, 'aiair-server/datasets/models/prophet-lstm/temp-bpnn-model.h5')
|
75 |
+
with open(temp_bpnn_json, 'r') as json_file:
|
76 |
+
loaded_model_json_bpnn = json_file.read()
|
77 |
+
|
78 |
+
loaded_model_bpnn = model_from_json(loaded_model_json_bpnn)
|
79 |
+
loaded_model_bpnn.load_weights(temp_bpnn_weight)
|
80 |
+
|
81 |
+
if os.path.exists(temp_lstm_weight) and os.path.exists(temp_bpnn_weight):
|
82 |
+
#-----------lstm-----------
|
83 |
+
print("--------model loaded lstm---------")
|
84 |
+
predictions = loaded_model_lstm.predict(input_data)
|
85 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
86 |
+
arrayForecast = np.array(predictions_inv)
|
87 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
88 |
+
lstmForecast = arrayForecast
|
89 |
+
|
90 |
+
#-----------prophet-----------
|
91 |
+
print("--------model loaded prophet---------")
|
92 |
+
dataset['ds'] = dataset['ds'].dt.tz_localize(None)
|
93 |
+
|
94 |
+
model_prophet = Prophet()
|
95 |
+
model_prophet.fit(dataset)
|
96 |
+
|
97 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
98 |
+
prophetForecast = model_prophet.predict(future)
|
99 |
+
prophetForecast = prophetForecast.tail(12)
|
100 |
+
|
101 |
+
# get only ds and yhat
|
102 |
+
prophetForecast = prophetForecast[['ds', 'yhat']]
|
103 |
+
prophetForecast = prophetForecast.set_index('ds')
|
104 |
+
prophetForecast.reset_index(inplace=True)
|
105 |
+
|
106 |
+
#-----------bpnn-----------
|
107 |
+
dataset_bpnn = dataset.copy().tail(12)
|
108 |
+
dataset_bpnn['ds'] = pd.to_datetime(dataset_bpnn['ds'])
|
109 |
+
dataset_bpnn['hour'] = dataset_bpnn['ds'].dt.hour
|
110 |
+
dataset_bpnn['minute'] = dataset_bpnn['ds'].dt.minute
|
111 |
+
dataset_bpnn['day_of_week'] = dataset_bpnn['ds'].dt.dayofweek
|
112 |
+
|
113 |
+
# drop ds and y column
|
114 |
+
dataset_bpnn = dataset_bpnn.drop(['ds', 'y'], axis=1)
|
115 |
+
|
116 |
+
#add lstm forecast to dataset
|
117 |
+
dataset_bpnn['lstm'] = lstmForecast
|
118 |
+
|
119 |
+
# add prophet forecast to dataset
|
120 |
+
dataset_bpnn['prophet'] = prophetForecast['yhat'].values
|
121 |
+
|
122 |
+
print("--------model loaded bpnn---------")
|
123 |
+
predictions = loaded_model_bpnn.predict(dataset_bpnn)
|
124 |
+
|
125 |
+
# convert 2D to 1D array
|
126 |
+
predictions = predictions.flatten()
|
127 |
+
|
128 |
+
# round up to 2 decimal
|
129 |
+
arrayForecast = np.around(predictions, decimals=4)
|
130 |
+
|
131 |
+
# convert to list
|
132 |
+
listForecast = arrayForecast.tolist()
|
133 |
+
|
134 |
+
# convert to json
|
135 |
+
objectFormat['forecast'] = listForecast
|
136 |
+
|
137 |
+
return jsonify(objectFormat)
|
138 |
+
|
139 |
+
else:
|
140 |
+
print(f"File not found: {temp_lstm_weight} or {temp_bpnn_weight}")
|
141 |
+
except Exception as e:
|
142 |
+
print(e)
|
143 |
+
|
144 |
+
#-------------------Prophet-------------------
|
145 |
+
def predictTempProphet():
|
146 |
+
if request.method == 'POST':
|
147 |
+
try:
|
148 |
+
data = request.json
|
149 |
+
objectFormat = data['dataTemp']
|
150 |
+
|
151 |
+
# push data to array
|
152 |
+
tempTime = []
|
153 |
+
for i in objectFormat['time']:
|
154 |
+
tempTime.append(i)
|
155 |
+
|
156 |
+
tempData = []
|
157 |
+
for i in objectFormat['value']:
|
158 |
+
tempData.append(i)
|
159 |
+
|
160 |
+
# convert to numpy array and pandas dataframe
|
161 |
+
arrayData = np.array(tempData)
|
162 |
+
arrayTime = np.array(tempTime)
|
163 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
164 |
+
|
165 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
166 |
+
dataset = dataset.set_index('ds')
|
167 |
+
dataset = dataset.resample('5T').ffill()
|
168 |
+
dataset.reset_index(inplace=True)
|
169 |
+
|
170 |
+
model_prophet = Prophet()
|
171 |
+
model_prophet.fit(dataset)
|
172 |
+
|
173 |
+
# Make a future dataframe for 1 hours later (5 minutes each)
|
174 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
175 |
+
forecast = model_prophet.predict(future)
|
176 |
+
|
177 |
+
# get last 12 rows
|
178 |
+
forecast = forecast.tail(12)
|
179 |
+
|
180 |
+
# get only ds and yhat
|
181 |
+
forecast = forecast[['ds', 'yhat']]
|
182 |
+
forecast = forecast.set_index('ds')
|
183 |
+
forecast.reset_index(inplace=True)
|
184 |
+
|
185 |
+
# convert to numpy array
|
186 |
+
arrayForecast = np.array(forecast['yhat'])
|
187 |
+
|
188 |
+
# round up to 2 decimal
|
189 |
+
arrayForecast = np.around(arrayForecast, decimals=2)
|
190 |
+
|
191 |
+
# combine array
|
192 |
+
# arrayForecast = np.concatenate((arrayData, arrayForecast), axis=0)
|
193 |
+
# print(arrayForecast)
|
194 |
+
|
195 |
+
# convert to list
|
196 |
+
listForecast = arrayForecast.tolist()
|
197 |
+
|
198 |
+
# convert to json
|
199 |
+
objectFormat['forecast'] = listForecast
|
200 |
+
|
201 |
+
except Exception as e:
|
202 |
+
print(e)
|
203 |
+
|
204 |
+
return jsonify(objectFormat)
|
205 |
+
|
206 |
+
def predictHumiProphet():
|
207 |
+
if request.method == 'POST':
|
208 |
+
try:
|
209 |
+
data = request.json
|
210 |
+
objectFormat = data['dataHumi']
|
211 |
+
|
212 |
+
humiTime = []
|
213 |
+
for i in objectFormat['time']:
|
214 |
+
humiTime.append(i)
|
215 |
+
|
216 |
+
humiData = []
|
217 |
+
for i in objectFormat['value']:
|
218 |
+
humiData.append(i)
|
219 |
+
|
220 |
+
arrayData = np.array(humiData)
|
221 |
+
arrayTime = np.array(humiTime)
|
222 |
+
datetimeHumi = pd.to_datetime(arrayTime)
|
223 |
+
|
224 |
+
dataset = pd.DataFrame({'ds': datetimeHumi, 'y': arrayData})
|
225 |
+
dataset = dataset.set_index('ds')
|
226 |
+
dataset = dataset.resample('5T').ffill()
|
227 |
+
dataset.reset_index(inplace=True)
|
228 |
+
|
229 |
+
model_prophet = Prophet()
|
230 |
+
model_prophet.fit(dataset)
|
231 |
+
|
232 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
233 |
+
forecast = model_prophet.predict(future)
|
234 |
+
forecast = forecast.tail(12)
|
235 |
+
|
236 |
+
forecast = forecast[['ds', 'yhat']]
|
237 |
+
forecast = forecast.set_index('ds')
|
238 |
+
forecast.reset_index(inplace=True)
|
239 |
+
|
240 |
+
arrayForecast = np.array(forecast['yhat'])
|
241 |
+
arrayForecast = np.around(arrayForecast, decimals=2)
|
242 |
+
listForecast = arrayForecast.tolist()
|
243 |
+
objectFormat['forecast'] = listForecast
|
244 |
+
except Exception as e:
|
245 |
+
print(e)
|
246 |
+
return jsonify(objectFormat)
|
247 |
+
|
248 |
+
def predictCO2Prophet():
|
249 |
+
if request.method == 'POST':
|
250 |
+
try:
|
251 |
+
data = request.json
|
252 |
+
objectFormat = data['dataCO2']
|
253 |
+
|
254 |
+
co2Time = []
|
255 |
+
for i in objectFormat['time']:
|
256 |
+
co2Time.append(i)
|
257 |
+
|
258 |
+
co2Data = []
|
259 |
+
for i in objectFormat['value']:
|
260 |
+
co2Data.append(i)
|
261 |
+
|
262 |
+
arrayData = np.array(co2Data)
|
263 |
+
arrayTime = np.array(co2Time)
|
264 |
+
datetimeCO2 = pd.to_datetime(arrayTime)
|
265 |
+
|
266 |
+
dataset = pd.DataFrame({'ds': datetimeCO2, 'y': arrayData})
|
267 |
+
dataset = dataset.set_index('ds')
|
268 |
+
dataset = dataset.resample('5T').ffill()
|
269 |
+
dataset.reset_index(inplace=True)
|
270 |
+
|
271 |
+
model_prophet = Prophet()
|
272 |
+
model_prophet.fit(dataset)
|
273 |
+
|
274 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
275 |
+
forecast = model_prophet.predict(future)
|
276 |
+
forecast = forecast.tail(12)
|
277 |
+
|
278 |
+
forecast = forecast[['ds', 'yhat']]
|
279 |
+
forecast = forecast.set_index('ds')
|
280 |
+
forecast.reset_index(inplace=True)
|
281 |
+
|
282 |
+
arrayForecast = np.array(forecast['yhat'])
|
283 |
+
arrayForecast = np.around(arrayForecast, decimals=2)
|
284 |
+
listForecast = arrayForecast.tolist()
|
285 |
+
objectFormat['forecast'] = listForecast
|
286 |
+
except Exception as e:
|
287 |
+
print(e)
|
288 |
+
return jsonify(objectFormat)
|
289 |
+
|
290 |
+
def predictCOProphet():
|
291 |
+
if request.method == 'POST':
|
292 |
+
try:
|
293 |
+
data = request.json
|
294 |
+
objectFormat = data['dataCO']
|
295 |
+
|
296 |
+
coTime = []
|
297 |
+
for i in objectFormat['time']:
|
298 |
+
coTime.append(i)
|
299 |
+
|
300 |
+
coData = []
|
301 |
+
for i in objectFormat['value']:
|
302 |
+
coData.append(i)
|
303 |
+
|
304 |
+
arrayData = np.array(coData)
|
305 |
+
arrayTime = np.array(coTime)
|
306 |
+
datetimeCO = pd.to_datetime(arrayTime)
|
307 |
+
|
308 |
+
dataset = pd.DataFrame({'ds': datetimeCO, 'y': arrayData})
|
309 |
+
dataset = dataset.set_index('ds')
|
310 |
+
dataset = dataset.resample('5T').ffill()
|
311 |
+
dataset.reset_index(inplace=True)
|
312 |
+
|
313 |
+
model_prophet = Prophet()
|
314 |
+
model_prophet.fit(dataset)
|
315 |
+
|
316 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
317 |
+
forecast = model_prophet.predict(future)
|
318 |
+
forecast = forecast.tail(12)
|
319 |
+
|
320 |
+
forecast = forecast[['ds', 'yhat']]
|
321 |
+
forecast = forecast.set_index('ds')
|
322 |
+
forecast.reset_index(inplace=True)
|
323 |
+
|
324 |
+
arrayForecast = np.array(forecast['yhat'])
|
325 |
+
arrayForecast = np.around(arrayForecast, decimals=2)
|
326 |
+
listForecast = arrayForecast.tolist()
|
327 |
+
objectFormat['forecast'] = listForecast
|
328 |
+
except Exception as e:
|
329 |
+
print(e)
|
330 |
+
return jsonify(objectFormat)
|
331 |
+
|
332 |
+
def predictUVProphet():
|
333 |
+
if request.method == 'POST':
|
334 |
+
try:
|
335 |
+
data = request.json
|
336 |
+
objectFormat = data['dataUV']
|
337 |
+
|
338 |
+
uvTime = []
|
339 |
+
for i in objectFormat['time']:
|
340 |
+
uvTime.append(i)
|
341 |
+
|
342 |
+
uvData = []
|
343 |
+
for i in objectFormat['value']:
|
344 |
+
uvData.append(i)
|
345 |
+
|
346 |
+
arrayData = np.array(uvData)
|
347 |
+
arrayTime = np.array(uvTime)
|
348 |
+
datetimeUV = pd.to_datetime(arrayTime)
|
349 |
+
|
350 |
+
dataset = pd.DataFrame({'ds': datetimeUV, 'y': arrayData})
|
351 |
+
dataset = dataset.set_index('ds')
|
352 |
+
dataset = dataset.resample('5T').ffill()
|
353 |
+
dataset.reset_index(inplace=True)
|
354 |
+
|
355 |
+
model_prophet = Prophet()
|
356 |
+
model_prophet.fit(dataset)
|
357 |
+
|
358 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
359 |
+
forecast = model_prophet.predict(future)
|
360 |
+
forecast = forecast.tail(12)
|
361 |
+
|
362 |
+
forecast = forecast[['ds', 'yhat']]
|
363 |
+
forecast = forecast.set_index('ds')
|
364 |
+
forecast.reset_index(inplace=True)
|
365 |
+
|
366 |
+
arrayForecast = np.array(forecast['yhat'])
|
367 |
+
arrayForecast = np.around(arrayForecast, decimals=2)
|
368 |
+
listForecast = arrayForecast.tolist()
|
369 |
+
objectFormat['forecast'] = listForecast
|
370 |
+
except Exception as e:
|
371 |
+
print(e)
|
372 |
+
return jsonify(objectFormat)
|
373 |
+
|
374 |
+
def predictPM25Prophet():
|
375 |
+
if request.method == 'POST':
|
376 |
+
try:
|
377 |
+
data = request.json
|
378 |
+
objectFormat = data['dataPM25']
|
379 |
+
|
380 |
+
pm25Time = []
|
381 |
+
for i in objectFormat['time']:
|
382 |
+
pm25Time.append(i)
|
383 |
+
|
384 |
+
pm25Data = []
|
385 |
+
for i in objectFormat['value']:
|
386 |
+
pm25Data.append(i)
|
387 |
+
|
388 |
+
arrayData = np.array(pm25Data)
|
389 |
+
arrayTime = np.array(pm25Time)
|
390 |
+
datetimePM25 = pd.to_datetime(arrayTime)
|
391 |
+
|
392 |
+
dataset = pd.DataFrame({'ds': datetimePM25, 'y': arrayData})
|
393 |
+
dataset = dataset.set_index('ds')
|
394 |
+
dataset = dataset.resample('5T').ffill()
|
395 |
+
dataset.reset_index(inplace=True)
|
396 |
+
|
397 |
+
model_prophet = Prophet()
|
398 |
+
model_prophet.fit(dataset)
|
399 |
+
|
400 |
+
future = model_prophet.make_future_dataframe(periods=12, freq='5T')
|
401 |
+
forecast = model_prophet.predict(future)
|
402 |
+
forecast = forecast.tail(12)
|
403 |
+
|
404 |
+
forecast = forecast[['ds', 'yhat']]
|
405 |
+
forecast = forecast.set_index('ds')
|
406 |
+
forecast.reset_index(inplace=True)
|
407 |
+
|
408 |
+
arrayForecast = np.array(forecast['yhat'])
|
409 |
+
arrayForecast = np.around(arrayForecast, decimals=2)
|
410 |
+
listForecast = arrayForecast.tolist()
|
411 |
+
objectFormat['forecast'] = listForecast
|
412 |
+
except Exception as e:
|
413 |
+
print(e)
|
414 |
+
return jsonify(objectFormat)
|
415 |
+
|
416 |
+
#-------------------LSTM-------------------
|
417 |
+
def predictTempLSTM():
|
418 |
+
if request.method == 'POST':
|
419 |
+
try:
|
420 |
+
data = request.json
|
421 |
+
objectFormat = data['dataTemp']
|
422 |
+
|
423 |
+
# push data to array
|
424 |
+
tempTime = []
|
425 |
+
for i in objectFormat['time']:
|
426 |
+
tempTime.append(i)
|
427 |
+
|
428 |
+
tempData = []
|
429 |
+
for i in objectFormat['value']:
|
430 |
+
tempData.append(i)
|
431 |
+
|
432 |
+
arrayData = np.array(tempData)
|
433 |
+
arrayTime = np.array(tempTime)
|
434 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
435 |
+
|
436 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
437 |
+
dataset = dataset.set_index('ds')
|
438 |
+
dataset = dataset.resample('5T').ffill()
|
439 |
+
dataset = dataset.dropna()
|
440 |
+
dataset = dataset.iloc[1:]
|
441 |
+
|
442 |
+
dataset.reset_index(inplace=True)
|
443 |
+
|
444 |
+
# Scale the data to be between 0 and 1
|
445 |
+
scaler = MinMaxScaler()
|
446 |
+
scaled_temp = scaler.fit_transform(dataset[['y']])
|
447 |
+
|
448 |
+
# Ensure the sequence length matches the model's input (100 time steps)
|
449 |
+
sequence_length = 12
|
450 |
+
|
451 |
+
# Pad or truncate the sequence to match the model's input sequence length
|
452 |
+
if len(scaled_temp) < sequence_length:
|
453 |
+
padded_temp = np.pad(scaled_temp, ((sequence_length - len(scaled_temp), 0), (0, 0)), mode='constant')
|
454 |
+
else:
|
455 |
+
padded_temp = scaled_temp[-sequence_length:]
|
456 |
+
|
457 |
+
# Reshape the data to be suitable for LSTM (samples, time steps, features)
|
458 |
+
input_data = padded_temp.reshape((1, 1, sequence_length))
|
459 |
+
|
460 |
+
# Load model architecture from JSON file
|
461 |
+
temp_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/test-lstm.json')
|
462 |
+
temp_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/test_lstm_weight.h5')
|
463 |
+
with open(temp_lstm_json, 'r') as json_file:
|
464 |
+
loaded_model_json = json_file.read()
|
465 |
+
|
466 |
+
# Load model json
|
467 |
+
loaded_model = model_from_json(loaded_model_json)
|
468 |
+
|
469 |
+
# Load model weights
|
470 |
+
loaded_model.load_weights(temp_lstm_weight)
|
471 |
+
|
472 |
+
if os.path.exists(temp_lstm_weight) and os.path.exists(temp_lstm_json):
|
473 |
+
print("--------model loaded---------")
|
474 |
+
predictions = loaded_model.predict(input_data)
|
475 |
+
|
476 |
+
# # Inverse transform the predictions to get original scale
|
477 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
478 |
+
|
479 |
+
# get data from predictions
|
480 |
+
arrayForecast = np.array(predictions_inv)
|
481 |
+
|
482 |
+
# round up to 2 decimal
|
483 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
484 |
+
|
485 |
+
# convert to list
|
486 |
+
listForecast = arrayForecast.tolist()
|
487 |
+
|
488 |
+
# convert to json
|
489 |
+
objectFormat['forecast'] = listForecast
|
490 |
+
|
491 |
+
else:
|
492 |
+
print(f"File not found: {temp_lstm_weight}")
|
493 |
+
except Exception as e:
|
494 |
+
print(e)
|
495 |
+
|
496 |
+
return jsonify(objectFormat)
|
497 |
+
|
498 |
+
def predictHumiLSTM():
|
499 |
+
if request.method == 'POST':
|
500 |
+
try:
|
501 |
+
data = request.json
|
502 |
+
objectFormat = data['dataHumi']
|
503 |
+
|
504 |
+
# push data to array
|
505 |
+
humiTime = []
|
506 |
+
for i in objectFormat['time']:
|
507 |
+
humiTime.append(i)
|
508 |
+
|
509 |
+
humiData = []
|
510 |
+
for i in objectFormat['value']:
|
511 |
+
humiData.append(i)
|
512 |
+
|
513 |
+
arrayData = np.array(humiData)
|
514 |
+
arrayTime = np.array(humiTime)
|
515 |
+
datetimeHumi = pd.to_datetime(arrayTime)
|
516 |
+
|
517 |
+
dataset = pd.DataFrame({'ds': datetimeHumi, 'y': arrayData})
|
518 |
+
dataset = dataset.set_index('ds')
|
519 |
+
dataset = dataset.resample('5T').ffill()
|
520 |
+
dataset = dataset.dropna()
|
521 |
+
dataset = dataset.iloc[1:]
|
522 |
+
dataset.reset_index(inplace=True)
|
523 |
+
|
524 |
+
scaler = MinMaxScaler()
|
525 |
+
scaled_humi = scaler.fit_transform(dataset[['y']])
|
526 |
+
|
527 |
+
sequence_length = 100
|
528 |
+
if len(scaled_humi) < sequence_length:
|
529 |
+
padded_humi = np.pad(scaled_humi, ((sequence_length - len(scaled_humi), 0), (0, 0)), mode='constant')
|
530 |
+
else:
|
531 |
+
padded_humi = scaled_humi[-sequence_length:]
|
532 |
+
input_data = padded_humi.reshape((1, 1, sequence_length))
|
533 |
+
|
534 |
+
humi_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/humi-lstm.json')
|
535 |
+
humi_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/humi_lstm_weight.h5')
|
536 |
+
with open(humi_lstm_json, 'r') as json_file:
|
537 |
+
loaded_model_json = json_file.read()
|
538 |
+
|
539 |
+
loaded_model = model_from_json(loaded_model_json)
|
540 |
+
loaded_model.load_weights(humi_lstm_weight)
|
541 |
+
|
542 |
+
if os.path.exists(humi_lstm_weight):
|
543 |
+
predictions = loaded_model.predict(input_data)
|
544 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
545 |
+
arrayForecast = np.array(predictions_inv)
|
546 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
547 |
+
listForecast = arrayForecast.tolist()
|
548 |
+
objectFormat['forecast'] = listForecast
|
549 |
+
else:
|
550 |
+
print(f"File not found: {humi_lstm_weight}")
|
551 |
+
except Exception as e:
|
552 |
+
print(e)
|
553 |
+
return jsonify(objectFormat)
|
554 |
+
|
555 |
+
def predictCO2LSTM():
|
556 |
+
if request.method == 'POST':
|
557 |
+
try:
|
558 |
+
data = request.json
|
559 |
+
objectFormat = data['dataCO2']
|
560 |
+
|
561 |
+
# push data to array
|
562 |
+
co2Time = []
|
563 |
+
for i in objectFormat['time']:
|
564 |
+
co2Time.append(i)
|
565 |
+
|
566 |
+
co2Data = []
|
567 |
+
for i in objectFormat['value']:
|
568 |
+
co2Data.append(i)
|
569 |
+
|
570 |
+
arrayData = np.array(co2Data)
|
571 |
+
arrayTime = np.array(co2Time)
|
572 |
+
datetimeCO2 = pd.to_datetime(arrayTime)
|
573 |
+
|
574 |
+
dataset = pd.DataFrame({'ds': datetimeCO2, 'y': arrayData})
|
575 |
+
dataset = dataset.set_index('ds')
|
576 |
+
dataset = dataset.resample('5T').ffill()
|
577 |
+
dataset.reset_index(inplace=True)
|
578 |
+
|
579 |
+
scaler = MinMaxScaler()
|
580 |
+
scaled_co2 = scaler.fit_transform(dataset[['y']])
|
581 |
+
|
582 |
+
sequence_length = 100
|
583 |
+
if len(scaled_co2) < sequence_length:
|
584 |
+
padded_co2 = np.pad(scaled_co2, ((sequence_length - len(scaled_co2), 0), (0, 0)), mode='constant')
|
585 |
+
else:
|
586 |
+
padded_co2 = scaled_co2[-sequence_length:]
|
587 |
+
input_data = padded_co2.reshape((1, 1, sequence_length))
|
588 |
+
|
589 |
+
co2_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/co2-lstm.json')
|
590 |
+
co2_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/co2_lstm_weight.h5')
|
591 |
+
with open(co2_lstm_json, 'r') as json_file:
|
592 |
+
loaded_model_json = json_file.read()
|
593 |
+
|
594 |
+
loaded_model = model_from_json(loaded_model_json)
|
595 |
+
loaded_model.load_weights(co2_lstm_weight)
|
596 |
+
|
597 |
+
if os.path.exists(co2_lstm_weight):
|
598 |
+
predictions = loaded_model.predict(input_data)
|
599 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
600 |
+
arrayForecast = np.array(predictions_inv)
|
601 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
602 |
+
listForecast = arrayForecast.tolist()
|
603 |
+
objectFormat['forecast'] = listForecast
|
604 |
+
else:
|
605 |
+
print(f"File not found: {co2_lstm_weight}")
|
606 |
+
except Exception as e:
|
607 |
+
print(e)
|
608 |
+
return jsonify(objectFormat)
|
609 |
+
|
610 |
+
def predictCOLSTM():
|
611 |
+
if request.method == 'POST':
|
612 |
+
try:
|
613 |
+
data = request.json
|
614 |
+
objectFormat = data['dataCO']
|
615 |
+
|
616 |
+
# push data to array
|
617 |
+
coTime = []
|
618 |
+
for i in objectFormat['time']:
|
619 |
+
coTime.append(i)
|
620 |
+
|
621 |
+
coData = []
|
622 |
+
for i in objectFormat['value']:
|
623 |
+
coData.append(i)
|
624 |
+
|
625 |
+
arrayData = np.array(coData)
|
626 |
+
arrayTime = np.array(coTime)
|
627 |
+
datetimeCO = pd.to_datetime(arrayTime)
|
628 |
+
|
629 |
+
dataset = pd.DataFrame({'ds': datetimeCO, 'y': arrayData})
|
630 |
+
dataset = dataset.set_index('ds')
|
631 |
+
dataset = dataset.resample('5T').ffill()
|
632 |
+
dataset.reset_index(inplace=True)
|
633 |
+
|
634 |
+
scaler = MinMaxScaler()
|
635 |
+
scaled_co = scaler.fit_transform(dataset[['y']])
|
636 |
+
|
637 |
+
sequence_length = 100
|
638 |
+
if len(scaled_co) < sequence_length:
|
639 |
+
padded_co = np.pad(scaled_co, ((sequence_length - len(scaled_co), 0), (0, 0)), mode='constant')
|
640 |
+
else:
|
641 |
+
padded_co = scaled_co[-sequence_length:]
|
642 |
+
input_data = padded_co.reshape((1, 1, sequence_length))
|
643 |
+
|
644 |
+
co_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/co-lstm.json')
|
645 |
+
co_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/co_lstm_weight.h5')
|
646 |
+
with open(co_lstm_json, 'r') as json_file:
|
647 |
+
loaded_model_json = json_file.read()
|
648 |
+
|
649 |
+
loaded_model = model_from_json(loaded_model_json)
|
650 |
+
loaded_model.load_weights(co_lstm_weight)
|
651 |
+
|
652 |
+
if os.path.exists(co_lstm_weight):
|
653 |
+
predictions = loaded_model.predict(input_data)
|
654 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
655 |
+
arrayForecast = np.array(predictions_inv)
|
656 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
657 |
+
listForecast = arrayForecast.tolist()
|
658 |
+
objectFormat['forecast'] = listForecast
|
659 |
+
else:
|
660 |
+
print(f"File not found: {co_lstm_weight}")
|
661 |
+
except Exception as e:
|
662 |
+
print(e)
|
663 |
+
return jsonify(objectFormat)
|
664 |
+
|
665 |
+
def predictUVLSTM():
|
666 |
+
if request.method == 'POST':
|
667 |
+
try:
|
668 |
+
data = request.json
|
669 |
+
objectFormat = data['dataUV']
|
670 |
+
|
671 |
+
# push data to array
|
672 |
+
uvTime = []
|
673 |
+
for i in objectFormat['time']:
|
674 |
+
uvTime.append(i)
|
675 |
+
|
676 |
+
uvData = []
|
677 |
+
for i in objectFormat['value']:
|
678 |
+
uvData.append(i)
|
679 |
+
|
680 |
+
arrayData = np.array(uvData)
|
681 |
+
arrayTime = np.array(uvTime)
|
682 |
+
datetimeUV = pd.to_datetime(arrayTime)
|
683 |
+
|
684 |
+
dataset = pd.DataFrame({'ds': datetimeUV, 'y': arrayData})
|
685 |
+
dataset = dataset.set_index('ds')
|
686 |
+
dataset = dataset.resample('5T').ffill()
|
687 |
+
dataset.reset_index(inplace=True)
|
688 |
+
|
689 |
+
scaler = MinMaxScaler()
|
690 |
+
scaled_uv = scaler.fit_transform(dataset[['y']])
|
691 |
+
|
692 |
+
sequence_length = 100
|
693 |
+
if len(scaled_uv) < sequence_length:
|
694 |
+
padded_uv = np.pad(scaled_uv, ((sequence_length - len(scaled_uv), 0), (0, 0)), mode='constant')
|
695 |
+
else:
|
696 |
+
padded_uv = scaled_uv[-sequence_length:]
|
697 |
+
input_data = padded_uv.reshape((1, 1, sequence_length))
|
698 |
+
|
699 |
+
uv_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/uv-lstm.json')
|
700 |
+
uv_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/uv_lstm_weight.h5')
|
701 |
+
with open(uv_lstm_json, 'r') as json_file:
|
702 |
+
loaded_model_json = json_file.read()
|
703 |
+
|
704 |
+
loaded_model = model_from_json(loaded_model_json)
|
705 |
+
loaded_model.load_weights(uv_lstm_weight)
|
706 |
+
|
707 |
+
if os.path.exists(uv_lstm_weight):
|
708 |
+
predictions = loaded_model.predict(input_data)
|
709 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
710 |
+
arrayForecast = np.array(predictions_inv)
|
711 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
712 |
+
listForecast = arrayForecast.tolist()
|
713 |
+
objectFormat['forecast'] = listForecast
|
714 |
+
else:
|
715 |
+
print(f"File not found: {uv_lstm_weight}")
|
716 |
+
except Exception as e:
|
717 |
+
print(e)
|
718 |
+
return jsonify(objectFormat)
|
719 |
+
|
720 |
+
def predictPM25LSTM():
|
721 |
+
if request.method == 'POST':
|
722 |
+
try:
|
723 |
+
data = request.json
|
724 |
+
objectFormat = data['dataPM25']
|
725 |
+
|
726 |
+
# push data to array
|
727 |
+
pm25Time = []
|
728 |
+
for i in objectFormat['time']:
|
729 |
+
pm25Time.append(i)
|
730 |
+
|
731 |
+
pm25Data = []
|
732 |
+
for i in objectFormat['value']:
|
733 |
+
pm25Data.append(i)
|
734 |
+
|
735 |
+
arrayData = np.array(pm25Data)
|
736 |
+
arrayTime = np.array(pm25Time)
|
737 |
+
datetimePM25 = pd.to_datetime(arrayTime)
|
738 |
+
|
739 |
+
dataset = pd.DataFrame({'ds': datetimePM25, 'y': arrayData})
|
740 |
+
dataset = dataset.set_index('ds')
|
741 |
+
dataset = dataset.resample('5T').ffill()
|
742 |
+
dataset.reset_index(inplace=True)
|
743 |
+
|
744 |
+
scaler = MinMaxScaler()
|
745 |
+
scaled_pm25 = scaler.fit_transform(dataset[['y']])
|
746 |
+
|
747 |
+
sequence_length = 100
|
748 |
+
if len(scaled_pm25) < sequence_length:
|
749 |
+
padded_pm25 = np.pad(scaled_pm25, ((sequence_length - len(scaled_pm25), 0), (0, 0)), mode='constant')
|
750 |
+
else:
|
751 |
+
padded_pm25 = scaled_pm25[-sequence_length:]
|
752 |
+
input_data = padded_pm25.reshape((1, 1, sequence_length))
|
753 |
+
|
754 |
+
pm25_lstm_json = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/pm25-lstm.json')
|
755 |
+
pm25_lstm_weight = os.path.join(server_dir, 'aiair-server/datasets/models/lstm/pm25_lstm_weight.h5')
|
756 |
+
with open(pm25_lstm_json, 'r') as json_file:
|
757 |
+
loaded_model_json = json_file.read()
|
758 |
+
|
759 |
+
loaded_model = model_from_json(loaded_model_json)
|
760 |
+
loaded_model.load_weights(pm25_lstm_weight)
|
761 |
+
|
762 |
+
if os.path.exists(pm25_lstm_weight):
|
763 |
+
predictions = loaded_model.predict(input_data)
|
764 |
+
predictions_inv = scaler.inverse_transform(predictions)[0]
|
765 |
+
arrayForecast = np.array(predictions_inv)
|
766 |
+
arrayForecast = np.absolute(arrayForecast)
|
767 |
+
arrayForecast = np.around(arrayForecast, decimals=4)
|
768 |
+
listForecast = arrayForecast.tolist()
|
769 |
+
objectFormat['forecast'] = listForecast
|
770 |
+
else:
|
771 |
+
print(f"File not found: {pm25_lstm_weight}")
|
772 |
+
except Exception as e:
|
773 |
+
print(e)
|
774 |
+
return jsonify(objectFormat)
|
775 |
+
|
776 |
+
#-------------------LR-------------------
|
777 |
+
def predictLRTemp():
|
778 |
+
if request.method == 'POST':
|
779 |
+
try:
|
780 |
+
data = request.json
|
781 |
+
objectFormat = data['dataTemp']
|
782 |
+
|
783 |
+
tempData = []
|
784 |
+
for i in objectFormat['value']:
|
785 |
+
tempData.append(i)
|
786 |
+
|
787 |
+
tempTime = []
|
788 |
+
for i in objectFormat['time']:
|
789 |
+
tempTime.append(i)
|
790 |
+
|
791 |
+
# convert to numpy array and pandas dataframe
|
792 |
+
arrayData = np.array(tempData)
|
793 |
+
arrayTime = np.array(tempTime)
|
794 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
795 |
+
|
796 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
797 |
+
dataset = dataset.set_index('ds')
|
798 |
+
dataset = dataset.resample('5T').ffill()
|
799 |
+
dataset = dataset.dropna()
|
800 |
+
dataset = dataset.iloc[1:]
|
801 |
+
dataset['time'] = np.arange(len(dataset))
|
802 |
+
|
803 |
+
X = dataset[['time']]
|
804 |
+
y = dataset['y']
|
805 |
+
|
806 |
+
model_lr = LinearRegression()
|
807 |
+
model_lr.fit(X, y)
|
808 |
+
|
809 |
+
# get the last timestamp in the dataset
|
810 |
+
last_timestamp = dataset.index[-1]
|
811 |
+
|
812 |
+
# Generate timestamps for the next hour with 5-minute intervals
|
813 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
814 |
+
|
815 |
+
# Reshape timestamps to be used as features for prediction
|
816 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
817 |
+
|
818 |
+
next_hour_features.set_index('date', inplace=True)
|
819 |
+
|
820 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
821 |
+
|
822 |
+
# Use the trained model to predict vehicle count for the next hour
|
823 |
+
predicted_counts = model_lr.predict(next_hour_features)
|
824 |
+
|
825 |
+
predictions = []
|
826 |
+
for i, count in enumerate(predicted_counts):
|
827 |
+
predictions.append(count)
|
828 |
+
|
829 |
+
arrayForecast = np.around(predictions, decimals=8)
|
830 |
+
|
831 |
+
# convert to list
|
832 |
+
listForecast = arrayForecast.tolist()
|
833 |
+
|
834 |
+
# convert to json
|
835 |
+
objectFormat['forecast'] = listForecast
|
836 |
+
|
837 |
+
# input_datetime_str = str(dataset['ds'].max())
|
838 |
+
# old_date = pd.to_datetime(input_datetime_str)
|
839 |
+
|
840 |
+
# # #get current date
|
841 |
+
# current_date = pd.Timestamp.now()
|
842 |
+
# time_differences = (current_date - old_date).total_seconds()
|
843 |
+
|
844 |
+
# model_path = os.path.join(server_dir, 'server/datasets/models/linear_regression/model_lr_temp.pkl')
|
845 |
+
# if os.path.exists(model_path):
|
846 |
+
# loaded_model = load(model_path)
|
847 |
+
|
848 |
+
# # Predicting 12 values
|
849 |
+
# predictions = []
|
850 |
+
# for _ in range(12):
|
851 |
+
# prediction = loaded_model.predict([[time_differences]])
|
852 |
+
# predictions.append(prediction[0])
|
853 |
+
# time_differences += 300 # Assuming hourly predictions
|
854 |
+
|
855 |
+
# arrayForecast = np.around(predictions, decimals=8)
|
856 |
+
|
857 |
+
# # convert to list
|
858 |
+
# listForecast = arrayForecast.tolist()
|
859 |
+
|
860 |
+
# # convert to json
|
861 |
+
# objectFormat['forecast'] = listForecast
|
862 |
+
# else:
|
863 |
+
# print(f"File not found: {model_path}")
|
864 |
+
|
865 |
+
return jsonify(objectFormat)
|
866 |
+
except Exception as e:
|
867 |
+
print(e)
|
868 |
+
|
869 |
+
def predictLRHumi():
|
870 |
+
if request.method == 'POST':
|
871 |
+
try:
|
872 |
+
data = request.json
|
873 |
+
objectFormat = data['dataHumi']
|
874 |
+
|
875 |
+
humiData = []
|
876 |
+
for i in objectFormat['value']:
|
877 |
+
humiData.append(i)
|
878 |
+
|
879 |
+
humiTime = []
|
880 |
+
for i in objectFormat['time']:
|
881 |
+
humiTime.append(i)
|
882 |
+
|
883 |
+
arrayData = np.array(humiData)
|
884 |
+
arrayTime = np.array(humiTime)
|
885 |
+
datetimeHumi = pd.to_datetime(arrayTime)
|
886 |
+
|
887 |
+
dataset = pd.DataFrame({'ds': datetimeHumi, 'y': arrayData})
|
888 |
+
dataset = dataset.set_index('ds')
|
889 |
+
dataset = dataset.resample('5T').ffill()
|
890 |
+
dataset = dataset.dropna()
|
891 |
+
dataset = dataset.iloc[1:]
|
892 |
+
dataset['time'] = np.arange(len(dataset))
|
893 |
+
|
894 |
+
X = dataset[['time']]
|
895 |
+
y = dataset['y']
|
896 |
+
|
897 |
+
model_lr = LinearRegression()
|
898 |
+
model_lr.fit(X, y)
|
899 |
+
|
900 |
+
last_timestamp = dataset.index[-1]
|
901 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
902 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
903 |
+
next_hour_features.set_index('date', inplace=True)
|
904 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
905 |
+
|
906 |
+
predicted_counts = model_lr.predict(next_hour_features)
|
907 |
+
predictions = []
|
908 |
+
for i, count in enumerate(predicted_counts):
|
909 |
+
predictions.append(count)
|
910 |
+
|
911 |
+
arrayForecast = np.around(predictions, decimals=8)
|
912 |
+
listForecast = arrayForecast.tolist()
|
913 |
+
objectFormat['forecast'] = listForecast
|
914 |
+
return jsonify(objectFormat)
|
915 |
+
except Exception as e:
|
916 |
+
print(e)
|
917 |
+
|
918 |
+
def predictLRCO2():
|
919 |
+
if request.method == 'POST':
|
920 |
+
try:
|
921 |
+
data = request.json
|
922 |
+
objectFormat = data['dataCO2']
|
923 |
+
|
924 |
+
co2Data = []
|
925 |
+
for i in objectFormat['value']:
|
926 |
+
co2Data.append(i)
|
927 |
+
|
928 |
+
co2Time = []
|
929 |
+
for i in objectFormat['time']:
|
930 |
+
co2Time.append(i)
|
931 |
+
|
932 |
+
arrayData = np.array(co2Data)
|
933 |
+
arrayTime = np.array(co2Time)
|
934 |
+
datetimeCO2 = pd.to_datetime(arrayTime)
|
935 |
+
|
936 |
+
dataset = pd.DataFrame({'ds': datetimeCO2, 'y': arrayData})
|
937 |
+
dataset = dataset.set_index('ds')
|
938 |
+
dataset = dataset.resample('5T').ffill()
|
939 |
+
dataset = dataset.dropna()
|
940 |
+
dataset = dataset.iloc[1:]
|
941 |
+
dataset['time'] = np.arange(len(dataset))
|
942 |
+
|
943 |
+
X = dataset[['time']]
|
944 |
+
y = dataset['y']
|
945 |
+
|
946 |
+
model_lr = LinearRegression()
|
947 |
+
model_lr.fit(X, y)
|
948 |
+
|
949 |
+
last_timestamp = dataset.index[-1]
|
950 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
951 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
952 |
+
next_hour_features.set_index('date', inplace=True)
|
953 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
954 |
+
|
955 |
+
predicted_counts = model_lr.predict(next_hour_features)
|
956 |
+
predictions = []
|
957 |
+
for i, count in enumerate(predicted_counts):
|
958 |
+
predictions.append(count)
|
959 |
+
|
960 |
+
arrayForecast = np.around(predictions, decimals=8)
|
961 |
+
listForecast = arrayForecast.tolist()
|
962 |
+
objectFormat['forecast'] = listForecast
|
963 |
+
return jsonify(objectFormat)
|
964 |
+
except Exception as e:
|
965 |
+
print(e)
|
966 |
+
|
967 |
+
def predictLRCO():
|
968 |
+
if request.method == 'POST':
|
969 |
+
try:
|
970 |
+
data = request.json
|
971 |
+
objectFormat = data['dataCO']
|
972 |
+
|
973 |
+
coData = []
|
974 |
+
for i in objectFormat['value']:
|
975 |
+
coData.append(i)
|
976 |
+
|
977 |
+
coTime = []
|
978 |
+
for i in objectFormat['time']:
|
979 |
+
coTime.append(i)
|
980 |
+
|
981 |
+
arrayData = np.array(coData)
|
982 |
+
arrayTime = np.array(coTime)
|
983 |
+
datetimeCO = pd.to_datetime(arrayTime)
|
984 |
+
|
985 |
+
dataset = pd.DataFrame({'ds': datetimeCO, 'y': arrayData})
|
986 |
+
dataset = dataset.set_index('ds')
|
987 |
+
dataset = dataset.resample('5T').ffill()
|
988 |
+
dataset = dataset.dropna()
|
989 |
+
dataset = dataset.iloc[1:]
|
990 |
+
dataset['time'] = np.arange(len(dataset))
|
991 |
+
|
992 |
+
X = dataset[['time']]
|
993 |
+
y = dataset['y']
|
994 |
+
|
995 |
+
model_lr = LinearRegression()
|
996 |
+
model_lr.fit(X, y)
|
997 |
+
|
998 |
+
last_timestamp = dataset.index[-1]
|
999 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1000 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1001 |
+
next_hour_features.set_index('date', inplace=True)
|
1002 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1003 |
+
|
1004 |
+
predicted_counts = model_lr.predict(next_hour_features)
|
1005 |
+
predictions = []
|
1006 |
+
for i, count in enumerate(predicted_counts):
|
1007 |
+
predictions.append(count)
|
1008 |
+
|
1009 |
+
arrayForecast = np.around(predictions, decimals=8)
|
1010 |
+
listForecast = arrayForecast.tolist()
|
1011 |
+
objectFormat['forecast'] = listForecast
|
1012 |
+
return jsonify(objectFormat)
|
1013 |
+
except Exception as e:
|
1014 |
+
print(e)
|
1015 |
+
|
1016 |
+
def predictLRUV():
|
1017 |
+
if request.method == 'POST':
|
1018 |
+
try:
|
1019 |
+
data = request.json
|
1020 |
+
objectFormat = data['dataUV']
|
1021 |
+
|
1022 |
+
uvData = []
|
1023 |
+
for i in objectFormat['value']:
|
1024 |
+
uvData.append(i)
|
1025 |
+
|
1026 |
+
uvTime = []
|
1027 |
+
for i in objectFormat['time']:
|
1028 |
+
uvTime.append(i)
|
1029 |
+
|
1030 |
+
arrayData = np.array(uvData)
|
1031 |
+
arrayTime = np.array(uvTime)
|
1032 |
+
datetimeUV = pd.to_datetime(arrayTime)
|
1033 |
+
|
1034 |
+
dataset = pd.DataFrame({'ds': datetimeUV, 'y': arrayData})
|
1035 |
+
dataset = dataset.set_index('ds')
|
1036 |
+
dataset = dataset.resample('5T').ffill()
|
1037 |
+
dataset = dataset.dropna()
|
1038 |
+
dataset = dataset.iloc[1:]
|
1039 |
+
dataset['time'] = np.arange(len(dataset))
|
1040 |
+
|
1041 |
+
X = dataset[['time']]
|
1042 |
+
y = dataset['y']
|
1043 |
+
|
1044 |
+
model_lr = LinearRegression()
|
1045 |
+
model_lr.fit(X, y)
|
1046 |
+
|
1047 |
+
last_timestamp = dataset.index[-1]
|
1048 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1049 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1050 |
+
next_hour_features.set_index('date', inplace=True)
|
1051 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1052 |
+
|
1053 |
+
predicted_counts = model_lr.predict(next_hour_features)
|
1054 |
+
predictions = []
|
1055 |
+
for i, count in enumerate(predicted_counts):
|
1056 |
+
predictions.append(count)
|
1057 |
+
|
1058 |
+
arrayForecast = np.around(predictions, decimals=8)
|
1059 |
+
listForecast = arrayForecast.tolist()
|
1060 |
+
objectFormat['forecast'] = listForecast
|
1061 |
+
return jsonify(objectFormat)
|
1062 |
+
except Exception as e:
|
1063 |
+
print(e)
|
1064 |
+
|
1065 |
+
def predictLRPM25():
|
1066 |
+
if request.method == 'POST':
|
1067 |
+
try:
|
1068 |
+
data = request.json
|
1069 |
+
objectFormat = data['dataPM25']
|
1070 |
+
|
1071 |
+
pm25Data = []
|
1072 |
+
for i in objectFormat['value']:
|
1073 |
+
pm25Data.append(i)
|
1074 |
+
|
1075 |
+
pm25Time = []
|
1076 |
+
for i in objectFormat['time']:
|
1077 |
+
pm25Time.append(i)
|
1078 |
+
|
1079 |
+
arrayData = np.array(pm25Data)
|
1080 |
+
arrayTime = np.array(pm25Time)
|
1081 |
+
datetimePM25 = pd.to_datetime(arrayTime)
|
1082 |
+
|
1083 |
+
dataset = pd.DataFrame({'ds': datetimePM25, 'y': arrayData})
|
1084 |
+
dataset = dataset.set_index('ds')
|
1085 |
+
dataset = dataset.resample('5T').ffill()
|
1086 |
+
dataset = dataset.dropna()
|
1087 |
+
dataset = dataset.iloc[1:]
|
1088 |
+
dataset['time'] = np.arange(len(dataset))
|
1089 |
+
|
1090 |
+
X = dataset[['time']]
|
1091 |
+
y = dataset['y']
|
1092 |
+
|
1093 |
+
model_lr = LinearRegression()
|
1094 |
+
model_lr.fit(X, y)
|
1095 |
+
|
1096 |
+
last_timestamp = dataset.index[-1]
|
1097 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1098 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1099 |
+
next_hour_features.set_index('date', inplace=True)
|
1100 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1101 |
+
|
1102 |
+
predicted_counts = model_lr.predict(next_hour_features)
|
1103 |
+
predictions = []
|
1104 |
+
for i, count in enumerate(predicted_counts):
|
1105 |
+
predictions.append(count)
|
1106 |
+
|
1107 |
+
arrayForecast = np.around(predictions, decimals=8)
|
1108 |
+
listForecast = arrayForecast.tolist()
|
1109 |
+
objectFormat['forecast'] = listForecast
|
1110 |
+
return jsonify(objectFormat)
|
1111 |
+
except Exception as e:
|
1112 |
+
print(e)
|
1113 |
+
|
1114 |
+
#-------------------GB-------------------
|
1115 |
+
def predictGBTemp():
|
1116 |
+
if request.method == 'POST':
|
1117 |
+
try:
|
1118 |
+
data = request.json
|
1119 |
+
objectFormat = data['dataTemp']
|
1120 |
+
|
1121 |
+
tempData = []
|
1122 |
+
for i in objectFormat['value']:
|
1123 |
+
tempData.append(i)
|
1124 |
+
|
1125 |
+
tempTime = []
|
1126 |
+
for i in objectFormat['time']:
|
1127 |
+
tempTime.append(i)
|
1128 |
+
|
1129 |
+
# convert to numpy array and pandas dataframe
|
1130 |
+
arrayData = np.array(tempData)
|
1131 |
+
arrayTime = np.array(tempTime)
|
1132 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
1133 |
+
|
1134 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
1135 |
+
dataset = dataset.set_index('ds')
|
1136 |
+
dataset = dataset.resample('5T').ffill()
|
1137 |
+
dataset = dataset.dropna()
|
1138 |
+
dataset = dataset.iloc[1:]
|
1139 |
+
dataset['time'] = np.arange(len(dataset))
|
1140 |
+
|
1141 |
+
X = dataset[['time']]
|
1142 |
+
y = dataset['y']
|
1143 |
+
|
1144 |
+
model_gb = GradientBoostingRegressor(**p_gb)
|
1145 |
+
model_gb.fit(X, y)
|
1146 |
+
|
1147 |
+
last_timestamp = dataset.index[-1]
|
1148 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1149 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1150 |
+
next_hour_features.set_index('date', inplace=True)
|
1151 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1152 |
+
|
1153 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1154 |
+
predictions = []
|
1155 |
+
for i, count in enumerate(predicted_counts):
|
1156 |
+
predictions.append(count)
|
1157 |
+
|
1158 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1159 |
+
listForecast = arrayForecast.tolist()
|
1160 |
+
objectFormat['forecast'] = listForecast
|
1161 |
+
return jsonify(objectFormat)
|
1162 |
+
|
1163 |
+
# model_path = os.path.join(server_dir, 'server/datasets/models/gradient_boost/model_gb_temp.pkl')
|
1164 |
+
# if os.path.exists(model_path):
|
1165 |
+
# loaded_model = load(model_path)
|
1166 |
+
# print(loaded_model)
|
1167 |
+
|
1168 |
+
# # Predicting 12 values
|
1169 |
+
# predictions = []
|
1170 |
+
# for _ in range(12):
|
1171 |
+
# prediction = loaded_model.predict([[time_differences]])
|
1172 |
+
# predictions.append(prediction[0])
|
1173 |
+
# time_differences += 300 # Assuming hourly predictions
|
1174 |
+
|
1175 |
+
# # round up to 2 decimal
|
1176 |
+
# arrayForecast = np.around(predictions, decimals=8)
|
1177 |
+
|
1178 |
+
# # convert to list
|
1179 |
+
# listForecast = arrayForecast.tolist()
|
1180 |
+
|
1181 |
+
# # convert to json
|
1182 |
+
# objectFormat['forecast'] = listForecast
|
1183 |
+
# else:
|
1184 |
+
# print(f"File not found: {model_path}")
|
1185 |
+
|
1186 |
+
# return jsonify(objectFormat)
|
1187 |
+
except Exception as e:
|
1188 |
+
print(e)
|
1189 |
+
|
1190 |
+
def predictGBHumi():
|
1191 |
+
if request.method == 'POST':
|
1192 |
+
try:
|
1193 |
+
data = request.json
|
1194 |
+
objectFormat = data['dataHumi']
|
1195 |
+
|
1196 |
+
humiData = []
|
1197 |
+
for i in objectFormat['value']:
|
1198 |
+
humiData.append(i)
|
1199 |
+
|
1200 |
+
humiTime = []
|
1201 |
+
for i in objectFormat['time']:
|
1202 |
+
humiTime.append(i)
|
1203 |
+
|
1204 |
+
arrayData = np.array(humiData)
|
1205 |
+
arrayTime = np.array(humiTime)
|
1206 |
+
datetimeHumi = pd.to_datetime(arrayTime)
|
1207 |
+
|
1208 |
+
dataset = pd.DataFrame({'ds': datetimeHumi, 'y': arrayData})
|
1209 |
+
dataset = dataset.set_index('ds')
|
1210 |
+
dataset = dataset.resample('5T').ffill()
|
1211 |
+
dataset = dataset.dropna()
|
1212 |
+
dataset = dataset.iloc[1:]
|
1213 |
+
dataset['time'] = np.arange(len(dataset))
|
1214 |
+
|
1215 |
+
X = dataset[['time']]
|
1216 |
+
y = dataset['y']
|
1217 |
+
|
1218 |
+
model_gb = GradientBoostingRegressor(**p_gb)
|
1219 |
+
model_gb.fit(X, y)
|
1220 |
+
|
1221 |
+
last_timestamp = dataset.index[-1]
|
1222 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1223 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1224 |
+
next_hour_features.set_index('date', inplace=True)
|
1225 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1226 |
+
|
1227 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1228 |
+
predictions = []
|
1229 |
+
for i, count in enumerate(predicted_counts):
|
1230 |
+
predictions.append(count)
|
1231 |
+
|
1232 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1233 |
+
listForecast = arrayForecast.tolist()
|
1234 |
+
objectFormat['forecast'] = listForecast
|
1235 |
+
return jsonify(objectFormat)
|
1236 |
+
except Exception as e:
|
1237 |
+
print(e)
|
1238 |
+
|
1239 |
+
def predictGBCO2():
|
1240 |
+
if request.method == 'POST':
|
1241 |
+
try:
|
1242 |
+
data = request.json
|
1243 |
+
objectFormat = data['dataCO2']
|
1244 |
+
|
1245 |
+
co2Data = []
|
1246 |
+
for i in objectFormat['value']:
|
1247 |
+
co2Data.append(i)
|
1248 |
+
|
1249 |
+
co2Time = []
|
1250 |
+
for i in objectFormat['time']:
|
1251 |
+
co2Time.append(i)
|
1252 |
+
|
1253 |
+
arrayData = np.array(co2Data)
|
1254 |
+
arrayTime = np.array(co2Time)
|
1255 |
+
datetimeCO2 = pd.to_datetime(arrayTime)
|
1256 |
+
|
1257 |
+
dataset = pd.DataFrame({'ds': datetimeCO2, 'y': arrayData})
|
1258 |
+
dataset = dataset.set_index('ds')
|
1259 |
+
dataset = dataset.resample('5T').ffill()
|
1260 |
+
dataset = dataset.dropna()
|
1261 |
+
dataset = dataset.iloc[1:]
|
1262 |
+
dataset['time'] = np.arange(len(dataset))
|
1263 |
+
|
1264 |
+
X = dataset[['time']]
|
1265 |
+
y = dataset['y']
|
1266 |
+
|
1267 |
+
model_gb = GradientBoostingRegressor(**p_gb)
|
1268 |
+
model_gb.fit(X, y)
|
1269 |
+
|
1270 |
+
last_timestamp = dataset.index[-1]
|
1271 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1272 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1273 |
+
next_hour_features.set_index('date', inplace=True)
|
1274 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1275 |
+
|
1276 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1277 |
+
predictions = []
|
1278 |
+
for i, count in enumerate(predicted_counts):
|
1279 |
+
predictions.append(count)
|
1280 |
+
|
1281 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1282 |
+
listForecast = arrayForecast.tolist()
|
1283 |
+
objectFormat['forecast'] = listForecast
|
1284 |
+
return jsonify(objectFormat)
|
1285 |
+
except Exception as e:
|
1286 |
+
print(e)
|
1287 |
+
|
1288 |
+
def predictGBCO():
|
1289 |
+
if request.method == 'POST':
|
1290 |
+
try:
|
1291 |
+
data = request.json
|
1292 |
+
objectFormat = data['dataCO']
|
1293 |
+
|
1294 |
+
coData = []
|
1295 |
+
for i in objectFormat['value']:
|
1296 |
+
coData.append(i)
|
1297 |
+
|
1298 |
+
coTime = []
|
1299 |
+
for i in objectFormat['time']:
|
1300 |
+
coTime.append(i)
|
1301 |
+
|
1302 |
+
arrayData = np.array(coData)
|
1303 |
+
arrayTime = np.array(coTime)
|
1304 |
+
datetimeCO = pd.to_datetime(arrayTime)
|
1305 |
+
|
1306 |
+
dataset = pd.DataFrame({'ds': datetimeCO, 'y': arrayData})
|
1307 |
+
dataset = dataset.set_index('ds')
|
1308 |
+
dataset = dataset.resample('5T').ffill()
|
1309 |
+
dataset = dataset.dropna()
|
1310 |
+
dataset = dataset.iloc[1:]
|
1311 |
+
dataset['time'] = np.arange(len(dataset))
|
1312 |
+
|
1313 |
+
X = dataset[['time']]
|
1314 |
+
y = dataset['y']
|
1315 |
+
|
1316 |
+
model_gb = GradientBoostingRegressor(**p_gb)
|
1317 |
+
model_gb.fit(X, y)
|
1318 |
+
|
1319 |
+
last_timestamp = dataset.index[-1]
|
1320 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1321 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1322 |
+
next_hour_features.set_index('date', inplace=True)
|
1323 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1324 |
+
|
1325 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1326 |
+
predictions = []
|
1327 |
+
for i, count in enumerate(predicted_counts):
|
1328 |
+
predictions.append(count)
|
1329 |
+
|
1330 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1331 |
+
listForecast = arrayForecast.tolist()
|
1332 |
+
objectFormat['forecast'] = listForecast
|
1333 |
+
return jsonify(objectFormat)
|
1334 |
+
except Exception as e:
|
1335 |
+
print(e)
|
1336 |
+
|
1337 |
+
def predictGBUV():
|
1338 |
+
if request.method == 'POST':
|
1339 |
+
try:
|
1340 |
+
data = request.json
|
1341 |
+
objectFormat = data['dataUV']
|
1342 |
+
|
1343 |
+
uvData = []
|
1344 |
+
for i in objectFormat['value']:
|
1345 |
+
uvData.append(i)
|
1346 |
+
|
1347 |
+
uvTime = []
|
1348 |
+
for i in objectFormat['time']:
|
1349 |
+
uvTime.append(i)
|
1350 |
+
|
1351 |
+
arrayData = np.array(uvData)
|
1352 |
+
arrayTime = np.array(uvTime)
|
1353 |
+
datetimeUV = pd.to_datetime(arrayTime)
|
1354 |
+
|
1355 |
+
dataset = pd.DataFrame({'ds': datetimeUV, 'y': arrayData})
|
1356 |
+
dataset = dataset.set_index('ds')
|
1357 |
+
dataset = dataset.resample('5T').ffill()
|
1358 |
+
dataset = dataset.dropna()
|
1359 |
+
dataset = dataset.iloc[1:]
|
1360 |
+
dataset['time'] = np.arange(len(dataset))
|
1361 |
+
|
1362 |
+
X = dataset[['time']]
|
1363 |
+
y = dataset['y']
|
1364 |
+
|
1365 |
+
model_gb = GradientBoostingRegressor(**p_gb)
|
1366 |
+
model_gb.fit(X, y)
|
1367 |
+
|
1368 |
+
last_timestamp = dataset.index[-1]
|
1369 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1370 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1371 |
+
next_hour_features.set_index('date', inplace=True)
|
1372 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1373 |
+
|
1374 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1375 |
+
predictions = []
|
1376 |
+
for i, count in enumerate(predicted_counts):
|
1377 |
+
predictions.append(count)
|
1378 |
+
|
1379 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1380 |
+
listForecast = arrayForecast.tolist()
|
1381 |
+
objectFormat['forecast'] = listForecast
|
1382 |
+
return jsonify(objectFormat)
|
1383 |
+
except Exception as e:
|
1384 |
+
print(e)
|
1385 |
+
|
1386 |
+
def predictGBPM25():
|
1387 |
+
if request.method == 'POST':
|
1388 |
+
try:
|
1389 |
+
data = request.json
|
1390 |
+
objectFormat = data['dataPM25']
|
1391 |
+
|
1392 |
+
pm25Data = []
|
1393 |
+
for i in objectFormat['value']:
|
1394 |
+
pm25Data.append(i)
|
1395 |
+
|
1396 |
+
pm25Time = []
|
1397 |
+
for i in objectFormat['time']:
|
1398 |
+
pm25Time.append(i)
|
1399 |
+
|
1400 |
+
arrayData = np.array(pm25Data)
|
1401 |
+
arrayTime = np.array(pm25Time)
|
1402 |
+
datetimePM25 = pd.to_datetime(arrayTime)
|
1403 |
+
|
1404 |
+
dataset = pd.DataFrame({'ds': datetimePM25, 'y': arrayData})
|
1405 |
+
dataset = dataset.set_index('ds')
|
1406 |
+
dataset = dataset.resample('5T').ffill()
|
1407 |
+
dataset = dataset.dropna()
|
1408 |
+
dataset = dataset.iloc[1:]
|
1409 |
+
dataset['time'] = np.arange(len(dataset))
|
1410 |
+
|
1411 |
+
X = dataset[['time']]
|
1412 |
+
y = dataset['y']
|
1413 |
+
|
1414 |
+
model_gb = GradientBoostingRegressor(**p_gb)
|
1415 |
+
model_gb.fit(X, y)
|
1416 |
+
|
1417 |
+
last_timestamp = dataset.index[-1]
|
1418 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1419 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1420 |
+
next_hour_features.set_index('date', inplace=True)
|
1421 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1422 |
+
|
1423 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1424 |
+
predictions = []
|
1425 |
+
for i, count in enumerate(predicted_counts):
|
1426 |
+
predictions.append(count)
|
1427 |
+
|
1428 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1429 |
+
listForecast = arrayForecast.tolist()
|
1430 |
+
objectFormat['forecast'] = listForecast
|
1431 |
+
return jsonify(objectFormat)
|
1432 |
+
except Exception as e:
|
1433 |
+
print(e)
|
1434 |
+
|
1435 |
+
#-------------------XGB-------------------
|
1436 |
+
def predictXGBTemp():
|
1437 |
+
if request.method == 'POST':
|
1438 |
+
try:
|
1439 |
+
data = request.json
|
1440 |
+
objectFormat = data['dataTemp']
|
1441 |
+
|
1442 |
+
tempData = []
|
1443 |
+
for i in objectFormat['value']:
|
1444 |
+
tempData.append(i)
|
1445 |
+
|
1446 |
+
tempTime = []
|
1447 |
+
for i in objectFormat['time']:
|
1448 |
+
tempTime.append(i)
|
1449 |
+
|
1450 |
+
# convert to numpy array and pandas dataframe
|
1451 |
+
arrayData = np.array(tempData)
|
1452 |
+
arrayTime = np.array(tempTime)
|
1453 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
1454 |
+
|
1455 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
1456 |
+
dataset = dataset.set_index('ds')
|
1457 |
+
dataset = dataset.resample('5T').ffill()
|
1458 |
+
dataset = dataset.dropna()
|
1459 |
+
dataset = dataset.iloc[1:]
|
1460 |
+
dataset['time'] = np.arange(len(dataset))
|
1461 |
+
|
1462 |
+
X = dataset[['time']]
|
1463 |
+
y = dataset['y']
|
1464 |
+
|
1465 |
+
model_gb = XGBRegressor(**p_gb)
|
1466 |
+
model_gb.fit(X, y)
|
1467 |
+
|
1468 |
+
last_timestamp = dataset.index[-1]
|
1469 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1470 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1471 |
+
next_hour_features.set_index('date', inplace=True)
|
1472 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1473 |
+
|
1474 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1475 |
+
predictions = []
|
1476 |
+
for i, count in enumerate(predicted_counts):
|
1477 |
+
predictions.append(count)
|
1478 |
+
|
1479 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1480 |
+
listForecast = arrayForecast.tolist()
|
1481 |
+
objectFormat['forecast'] = listForecast
|
1482 |
+
return jsonify(objectFormat)
|
1483 |
+
except Exception as e:
|
1484 |
+
print(e)
|
1485 |
+
|
1486 |
+
def predictXGBHumi():
|
1487 |
+
if request.method == 'POST':
|
1488 |
+
try:
|
1489 |
+
data = request.json
|
1490 |
+
objectFormat = data['dataHumi']
|
1491 |
+
|
1492 |
+
humiData = []
|
1493 |
+
for i in objectFormat['value']:
|
1494 |
+
humiData.append(i)
|
1495 |
+
|
1496 |
+
humiTime = []
|
1497 |
+
for i in objectFormat['time']:
|
1498 |
+
humiTime.append(i)
|
1499 |
+
|
1500 |
+
# convert to numpy array and pandas dataframe
|
1501 |
+
arrayData = np.array(humiData)
|
1502 |
+
arrayTime = np.array(humiTime)
|
1503 |
+
datetimeHumi = pd.to_datetime(arrayTime)
|
1504 |
+
|
1505 |
+
dataset = pd.DataFrame({'ds': datetimeHumi, 'y': arrayData})
|
1506 |
+
dataset = dataset.set_index('ds')
|
1507 |
+
dataset = dataset.resample('5T').ffill()
|
1508 |
+
dataset = dataset.dropna()
|
1509 |
+
dataset = dataset.iloc[1:]
|
1510 |
+
dataset['time'] = np.arange(len(dataset))
|
1511 |
+
|
1512 |
+
X = dataset[['time']]
|
1513 |
+
y = dataset['y']
|
1514 |
+
|
1515 |
+
model_gb = XGBRegressor(**p_gb)
|
1516 |
+
model_gb.fit(X, y)
|
1517 |
+
|
1518 |
+
last_timestamp = dataset.index[-1]
|
1519 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1520 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1521 |
+
next_hour_features.set_index('date', inplace=True)
|
1522 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1523 |
+
|
1524 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1525 |
+
predictions = []
|
1526 |
+
for i, count in enumerate(predicted_counts):
|
1527 |
+
predictions.append(count)
|
1528 |
+
|
1529 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1530 |
+
listForecast = arrayForecast.tolist()
|
1531 |
+
objectFormat['forecast'] = listForecast
|
1532 |
+
return jsonify(objectFormat)
|
1533 |
+
except Exception as e:
|
1534 |
+
print(e)
|
1535 |
+
|
1536 |
+
def predictXGBCO2():
|
1537 |
+
if request.method == 'POST':
|
1538 |
+
try:
|
1539 |
+
data = request.json
|
1540 |
+
objectFormat = data['dataCO2']
|
1541 |
+
|
1542 |
+
co2Data = []
|
1543 |
+
for i in objectFormat['value']:
|
1544 |
+
co2Data.append(i)
|
1545 |
+
|
1546 |
+
co2Time = []
|
1547 |
+
for i in objectFormat['time']:
|
1548 |
+
co2Time.append(i)
|
1549 |
+
|
1550 |
+
# convert to numpy array and pandas dataframe
|
1551 |
+
arrayData = np.array(co2Data)
|
1552 |
+
arrayTime = np.array(co2Time)
|
1553 |
+
datetimeCO2 = pd.to_datetime(arrayTime)
|
1554 |
+
|
1555 |
+
dataset = pd.DataFrame({'ds': datetimeCO2, 'y': arrayData})
|
1556 |
+
dataset = dataset.set_index('ds')
|
1557 |
+
dataset = dataset.resample('5T').ffill()
|
1558 |
+
dataset = dataset.dropna()
|
1559 |
+
dataset = dataset.iloc[1:]
|
1560 |
+
dataset['time'] = np.arange(len(dataset))
|
1561 |
+
|
1562 |
+
X = dataset[['time']]
|
1563 |
+
y = dataset['y']
|
1564 |
+
|
1565 |
+
model_gb = XGBRegressor(**p_gb)
|
1566 |
+
model_gb.fit(X, y)
|
1567 |
+
|
1568 |
+
last_timestamp = dataset.index[-1]
|
1569 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1570 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1571 |
+
next_hour_features.set_index('date', inplace=True)
|
1572 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1573 |
+
|
1574 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1575 |
+
predictions = []
|
1576 |
+
for i, count in enumerate(predicted_counts):
|
1577 |
+
predictions.append(count)
|
1578 |
+
|
1579 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1580 |
+
listForecast = arrayForecast.tolist()
|
1581 |
+
objectFormat['forecast'] = listForecast
|
1582 |
+
return jsonify(objectFormat)
|
1583 |
+
except Exception as e:
|
1584 |
+
print(e)
|
1585 |
+
|
1586 |
+
def predictXGBCO():
|
1587 |
+
if request.method == 'POST':
|
1588 |
+
try:
|
1589 |
+
data = request.json
|
1590 |
+
objectFormat = data['dataCO']
|
1591 |
+
|
1592 |
+
coData = []
|
1593 |
+
for i in objectFormat['value']:
|
1594 |
+
coData.append(i)
|
1595 |
+
|
1596 |
+
coTime = []
|
1597 |
+
for i in objectFormat['time']:
|
1598 |
+
coTime.append(i)
|
1599 |
+
|
1600 |
+
# convert to numpy array and pandas dataframe
|
1601 |
+
arrayData = np.array(coData)
|
1602 |
+
arrayTime = np.array(coTime)
|
1603 |
+
datetimeCO = pd.to_datetime(arrayTime)
|
1604 |
+
|
1605 |
+
dataset = pd.DataFrame({'ds': datetimeCO, 'y': arrayData})
|
1606 |
+
dataset = dataset.set_index('ds')
|
1607 |
+
dataset = dataset.resample('5T').ffill()
|
1608 |
+
dataset = dataset.dropna()
|
1609 |
+
dataset = dataset.iloc[1:]
|
1610 |
+
dataset['time'] = np.arange(len(dataset))
|
1611 |
+
|
1612 |
+
X = dataset[['time']]
|
1613 |
+
y = dataset['y']
|
1614 |
+
|
1615 |
+
model_gb = XGBRegressor(**p_gb)
|
1616 |
+
model_gb.fit(X, y)
|
1617 |
+
|
1618 |
+
last_timestamp = dataset.index[-1]
|
1619 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1620 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1621 |
+
next_hour_features.set_index('date', inplace=True)
|
1622 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1623 |
+
|
1624 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1625 |
+
predictions = []
|
1626 |
+
for i, count in enumerate(predicted_counts):
|
1627 |
+
predictions.append(count)
|
1628 |
+
|
1629 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1630 |
+
listForecast = arrayForecast.tolist()
|
1631 |
+
objectFormat['forecast'] = listForecast
|
1632 |
+
return jsonify(objectFormat)
|
1633 |
+
except Exception as e:
|
1634 |
+
print(e)
|
1635 |
+
|
1636 |
+
def predictXGBPM25():
|
1637 |
+
if request.method == 'POST':
|
1638 |
+
try:
|
1639 |
+
data = request.json
|
1640 |
+
objectFormat = data['dataUV']
|
1641 |
+
|
1642 |
+
uvData = []
|
1643 |
+
for i in objectFormat['value']:
|
1644 |
+
uvData.append(i)
|
1645 |
+
|
1646 |
+
uvTime = []
|
1647 |
+
for i in objectFormat['time']:
|
1648 |
+
uvTime.append(i)
|
1649 |
+
|
1650 |
+
# convert to numpy array and pandas dataframe
|
1651 |
+
arrayData = np.array(uvData)
|
1652 |
+
arrayTime = np.array(uvTime)
|
1653 |
+
datetimeUV = pd.to_datetime(arrayTime)
|
1654 |
+
|
1655 |
+
dataset = pd.DataFrame({'ds': datetimeUV, 'y': arrayData})
|
1656 |
+
dataset = dataset.set_index('ds')
|
1657 |
+
dataset = dataset.resample('5T').ffill()
|
1658 |
+
dataset = dataset.dropna()
|
1659 |
+
dataset = dataset.iloc[1:]
|
1660 |
+
dataset['time'] = np.arange(len(dataset))
|
1661 |
+
|
1662 |
+
X = dataset[['time']]
|
1663 |
+
y = dataset['y']
|
1664 |
+
|
1665 |
+
model_gb = XGBRegressor(**p_gb)
|
1666 |
+
model_gb.fit(X, y)
|
1667 |
+
|
1668 |
+
last_timestamp = dataset.index[-1]
|
1669 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1670 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1671 |
+
next_hour_features.set_index('date', inplace=True)
|
1672 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1673 |
+
|
1674 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1675 |
+
predictions = []
|
1676 |
+
for i, count in enumerate(predicted_counts):
|
1677 |
+
predictions.append(count)
|
1678 |
+
|
1679 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1680 |
+
listForecast = arrayForecast.tolist()
|
1681 |
+
objectFormat['forecast'] = listForecast
|
1682 |
+
return jsonify(objectFormat)
|
1683 |
+
except Exception as e:
|
1684 |
+
print(e)
|
1685 |
+
|
1686 |
+
def predictXGBUV():
|
1687 |
+
if request.method == 'POST':
|
1688 |
+
try:
|
1689 |
+
data = request.json
|
1690 |
+
objectFormat = data['dataPM25']
|
1691 |
+
|
1692 |
+
pm25Data = []
|
1693 |
+
for i in objectFormat['value']:
|
1694 |
+
pm25Data.append(i)
|
1695 |
+
|
1696 |
+
pm25Time = []
|
1697 |
+
for i in objectFormat['time']:
|
1698 |
+
pm25Time.append(i)
|
1699 |
+
|
1700 |
+
# convert to numpy array and pandas dataframe
|
1701 |
+
arrayData = np.array(pm25Data)
|
1702 |
+
arrayTime = np.array(pm25Time)
|
1703 |
+
datetimePM25 = pd.to_datetime(arrayTime)
|
1704 |
+
|
1705 |
+
dataset = pd.DataFrame({'ds': datetimePM25, 'y': arrayData})
|
1706 |
+
dataset = dataset.set_index('ds')
|
1707 |
+
dataset = dataset.resample('5T').ffill()
|
1708 |
+
dataset = dataset.dropna()
|
1709 |
+
dataset = dataset.iloc[1:]
|
1710 |
+
dataset['time'] = np.arange(len(dataset))
|
1711 |
+
|
1712 |
+
X = dataset[['time']]
|
1713 |
+
y = dataset['y']
|
1714 |
+
|
1715 |
+
model_gb = XGBRegressor(**p_gb)
|
1716 |
+
model_gb.fit(X, y)
|
1717 |
+
|
1718 |
+
last_timestamp = dataset.index[-1]
|
1719 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1720 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1721 |
+
next_hour_features.set_index('date', inplace=True)
|
1722 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1723 |
+
|
1724 |
+
predicted_counts = model_gb.predict(next_hour_features)
|
1725 |
+
predictions = []
|
1726 |
+
for i, count in enumerate(predicted_counts):
|
1727 |
+
predictions.append(count)
|
1728 |
+
|
1729 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1730 |
+
listForecast = arrayForecast.tolist()
|
1731 |
+
objectFormat['forecast'] = listForecast
|
1732 |
+
return jsonify(objectFormat)
|
1733 |
+
except Exception as e:
|
1734 |
+
print(e)
|
1735 |
+
|
1736 |
+
#-------------------RF-------------------
|
1737 |
+
def predictRFTemp():
|
1738 |
+
if request.method == 'POST':
|
1739 |
+
try:
|
1740 |
+
data = request.json
|
1741 |
+
objectFormat = data['dataTemp']
|
1742 |
+
|
1743 |
+
tempData = []
|
1744 |
+
for i in objectFormat['value']:
|
1745 |
+
tempData.append(i)
|
1746 |
+
|
1747 |
+
tempTime = []
|
1748 |
+
for i in objectFormat['time']:
|
1749 |
+
tempTime.append(i)
|
1750 |
+
|
1751 |
+
# convert to numpy array and pandas dataframe
|
1752 |
+
arrayData = np.array(tempData)
|
1753 |
+
arrayTime = np.array(tempTime)
|
1754 |
+
datetimeTemp = pd.to_datetime(arrayTime)
|
1755 |
+
|
1756 |
+
dataset = pd.DataFrame({'ds': datetimeTemp, 'y': arrayData})
|
1757 |
+
dataset = dataset.set_index('ds')
|
1758 |
+
dataset = dataset.resample('5T').ffill()
|
1759 |
+
dataset = dataset.dropna()
|
1760 |
+
dataset = dataset.iloc[1:]
|
1761 |
+
dataset['time'] = np.arange(len(dataset))
|
1762 |
+
|
1763 |
+
X = dataset[['time']]
|
1764 |
+
y = dataset['y']
|
1765 |
+
|
1766 |
+
model_rf = RandomForestRegressor(**p_rf)
|
1767 |
+
model_rf.fit(X, y)
|
1768 |
+
|
1769 |
+
last_timestamp = dataset.index[-1]
|
1770 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1771 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1772 |
+
next_hour_features.set_index('date', inplace=True)
|
1773 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1774 |
+
|
1775 |
+
predicted_counts = model_rf.predict(next_hour_features)
|
1776 |
+
predictions = []
|
1777 |
+
for i, count in enumerate(predicted_counts):
|
1778 |
+
predictions.append(count)
|
1779 |
+
|
1780 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1781 |
+
listForecast = arrayForecast.tolist()
|
1782 |
+
objectFormat['forecast'] = listForecast
|
1783 |
+
return jsonify(objectFormat)
|
1784 |
+
except Exception as e:
|
1785 |
+
print(e)
|
1786 |
+
|
1787 |
+
def predictRFHumi():
|
1788 |
+
if request.method == 'POST':
|
1789 |
+
try:
|
1790 |
+
data = request.json
|
1791 |
+
objectFormat = data['dataHumi']
|
1792 |
+
|
1793 |
+
humiData = []
|
1794 |
+
for i in objectFormat['value']:
|
1795 |
+
humiData.append(i)
|
1796 |
+
|
1797 |
+
humiTime = []
|
1798 |
+
for i in objectFormat['time']:
|
1799 |
+
humiTime.append(i)
|
1800 |
+
|
1801 |
+
# convert to numpy array and pandas dataframe
|
1802 |
+
arrayData = np.array(humiData)
|
1803 |
+
arrayTime = np.array(humiTime)
|
1804 |
+
datetimeHumi = pd.to_datetime(arrayTime)
|
1805 |
+
|
1806 |
+
dataset = pd.DataFrame({'ds': datetimeHumi, 'y': arrayData})
|
1807 |
+
dataset = dataset.set_index('ds')
|
1808 |
+
dataset = dataset.resample('5T').ffill()
|
1809 |
+
dataset = dataset.dropna()
|
1810 |
+
dataset = dataset.iloc[1:]
|
1811 |
+
dataset['time'] = np.arange(len(dataset))
|
1812 |
+
|
1813 |
+
X = dataset[['time']]
|
1814 |
+
y = dataset['y']
|
1815 |
+
|
1816 |
+
model_rf = RandomForestRegressor(**p_rf)
|
1817 |
+
model_rf.fit(X, y)
|
1818 |
+
|
1819 |
+
last_timestamp = dataset.index[-1]
|
1820 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1821 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1822 |
+
next_hour_features.set_index('date', inplace=True)
|
1823 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1824 |
+
|
1825 |
+
predicted_counts = model_rf.predict(next_hour_features)
|
1826 |
+
predictions = []
|
1827 |
+
for i, count in enumerate(predicted_counts):
|
1828 |
+
predictions.append(count)
|
1829 |
+
|
1830 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1831 |
+
listForecast = arrayForecast.tolist()
|
1832 |
+
objectFormat['forecast'] = listForecast
|
1833 |
+
return jsonify(objectFormat)
|
1834 |
+
except Exception as e:
|
1835 |
+
print(e)
|
1836 |
+
|
1837 |
+
def predictRFCO2():
|
1838 |
+
if request.method == 'POST':
|
1839 |
+
try:
|
1840 |
+
data = request.json
|
1841 |
+
objectFormat = data['dataCO2']
|
1842 |
+
|
1843 |
+
co2Data = []
|
1844 |
+
for i in objectFormat['value']:
|
1845 |
+
co2Data.append(i)
|
1846 |
+
|
1847 |
+
co2Time = []
|
1848 |
+
for i in objectFormat['time']:
|
1849 |
+
co2Time.append(i)
|
1850 |
+
|
1851 |
+
# convert to numpy array and pandas dataframe
|
1852 |
+
arrayData = np.array(co2Data)
|
1853 |
+
arrayTime = np.array(co2Time)
|
1854 |
+
datetimeCO2 = pd.to_datetime(arrayTime)
|
1855 |
+
|
1856 |
+
dataset = pd.DataFrame({'ds': datetimeCO2, 'y': arrayData})
|
1857 |
+
dataset = dataset.set_index('ds')
|
1858 |
+
dataset = dataset.resample('5T').ffill()
|
1859 |
+
dataset = dataset.dropna()
|
1860 |
+
dataset = dataset.iloc[1:]
|
1861 |
+
dataset['time'] = np.arange(len(dataset))
|
1862 |
+
|
1863 |
+
X = dataset[['time']]
|
1864 |
+
y = dataset['y']
|
1865 |
+
|
1866 |
+
model_rf = RandomForestRegressor(**p_rf)
|
1867 |
+
model_rf.fit(X, y)
|
1868 |
+
|
1869 |
+
last_timestamp = dataset.index[-1]
|
1870 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1871 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1872 |
+
next_hour_features.set_index('date', inplace=True)
|
1873 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1874 |
+
|
1875 |
+
predicted_counts = model_rf.predict(next_hour_features)
|
1876 |
+
predictions = []
|
1877 |
+
for i, count in enumerate(predicted_counts):
|
1878 |
+
predictions.append(count)
|
1879 |
+
|
1880 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1881 |
+
listForecast = arrayForecast.tolist()
|
1882 |
+
objectFormat['forecast'] = listForecast
|
1883 |
+
return jsonify(objectFormat)
|
1884 |
+
except Exception as e:
|
1885 |
+
print(e)
|
1886 |
+
|
1887 |
+
def predictRFCO():
|
1888 |
+
if request.method == 'POST':
|
1889 |
+
try:
|
1890 |
+
data = request.json
|
1891 |
+
objectFormat = data['dataCO']
|
1892 |
+
|
1893 |
+
coData = []
|
1894 |
+
for i in objectFormat['value']:
|
1895 |
+
coData.append(i)
|
1896 |
+
|
1897 |
+
coTime = []
|
1898 |
+
for i in objectFormat['time']:
|
1899 |
+
coTime.append(i)
|
1900 |
+
|
1901 |
+
# convert to numpy array and pandas dataframe
|
1902 |
+
arrayData = np.array(coData)
|
1903 |
+
arrayTime = np.array(coTime)
|
1904 |
+
datetimeCO = pd.to_datetime(arrayTime)
|
1905 |
+
|
1906 |
+
dataset = pd.DataFrame({'ds': datetimeCO, 'y': arrayData})
|
1907 |
+
dataset = dataset.set_index('ds')
|
1908 |
+
dataset = dataset.resample('5T').ffill()
|
1909 |
+
dataset = dataset.dropna()
|
1910 |
+
dataset = dataset.iloc[1:]
|
1911 |
+
dataset['time'] = np.arange(len(dataset))
|
1912 |
+
|
1913 |
+
X = dataset[['time']]
|
1914 |
+
y = dataset['y']
|
1915 |
+
|
1916 |
+
model_rf = RandomForestRegressor(**p_rf)
|
1917 |
+
model_rf.fit(X, y)
|
1918 |
+
|
1919 |
+
last_timestamp = dataset.index[-1]
|
1920 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1921 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1922 |
+
next_hour_features.set_index('date', inplace=True)
|
1923 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1924 |
+
|
1925 |
+
predicted_counts = model_rf.predict(next_hour_features)
|
1926 |
+
predictions = []
|
1927 |
+
for i, count in enumerate(predicted_counts):
|
1928 |
+
predictions.append(count)
|
1929 |
+
|
1930 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1931 |
+
listForecast = arrayForecast.tolist()
|
1932 |
+
objectFormat['forecast'] = listForecast
|
1933 |
+
return jsonify(objectFormat)
|
1934 |
+
except Exception as e:
|
1935 |
+
print(e)
|
1936 |
+
|
1937 |
+
def predictRFUV():
|
1938 |
+
if request.method == 'POST':
|
1939 |
+
try:
|
1940 |
+
data = request.json
|
1941 |
+
objectFormat = data['dataUV']
|
1942 |
+
|
1943 |
+
uvData = []
|
1944 |
+
for i in objectFormat['value']:
|
1945 |
+
uvData.append(i)
|
1946 |
+
|
1947 |
+
uvTime = []
|
1948 |
+
for i in objectFormat['time']:
|
1949 |
+
uvTime.append(i)
|
1950 |
+
|
1951 |
+
# convert to numpy array and pandas dataframe
|
1952 |
+
arrayData = np.array(uvData)
|
1953 |
+
arrayTime = np.array(uvTime)
|
1954 |
+
datetimeUV = pd.to_datetime(arrayTime)
|
1955 |
+
|
1956 |
+
dataset = pd.DataFrame({'ds': datetimeUV, 'y': arrayData})
|
1957 |
+
dataset = dataset.set_index('ds')
|
1958 |
+
dataset = dataset.resample('5T').ffill()
|
1959 |
+
dataset = dataset.dropna()
|
1960 |
+
dataset = dataset.iloc[1:]
|
1961 |
+
dataset['time'] = np.arange(len(dataset))
|
1962 |
+
|
1963 |
+
X = dataset[['time']]
|
1964 |
+
y = dataset['y']
|
1965 |
+
|
1966 |
+
model_rf = RandomForestRegressor(**p_rf)
|
1967 |
+
model_rf.fit(X, y)
|
1968 |
+
|
1969 |
+
last_timestamp = dataset.index[-1]
|
1970 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
1971 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
1972 |
+
next_hour_features.set_index('date', inplace=True)
|
1973 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
1974 |
+
|
1975 |
+
predicted_counts = model_rf.predict(next_hour_features)
|
1976 |
+
predictions = []
|
1977 |
+
for i, count in enumerate(predicted_counts):
|
1978 |
+
predictions.append(count)
|
1979 |
+
|
1980 |
+
arrayForecast = np.around(predictions, decimals=10)
|
1981 |
+
listForecast = arrayForecast.tolist()
|
1982 |
+
objectFormat['forecast'] = listForecast
|
1983 |
+
return jsonify(objectFormat)
|
1984 |
+
except Exception as e:
|
1985 |
+
print(e)
|
1986 |
+
|
1987 |
+
def predictRFPM25():
|
1988 |
+
if request.method == 'POST':
|
1989 |
+
try:
|
1990 |
+
data = request.json
|
1991 |
+
objectFormat = data['dataPM25']
|
1992 |
+
|
1993 |
+
pm25Data = []
|
1994 |
+
for i in objectFormat['value']:
|
1995 |
+
pm25Data.append(i)
|
1996 |
+
|
1997 |
+
pm25Time = []
|
1998 |
+
for i in objectFormat['time']:
|
1999 |
+
pm25Time.append(i)
|
2000 |
+
|
2001 |
+
# convert to numpy array and pandas dataframe
|
2002 |
+
arrayData = np.array(pm25Data)
|
2003 |
+
arrayTime = np.array(pm25Time)
|
2004 |
+
datetimePM25 = pd.to_datetime(arrayTime)
|
2005 |
+
|
2006 |
+
dataset = pd.DataFrame({'ds': datetimePM25, 'y': arrayData})
|
2007 |
+
dataset = dataset.set_index('ds')
|
2008 |
+
dataset = dataset.resample('5T').ffill()
|
2009 |
+
dataset = dataset.dropna()
|
2010 |
+
dataset = dataset.iloc[1:]
|
2011 |
+
dataset['time'] = np.arange(len(dataset))
|
2012 |
+
|
2013 |
+
X = dataset[['time']]
|
2014 |
+
y = dataset['y']
|
2015 |
+
|
2016 |
+
model_rf = RandomForestRegressor(**p_rf)
|
2017 |
+
model_rf.fit(X, y)
|
2018 |
+
|
2019 |
+
last_timestamp = dataset.index[-1]
|
2020 |
+
next_hour_timestamps = pd.date_range(last_timestamp, periods=12, freq='5T')
|
2021 |
+
next_hour_features = pd.DataFrame({'date': next_hour_timestamps})
|
2022 |
+
next_hour_features.set_index('date', inplace=True)
|
2023 |
+
next_hour_features['time'] = np.arange(len(next_hour_features))
|
2024 |
+
|
2025 |
+
predicted_counts = model_rf.predict(next_hour_features)
|
2026 |
+
predictions = []
|
2027 |
+
for i, count in enumerate(predicted_counts):
|
2028 |
+
predictions.append(count)
|
2029 |
+
|
2030 |
+
arrayForecast = np.around(predictions, decimals=10)
|
2031 |
+
listForecast = arrayForecast.tolist()
|
2032 |
+
objectFormat['forecast'] = listForecast
|
2033 |
+
return jsonify(objectFormat)
|
2034 |
+
except Exception as e:
|
2035 |
+
print(e)
|
aiair-server/datasets/models/lstm/bi-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_6", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 1, 12], "dtype": "float32", "sparse": false, "ragged": false, "name": "bidirectional_input"}}, {"class_name": "Bidirectional", "config": {"name": "bidirectional", "trainable": true, "batch_input_shape": [null, 1, 12], "dtype": "float32", "layer": {"class_name": "LSTM", "config": {"name": "lstm_12", "trainable": true, "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 256, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, "merge_mode": "concat"}}, {"class_name": "Bidirectional", "config": {"name": "bidirectional_1", "trainable": true, "dtype": "float32", "layer": {"class_name": "LSTM", "config": {"name": "lstm_13", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, "merge_mode": "concat"}}, {"class_name": "Dense", "config": {"name": "dense_6", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/bi_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:443face69dc60cd8619fb48f89f5a2b049e4995dc2f8d3085012bdf097d2a534
|
3 |
+
size 4868000
|
aiair-server/datasets/models/lstm/co-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_19", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_40_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_40", "trainable": true, "batch_input_shape": [null, null, 100], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_41", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 64, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_34", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_35", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/co2-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_18", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_38_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_38", "trainable": true, "batch_input_shape": [null, null, 100], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_39", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 64, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_32", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_33", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/co2_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:395b8a4e63fe03072d3180cf70402319e40554445e60dbc1228310f026d05cf4
|
3 |
+
size 695888
|
aiair-server/datasets/models/lstm/co_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:41b556aea954871b9129a99257f67d29dcafb2d6e7b0c2423c8c71adb42b5912
|
3 |
+
size 695888
|
aiair-server/datasets/models/lstm/humi-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_17", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_36_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_36", "trainable": true, "batch_input_shape": [null, null, 100], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_37", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 64, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_30", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_31", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/humi_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fffb447e33c4adff138c8d480c8539313fb403308719dfb7c94f1175a31aaf8b
|
3 |
+
size 695888
|
aiair-server/datasets/models/lstm/pm25-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_22", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_46_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_46", "trainable": true, "batch_input_shape": [null, null, 100], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_47", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 64, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_40", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_41", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/pm25_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:898ee1db33a080b749678c4e8cb98ebf4e520ea8bdedfb2c1e5e1cdfceaabdb8
|
3 |
+
size 695888
|
aiair-server/datasets/models/lstm/temp-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"module": "keras.layers", "class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_input"}, "registered_name": null}, {"module": "keras.layers", "class_name": "LSTM", "config": {"name": "lstm", "trainable": true, "dtype": "float32", "batch_input_shape": [null, null, 100], "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 256, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "recurrent_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}, "registered_name": null, "build_config": {"input_shape": [null, null, 100]}}, {"module": "keras.layers", "class_name": "Dropout", "config": {"name": "dropout", "trainable": true, "dtype": "float32", "rate": 0.2, "noise_shape": null, "seed": null}, "registered_name": null, "build_config": {"input_shape": [null, null, 256]}}, {"module": "keras.layers", "class_name": "LSTM", "config": {"name": "lstm_1", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "recurrent_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}, "registered_name": null, "build_config": {"input_shape": [null, null, 256]}}, {"module": "keras.layers", "class_name": "Dropout", "config": {"name": "dropout_1", "trainable": true, "dtype": "float32", "rate": 0.2, "noise_shape": null, "seed": null}, "registered_name": null, "build_config": {"input_shape": [null, 128]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": {"module": "keras.regularizers", "class_name": "L2", "config": {"l2": 0.009999999776482582}, "registered_name": null}, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 128]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 25]}}]}, "keras_version": "2.15.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/temp_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8df02287d654595e1cbb251b279ae7288aefb1585ff1b16ae8d589f8014b630a
|
3 |
+
size 2287968
|
aiair-server/datasets/models/lstm/temp_pc_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9be575debf9780c1492ec4c2f333178475d44e91ce4de0d4d23bf5d55c4369de
|
3 |
+
size 2286416
|
aiair-server/datasets/models/lstm/temp_pc_lstm_weight.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_2", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_5_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_5", "trainable": true, "batch_input_shape": [null, null, 100], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 256, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_6", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/test-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 1, 12], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_input"}}, {"class_name": "LSTM", "config": {"name": "lstm", "trainable": true, "batch_input_shape": [null, 1, 12], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 256, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_1", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/test_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d3f6fab409a35b04cb024a0330a92cee334dbe0a52a5b1eba97d6c9ec528363c
|
3 |
+
size 1916928
|
aiair-server/datasets/models/lstm/trick-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_3", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 1, 12], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_6_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_6", "trainable": true, "batch_input_shape": [null, 1, 12], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 256, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_7", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/trick_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b91b27e910696705f55020e32eafbfd288a4e8bbb8771a148a1611b219512677
|
3 |
+
size 1916928
|
aiair-server/datasets/models/lstm/uv-lstm.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_21", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, null, 100], "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_44_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_44", "trainable": true, "batch_input_shape": [null, null, 100], "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "LSTM", "config": {"name": "lstm_45", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 64, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense_38", "trainable": true, "dtype": "float32", "units": 25, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_39", "trainable": true, "dtype": "float32", "units": 12, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.6.0", "backend": "tensorflow"}
|
aiair-server/datasets/models/lstm/uv_lstm_weight.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a487087796e0797661b820bc70d76a2d4f8bcded3afc0453fc5b6e30517cb07c
|
3 |
+
size 695888
|
aiair-server/datasets/models/prophet-lstm/temp-bpnn-model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:179fc44cc2a6e8ac787aa80adac7384176e073a2767d4ea211006446d0fcfa4f
|
3 |
+
size 63136
|
aiair-server/datasets/models/prophet-lstm/temp-bpnn-model.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential_14", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 5], "dtype": "float32", "sparse": false, "ragged": false, "name": "dense_40_input"}}, {"class_name": "Dense", "config": {"name": "dense_40", "trainable": true, "batch_input_shape": [null, 5], "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_41", "trainable": true, "dtype": "float32", "units": 64, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_42", "trainable": true, "dtype": "float32", "units": 32, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_43", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.9.0", "backend": "tensorflow"}
|
aiair-server/requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
Flask-Cors
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
prophet
|
6 |
+
gunicorn
|
7 |
+
scikit-learn
|
8 |
+
keras
|
9 |
+
tensorflow
|
10 |
+
xgboost
|
aiair-server/routes/Predict.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint
|
2 |
+
|
3 |
+
from controllers.PredictController import PredictController
|
4 |
+
|
5 |
+
Predict = Blueprint('Predict', __name__)
|
6 |
+
|
7 |
+
Predict.route('/lr/temp', methods = ['POST'])(PredictController.predictLRTemp)
|
8 |
+
Predict.route('/lr/humi', methods = ['POST'])(PredictController.predictLRHumi)
|
9 |
+
Predict.route('/lr/co2', methods = ['POST'])(PredictController.predictLRCO2)
|
10 |
+
Predict.route('/lr/co', methods = ['POST'])(PredictController.predictLRCO)
|
11 |
+
Predict.route('/lr/uv', methods = ['POST'])(PredictController.predictLRUV)
|
12 |
+
Predict.route('/lr/pm25', methods = ['POST'])(PredictController.predictLRPM25)
|
13 |
+
|
14 |
+
Predict.route('/prophet/temp', methods = ['POST'])(PredictController.predictTempProphet)
|
15 |
+
Predict.route('/prophet/humi', methods = ['POST'])(PredictController.predictHumiProphet)
|
16 |
+
Predict.route('/prophet/co2', methods = ['POST'])(PredictController.predictCO2Prophet)
|
17 |
+
Predict.route('/prophet/co', methods = ['POST'])(PredictController.predictCOProphet)
|
18 |
+
Predict.route('/prophet/uv', methods = ['POST'])(PredictController.predictUVProphet)
|
19 |
+
Predict.route('/prophet/pm25', methods = ['POST'])(PredictController.predictPM25Prophet)
|
20 |
+
|
21 |
+
Predict.route('/prophet-lstm/temp', methods = ['POST'])(PredictController.predictTempProphetLSTM)
|
22 |
+
|
23 |
+
Predict.route('/lstm/temp', methods = ['POST'])(PredictController.predictTempLSTM)
|
24 |
+
Predict.route('/lstm/humi', methods = ['POST'])(PredictController.predictHumiLSTM)
|
25 |
+
Predict.route('/lstm/co2', methods = ['POST'])(PredictController.predictCO2LSTM)
|
26 |
+
Predict.route('/lstm/co', methods = ['POST'])(PredictController.predictCOLSTM)
|
27 |
+
Predict.route('/lstm/uv', methods = ['POST'])(PredictController.predictUVLSTM)
|
28 |
+
Predict.route('/lstm/pm25', methods = ['POST'])(PredictController.predictPM25LSTM)
|
29 |
+
|
30 |
+
Predict.route('/gb/temp', methods = ['POST'])(PredictController.predictGBTemp)
|
31 |
+
Predict.route('/gb/humi', methods = ['POST'])(PredictController.predictGBHumi)
|
32 |
+
Predict.route('/gb/co2', methods = ['POST'])(PredictController.predictGBCO2)
|
33 |
+
Predict.route('/gb/co', methods = ['POST'])(PredictController.predictGBCO)
|
34 |
+
Predict.route('/gb/uv', methods = ['POST'])(PredictController.predictGBUV)
|
35 |
+
Predict.route('/gb/pm25', methods = ['POST'])(PredictController.predictGBPM25)
|
36 |
+
|
37 |
+
Predict.route('/xgb/temp', methods = ['POST'])(PredictController.predictXGBTemp)
|
38 |
+
Predict.route('/xgb/humi', methods = ['POST'])(PredictController.predictXGBHumi)
|
39 |
+
Predict.route('/xgb/co2', methods = ['POST'])(PredictController.predictXGBCO2)
|
40 |
+
Predict.route('/xgb/co', methods = ['POST'])(PredictController.predictXGBCO)
|
41 |
+
Predict.route('/xgb/uv', methods = ['POST'])(PredictController.predictXGBUV)
|
42 |
+
Predict.route('/xgb/pm25', methods = ['POST'])(PredictController.predictXGBPM25)
|
43 |
+
|
44 |
+
Predict.route('/rf/temp', methods = ['POST'])(PredictController.predictRFTemp)
|
45 |
+
Predict.route('/rf/humi', methods = ['POST'])(PredictController.predictRFHumi)
|
46 |
+
Predict.route('/rf/co2', methods = ['POST'])(PredictController.predictRFCO2)
|
47 |
+
Predict.route('/rf/co', methods = ['POST'])(PredictController.predictRFCO)
|
48 |
+
Predict.route('/rf/uv', methods = ['POST'])(PredictController.predictRFUV)
|
49 |
+
Predict.route('/rf/pm25', methods = ['POST'])(PredictController.predictRFPM25)
|
aiair-server/routes/Router.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from routes.Predict import Predict
|
2 |
+
|
3 |
+
class Router:
|
4 |
+
def run(app):
|
5 |
+
app.register_blueprint(Predict, url_prefix = '/predict')
|
6 |
+
|