Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-211
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-211
2022-211
Commits
9672efc1
Commit
9672efc1
authored
Oct 13, 2022
by
H.C.K. De Silva
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
90% complete
parent
1a763ff8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
122 additions
and
0 deletions
+122
-0
test_hybrid.py
test_hybrid.py
+122
-0
No files found.
test_hybrid.py
0 → 100644
View file @
9672efc1
import
numpy
as
np
import
pandas
as
pd
from
keras.models
import
load_model
from
statsmodels.tsa.arima.model
import
ARIMAResults
from
datetime
import
date
,
timedelta
import
warnings
warnings
.
filterwarnings
(
"ignore"
)
df
=
pd
.
read_csv
(
'out_new.csv'
)
df
.
Month
=
pd
.
to_datetime
(
df
.
Month
)
df
=
df
.
set_index
(
"Month"
)
train_data
=
df
[:
len
(
df
)
-
12
]
test_data
=
df
[
len
(
df
)
-
12
:]
next_12_months
=
[]
rain_zero_data
=
np
.
zeros
(
12
,
int
)
first_day_of_this_month
=
date
.
today
()
.
replace
(
day
=
1
)
next_12_months
.
append
(
first_day_of_this_month
.
strftime
(
"
%
Y-
%
m-
%
d"
))
for
i
in
range
(
1
,
12
):
if
i
==
1
:
temp
=
first_day_of_this_month
start_day_of_next_month
=
(
temp
+
timedelta
(
days
=
32
))
.
replace
(
day
=
1
)
temp
=
start_day_of_next_month
# For printing results
next_12_months
.
append
(
temp
.
strftime
(
"
%
Y-
%
m-
%
d"
))
print
(
next_12_months
)
print
(
rain_zero_data
)
x
=
pd
.
DataFrame
(
list
(
zip
(
next_12_months
,
rain_zero_data
)),
columns
=
[
'Month'
,
'Monthly rain'
])
x
.
Month
=
pd
.
to_datetime
(
x
.
Month
)
x
=
x
.
set_index
(
"Month"
)
print
(
'x type'
,
type
(
x
))
print
(
'test type'
,
type
(
test_data
))
print
(
'x info'
,
x
.
info
())
print
(
'test data info'
,
test_data
.
info
())
arima_pred_result
=
[]
lstm_pred_result
=
[]
fbp_pred_result
=
[]
print
(
'-------------------arima----------------------'
)
arima_result
=
ARIMAResults
.
load
(
'hybrid_trained_models/arima_model.pkl'
)
arima_pred
=
arima_result
.
predict
(
start
=
len
(
df
),
end
=
len
(
df
)
+
11
,
typ
=
"levels"
)
.
rename
(
"ARIMA Predictions"
)
print
(
arima_pred
)
for
x
in
range
(
len
(
arima_pred
)):
arima_pred_result
.
append
(
round
(
arima_pred
[
x
],
2
))
print
(
'-------------------lstm----------------------'
)
from
sklearn.preprocessing
import
MinMaxScaler
scaler
=
MinMaxScaler
()
scaler
.
fit
(
train_data
)
scaled_train_data
=
scaler
.
transform
(
df
)
lstm_model
=
load_model
(
'hybrid_trained_models/lstm_model.h5'
)
n_input
=
12
n_features
=
1
lstm_predictions_scaled
=
list
()
batch
=
scaled_train_data
[
-
n_input
:]
current_batch
=
batch
.
reshape
((
1
,
n_input
,
n_features
))
for
i
in
range
(
len
(
test_data
)):
lstm_pred
=
lstm_model
.
predict
(
current_batch
)[
0
]
lstm_predictions_scaled
.
append
(
lstm_pred
)
current_batch
=
np
.
append
(
current_batch
[:,
1
:,
:],
[[
lstm_pred
]],
axis
=
1
)
lstm_predictions
=
scaler
.
inverse_transform
(
lstm_predictions_scaled
)
lstm_predictions_list
=
lstm_predictions
.
tolist
()
print
(
'type'
,
type
(
lstm_predictions_list
))
print
(
lstm_predictions_list
)
for
x
in
range
(
len
(
lstm_predictions_list
)):
lstm_pred_result
.
append
(
round
(
lstm_predictions_list
[
x
][
0
],
2
))
print
(
'-------------------fb----------------------'
)
from
prophet.serialize
import
model_to_json
,
model_from_json
with
open
(
'hybrid_trained_models/fbp_model.json'
,
'r'
)
as
fin
:
m
=
model_from_json
(
fin
.
read
())
# Load model
future
=
m
.
make_future_dataframe
(
periods
=
12
,
freq
=
'MS'
)
prophet_pred
=
m
.
predict
(
future
)
for
i
in
prophet_pred
[
-
12
:][
"yhat"
]:
fbp_pred_result
.
append
(
round
(
i
,
2
))
prophet_pred
=
pd
.
DataFrame
({
"Date"
:
prophet_pred
[
-
12
:][
'ds'
],
"Pred"
:
prophet_pred
[
-
12
:][
"yhat"
]})
prophet_pred
=
prophet_pred
.
set_index
(
"Date"
)
prophet_pred
.
index
.
freq
=
"MS"
print
(
prophet_pred
)
print
(
'-------------------hybrid result----------------------'
)
print
(
'arima result : '
)
print
(
arima_pred_result
)
print
(
'lstm result : '
)
print
(
lstm_pred_result
)
print
(
'fbp result : '
)
print
(
fbp_pred_result
)
hybrid_result_list
=
[]
for
i
in
range
(
len
(
arima_pred
)):
hybrid_result_list
.
append
(
round
((
arima_pred_result
[
i
]
+
lstm_pred_result
[
i
]
+
fbp_pred_result
[
i
])
/
3
,
2
))
print
(
'Hybrid result : '
)
print
(
hybrid_result_list
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment