Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
Vorhersage der Verkehrslage CVH
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
Container 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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Frederic Aust
Vorhersage der Verkehrslage CVH
Commits
5a130050
Commit
5a130050
authored
4 years ago
by
Christoph Olberding
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
59b5d825
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Imputer/impute_median.py
+90
-0
90 additions, 0 deletions
Imputer/impute_median.py
with
90 additions
and
0 deletions
Imputer/impute_median.py
0 → 100644
+
90
−
0
View file @
5a130050
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 29 09:04:41 2021
@author: Christoph
"""
import
pandas
as
pd
import
numpy
as
np
from
tensorflow.keras.models
import
Sequential
from
tensorflow.keras.layers
import
Dense
from
sklearn.model_selection
import
train_test_split
np
.
random
.
seed
(
42
)
data
=
pd
.
read_csv
(
'
data_unfilled.csv
'
)
data
=
data
.
iloc
[:,
1
:]
#komplette Daten fürs Training:
data_cmpl
=
data
.
loc
[
data
[
'
Streckenvorhersage.Dauer
'
]
!=
0
]
X_cmpl
=
data_cmpl
[[
'
Streckenvorhersage.ZielortID
'
,
'
Streckenvorhersage.StartortID
'
,
'
time
'
,
'
weekday
'
]]
Y_cmpl
=
data_cmpl
[
'
Streckenvorhersage.Dauer
'
]
X_cmpl_train
,
X_cmpl_test
,
y_cmpl_train
,
y_cmpl_test
=
train_test_split
(
X_cmpl
,
Y_cmpl
,
test_size
=
0.2
)
# fehlende Daten für Test:
data_incmpl
=
data
.
loc
[
data
[
'
Streckenvorhersage.Dauer
'
]
==
0
]
X_incmpl
=
data_incmpl
[[
'
Streckenvorhersage.ZielortID
'
,
'
Streckenvorhersage.StartortID
'
,
'
time
'
,
'
weekday
'
]]
Y_incmpl
=
data_incmpl
[
'
Streckenvorhersage.Dauer
'
]
#Prediction anhand vorhandener Daten
myANN
=
Sequential
()
myANN
.
add
(
Dense
(
80
,
activation
=
'
relu
'
,
input_dim
=
X_cmpl
.
shape
[
1
]))
myANN
.
add
(
Dense
(
50
,
activation
=
'
relu
'
))
myANN
.
add
(
Dense
(
30
,
activation
=
'
relu
'
))
myANN
.
add
(
Dense
(
1
,
activation
=
'
linear
'
))
myANN
.
compile
(
loss
=
'
mean_squared_error
'
,
optimizer
=
'
adam
'
)
myANN
.
fit
(
X_cmpl_train
,
y_cmpl_train
,
epochs
=
100
,
shuffle
=
True
,
verbose
=
False
)
yp
=
myANN
.
predict
(
X_cmpl_test
)
yp
=
np
.
squeeze
(
yp
)
yDiff
=
yp
-
y_cmpl_test
print
(
'
Mittlere Abweichung auf fehlende Daten: %e
'
%
(
np
.
mean
(
np
.
abs
(
yDiff
))))
#impute Dauer auf vorhandenen Daten indem er jede reihe durchgeht und den durchschnittswert den er in den Originaldaten hat berechnet.
for
index
,
row
in
data_incmpl
.
iterrows
():
relevant_data
=
data_cmpl
[
data_cmpl
[
'
time
'
]
==
row
[
'
time
'
]]
data_incmpl
.
loc
[
index
,
'
Streckenvorhersage.Dauer
'
]
=
pd
.
DataFrame
.
median
(
relevant_data
[
'
Streckenvorhersage.Dauer
'
])
data_incmpl
=
data_incmpl
[
~
np
.
isnan
(
data_incmpl
[
'
Streckenvorhersage.Dauer
'
])]
# testing here
X_incmpl
=
data_incmpl
[[
'
Streckenvorhersage.ZielortID
'
,
'
Streckenvorhersage.StartortID
'
,
'
time
'
,
'
weekday
'
]]
Y_incmpl
=
data_incmpl
[
'
Streckenvorhersage.Dauer
'
]
#größere Testmenge aus den originaldaten suchen
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X_cmpl
,
Y_cmpl
,
test_size
=
0.35
)
y_train
=
pd
.
DataFrame
(
data
=
y_train
,
columns
=
[
'
Streckenvorhersage.Dauer
'
])
X_train
=
X_train
.
append
(
X_incmpl
)
Y_incmpl
=
pd
.
DataFrame
(
data
=
Y_incmpl
,
columns
=
[
'
Streckenvorhersage.Dauer
'
])
y_train
=
pd
.
concat
([
y_train
,
Y_incmpl
])
myANN
=
Sequential
()
myANN
.
add
(
Dense
(
80
,
activation
=
'
relu
'
,
input_dim
=
X_cmpl
.
shape
[
1
]))
myANN
.
add
(
Dense
(
50
,
activation
=
'
relu
'
))
myANN
.
add
(
Dense
(
30
,
activation
=
'
relu
'
))
myANN
.
add
(
Dense
(
1
,
activation
=
'
linear
'
))
myANN
.
compile
(
loss
=
'
mean_squared_error
'
,
optimizer
=
'
adam
'
)
#ANN mit gefüllten Daten als trainingsmenge
myANN
.
fit
(
X_train
,
y_train
,
epochs
=
100
,
shuffle
=
True
,
verbose
=
False
)
yp
=
myANN
.
predict
(
X_test
)
yp
=
np
.
squeeze
(
yp
)
yDiff
=
yp
-
y_test
print
(
'
Mittlere Abweichung mit aufgefüllten Daten(mean): %e
'
%
(
np
.
mean
(
np
.
abs
(
yDiff
))))
#das Data-DateaFrame mit den gefüllten Daten füllen
y_test
=
pd
.
DataFrame
(
data
=
y_test
,
columns
=
[
'
Streckenvorhersage.Dauer
'
])
X_all
=
X_train
.
append
(
X_test
)
y_all
=
pd
.
concat
([
y_train
,
y_test
])
data
=
X_all
y_all
=
np
.
asarray
(
y_all
)
data
[
'
Streckenvorhersage.Dauer
'
]
=
y_all
data
.
to_csv
(
'
data_filled(median).csv
'
)
# Die gefülten Daten in einer neuen csv abspeichern
\ No newline at end of file
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