Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CSCT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dsa2-gunasinha
CSCT
Commits
3ffc07c4
Commit
3ffc07c4
authored
2 years ago
by
amilashanaka
Browse files
Options
Downloads
Patches
Plain Diff
lstm and optimize
parent
bf62714d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
model.py
+203
-1
203 additions, 1 deletion
model.py
with
203 additions
and
1 deletion
model.py
+
203
−
1
View file @
3ffc07c4
...
@@ -108,4 +108,206 @@ plt.show()
...
@@ -108,4 +108,206 @@ plt.show()
# Monthly pivot table
# Monthly pivot table
df
=
(
df
.
pivot
(
index
=
'
Year
'
,
columns
=
'
Month
'
,
values
=
'
OrderDemand
'
))
df
=
(
df
.
pivot
(
index
=
'
Year
'
,
columns
=
'
Month
'
,
values
=
'
OrderDemand
'
))
df
=
df
.
loc
[:,
[
'
Jan
'
,
'
Feb
'
,
'
Mar
'
,
'
Apr
'
,
'
May
'
,
'
Jun
'
,
'
Jul
'
,
'
Aug
'
,
'
Sep
'
,
'
Oct
'
,
'
Nov
'
,
'
Dec
'
]]
df
=
df
.
loc
[:,
[
'
Jan
'
,
'
Feb
'
,
'
Mar
'
,
'
Apr
'
,
'
May
'
,
'
Jun
'
,
'
Jul
'
,
'
Aug
'
,
'
Sep
'
,
'
Oct
'
,
'
Nov
'
,
'
Dec
'
]]
df
print
(
df
)
\ No newline at end of file
custom_dict
=
{
'
Jan
'
:
0
,
'
Feb
'
:
1
,
'
Mar
'
:
2
,
'
Apr
'
:
3
,
'
May
'
:
4
,
'
Jun
'
:
5
,
'
Jul
'
:
6
,
'
Aug
'
:
7
,
'
Sep
'
:
8
,
'
Oct
'
:
9
,
'
Nov
'
:
10
,
'
Dec
'
:
11
}
temp_data
=
data
.
copy
()
temp_data
.
Month
.
replace
([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
],
[
'
Jan
'
,
'
Feb
'
,
'
Mar
'
,
'
Apr
'
,
'
May
'
,
'
Jun
'
,
'
Jul
'
,
'
Aug
'
,
'
Sep
'
,
'
Oct
'
,
'
Nov
'
,
'
Dec
'
],
inplace
=
True
)
df
=
temp_data
[[
"
OrderDemand
"
,
'
Month
'
,
'
Year
'
]].
groupby
([
"
Year
"
,
"
Month
"
]).
sum
().
reset_index
().
sort_values
(
by
=
[
'
Year
'
,
'
Month
'
],
ascending
=
True
)
df
=
df
.
iloc
[
df
[
'
Month
'
].
map
(
custom_dict
).
argsort
()]
f
,
ax
=
plt
.
subplots
(
figsize
=
(
15
,
5
))
sns
.
pointplot
(
x
=
'
Month
'
,
y
=
"
OrderDemand
"
,
data
=
df
,
hue
=
"
Year
"
)
plt
.
show
()
print
(
df
.
describe
())
df
=
data
[[
"
OrderDemand
"
,
'
Year
'
,
'
Warehouse
'
]].
groupby
([
"
Year
"
,
"
Warehouse
"
]).
sum
().
reset_index
().
sort_values
(
by
=
[
'
Warehouse
'
,
'
Year
'
],
ascending
=
False
)
f
,
ax
=
plt
.
subplots
(
figsize
=
(
15
,
5
))
sns
.
pointplot
(
x
=
'
Year
'
,
y
=
"
OrderDemand
"
,
data
=
df
,
hue
=
"
Warehouse
"
)
plt
.
show
()
df
=
(
df
.
pivot
(
index
=
'
Year
'
,
columns
=
'
Warehouse
'
,
values
=
'
OrderDemand
'
))
print
(
df
)
print
(
df
.
describe
())
#Product Category Based Analysis
df
=
data
[[
"
OrderDemand
"
,
'
ProductCategory
'
,
'
Warehouse
'
]].
groupby
([
"
ProductCategory
"
,
"
Warehouse
"
]).
sum
().
reset_index
().
sort_values
(
by
=
[
'
OrderDemand
'
],
ascending
=
False
)
df
=
df
.
pivot
(
index
=
'
ProductCategory
'
,
columns
=
'
Warehouse
'
,
values
=
'
OrderDemand
'
)
print
(
df
)
#Forecast the Order Demand with LSTM Model
df
=
data
[(
data
[
'
Date
'
]
>=
'
2012-01-01
'
)
&
(
data
[
'
Date
'
]
<=
'
2016-12-31
'
)].
sort_values
(
'
Date
'
,
ascending
=
True
)
df
=
df
.
groupby
(
'
Date
'
)[
'
OrderDemand
'
].
sum
().
reset_index
()
print
(
df
)
# Visualize the order demand as time series
plt
.
figure
(
figsize
=
(
16
,
8
))
plt
.
title
(
"
Order Demand Graph
"
)
plt
.
plot
(
df
[
"
Date
"
],
df
[
"
OrderDemand
"
])
plt
.
xlabel
(
"
Time
"
,
fontsize
=
14
,)
plt
.
ylabel
(
"
Order Demand
"
,
fontsize
=
14
)
plt
.
show
()
# Create new data with only the "OrderDemand" column
orderD
=
df
.
filter
([
"
OrderDemand
"
])
# Convert the dataframe to a np array
orderD_array
=
orderD
.
values
# See the train data len
train_close_len
=
math
.
ceil
(
len
(
orderD_array
)
*
0.8
)
print
(
train_close_len
)
# Normalize the data
scaler
=
MinMaxScaler
()
scaled_data
=
scaler
.
fit_transform
(
orderD_array
)
print
(
scaled_data
)
# Create the training dataset
train_data
=
scaled_data
[
0
:
train_close_len
,
:]
# Create X_train and y_train
X_train
=
[]
y_train
=
[]
for
i
in
range
(
60
,
len
(
train_data
)):
X_train
.
append
(
train_data
[
i
-
60
:
i
,
0
])
y_train
.
append
(
train_data
[
i
,
0
])
if
i
<=
60
:
print
(
X_train
)
print
(
y_train
)
# make X_train and y_train np array
X_train
,
y_train
=
np
.
array
(
X_train
),
np
.
array
(
y_train
)
# reshape the data
X_train
=
np
.
reshape
(
X_train
,
(
X_train
.
shape
[
0
],
X_train
.
shape
[
1
],
1
))
print
(
X_train
.
shape
)
# create the testing dataset
test_data
=
scaled_data
[
train_close_len
-
60
:
,
:]
# create X_test and y_test
X_test
=
[]
y_test
=
df
.
iloc
[
train_close_len
:
,
:]
for
i
in
range
(
60
,
len
(
test_data
)):
X_test
.
append
(
test_data
[
i
-
60
:
i
,
0
])
# convert the test data to a np array and reshape the test data
X_test
=
np
.
array
(
X_test
)
X_test
=
np
.
reshape
(
X_test
,
(
X_test
.
shape
[
0
],
X_test
.
shape
[
1
],
1
))
# Build the LSTM Model
model
=
Sequential
()
model
.
add
(
LSTM
(
units
=
512
,
return_sequences
=
True
,
activation
=
'
relu
'
,
input_shape
=
(
X_train
.
shape
[
1
],
1
)))
model
.
add
(
LSTM
(
units
=
256
,
activation
=
'
relu
'
,
return_sequences
=
False
))
model
.
add
(
Dense
(
units
=
1
))
# compile the LSTM model
model
.
compile
(
optimizer
=
"
Adam
"
,
loss
=
"
mean_squared_error
"
,
metrics
=
[
'
mae
'
])
# train the LSTM model
model
.
fit
(
X_train
,
y_train
,
epochs
=
3
,
batch_size
=
100
,
verbose
=
1
)
# predict with LSTM model
predictions
=
model
.
predict
(
X_test
)
predictions
=
scaler
.
inverse_transform
(
predictions
)
# plot the data
train
=
orderD
[:
train_close_len
]
valid
=
orderD
[
train_close_len
:]
valid
[
"
Predictions
"
]
=
predictions
#visualize the data
plt
.
figure
(
figsize
=
(
16
,
8
))
plt
.
title
(
"
Forecast with LSTM Model
"
)
plt
.
xlabel
(
"
Time
"
,
fontsize
=
14
)
plt
.
ylabel
(
"
Order Demand
"
,
fontsize
=
14
)
plt
.
plot
(
df
[
"
Date
"
][:
train_close_len
],
train
[
"
OrderDemand
"
])
plt
.
plot
(
df
[
"
Date
"
][
train_close_len
:],
valid
[[
"
OrderDemand
"
,
"
Predictions
"
]])
plt
.
legend
([
"
Train
"
,
"
Validation
"
,
"
Predictions
"
],
loc
=
"
lower right
"
)
plt
.
show
()
# change the parameters of first LSTM model and build the Optimized LSTM Model
optimized_model
=
Sequential
()
optimized_model
.
add
(
LSTM
(
512
,
activation
=
'
relu
'
,
return_sequences
=
True
,
input_shape
=
(
X_train
.
shape
[
1
],
1
)))
optimized_model
.
add
(
LSTM
(
256
,
activation
=
'
relu
'
,
return_sequences
=
False
))
optimized_model
.
add
(
Dense
(
128
))
optimized_model
.
add
(
Dense
(
64
))
optimized_model
.
add
(
Dense
(
32
))
optimized_model
.
add
(
Dense
(
1
))
# compile the model
optimized_model
.
compile
(
optimizer
=
"
Adam
"
,
loss
=
"
mean_squared_error
"
,
metrics
=
[
'
mae
'
])
# train the optimized model
optimized_model
.
fit
(
X_train
,
y_train
,
batch_size
=
32
,
epochs
=
20
,
verbose
=
1
)
# Predict with optimized LSTM model
o_predictions
=
optimized_model
.
predict
(
X_test
)
o_predictions
=
scaler
.
inverse_transform
(
o_predictions
)
# plot the data
train
=
orderD
[:
train_close_len
]
valid
=
orderD
[
train_close_len
:]
valid
[
"
Predictions
"
]
=
o_predictions
#visualize the data
plt
.
figure
(
figsize
=
(
16
,
8
))
plt
.
title
(
"
Forecast with Optimized LSTM Model
"
)
plt
.
xlabel
(
"
Time
"
,
fontsize
=
14
)
plt
.
ylabel
(
"
Order Demand
"
,
fontsize
=
14
)
plt
.
plot
(
df
[
"
Date
"
][:
train_close_len
],
train
[
"
OrderDemand
"
])
plt
.
plot
(
df
[
"
Date
"
][
train_close_len
:],
valid
[[
"
OrderDemand
"
,
"
Predictions
"
]])
plt
.
legend
([
"
Train
"
,
"
Validation
"
,
"
Predictions
"
],
loc
=
"
upper right
"
)
plt
.
show
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment