Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-089
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
Deshini Perera
2022-089
Commits
9dd1e4b6
Commit
9dd1e4b6
authored
Oct 08, 2022
by
Deshini Perera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
db6c091b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
0 deletions
+86
-0
predictor.py
predictor.py
+86
-0
No files found.
predictor.py
0 → 100644
View file @
9dd1e4b6
import
pandas
as
pd
import
numpy
as
np
from
statsmodels.tsa.seasonal
import
seasonal_decompose
from
sklearn.preprocessing
import
MinMaxScaler
from
keras.preprocessing.sequence
import
TimeseriesGenerator
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
keras.layers
import
LSTM
import
tensorflow
as
tf
model
=
tf
.
keras
.
models
.
load_model
(
'model.h5'
)
def
getData
(
month_count
):
#df = pd.read_csv('new_data.csv', index_col='Date', parse_dates=True)
df
=
pd
.
read_csv
(
'new_data.csv'
,
sep
=
","
,
index_col
=
'Date'
,
parse_dates
=
[
'Date'
])
df
.
index
.
freq
=
'MS'
df
.
head
()
results
=
seasonal_decompose
(
df
[
'Bill'
])
train
=
df
.
iloc
[:
174
]
# test = df.iloc[160:]
scaler
=
MinMaxScaler
()
scaler
.
fit
(
train
)
scaled_train
=
scaler
.
transform
(
train
)
# scaled_test = scaler.transform(test)
n_features
=
1
n_input
=
12
generator
=
TimeseriesGenerator
(
scaled_train
,
scaled_train
,
length
=
n_input
,
batch_size
=
1
)
# define model
model
=
Sequential
()
model
.
add
(
LSTM
(
100
,
activation
=
'relu'
,
input_shape
=
(
n_input
,
n_features
)))
model
.
add
(
Dense
(
1
))
model
.
compile
(
optimizer
=
'adam'
,
loss
=
'mse'
)
model
.
summary
()
# model.fit(generator, epochs=25)
# loss_per_epoch = model.history.history['loss']
# plt.plot(range(len(loss_per_epoch)),loss_per_epoch)
last_train_batch
=
scaled_train
[
-
12
:]
last_train_batch
=
last_train_batch
.
reshape
((
1
,
n_input
,
n_features
))
#model.save('model.h5')
model
.
predict
(
last_train_batch
)
test_predictions
=
[]
first_eval_batch
=
scaled_train
[
-
n_input
:]
current_batch
=
first_eval_batch
.
reshape
((
1
,
n_input
,
n_features
))
# print("length is " + str(len(test)))
value
=
int
(
month_count
)
for
i
in
range
(
value
):
# get the prediction value for the first batch
current_pred
=
model
.
predict
(
current_batch
)[
0
]
# append the prediction into the array
test_predictions
.
append
(
current_pred
)
# use the prediction to update the batch and remove the first value
current_batch
=
np
.
append
(
current_batch
[:,
1
:,
:],
[[
current_pred
]],
axis
=
1
)
# test.head()
true_predictions
=
scaler
.
inverse_transform
(
test_predictions
)
tt
=
true_predictions
.
reshape
(
-
1
)
data
=
[]
i
=
0
for
x
in
tt
:
obj
=
{
'index'
:
i
,
'value'
:
x
}
i
=
i
+
1
data
.
append
(
obj
)
return
data
# print(getData(6))
\ No newline at end of file
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