Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ISC4_IA_ML
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kevineri.bonga
ISC4_IA_ML
Commits
98a46b47
Commit
98a46b47
authored
8 months ago
by
Kevin Bonga
Browse files
Options
Downloads
Patches
Plain Diff
version 1.2 du tp2
parent
95966a2a
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
exos/tp2_decision_tree/pmc.py
+9
-4
9 additions, 4 deletions
exos/tp2_decision_tree/pmc.py
with
9 additions
and
4 deletions
exos/tp2_decision_tree/pmc.py
+
9
−
4
View file @
98a46b47
...
@@ -5,6 +5,7 @@ from sklearn.metrics import accuracy_score
...
@@ -5,6 +5,7 @@ from sklearn.metrics import accuracy_score
from
keras.models
import
Sequential
from
keras.models
import
Sequential
from
keras.layers
import
Dense
,
Input
from
keras.layers
import
Dense
,
Input
from
keras.utils
import
to_categorical
from
keras.utils
import
to_categorical
import
tensorflow
as
tf
def
load_data
(
file_path
):
def
load_data
(
file_path
):
column_names
=
[
'
sepal_length
'
,
'
sepal_width
'
,
'
petal_length
'
,
'
petal_width
'
,
'
species
'
]
column_names
=
[
'
sepal_length
'
,
'
sepal_width
'
,
'
petal_length
'
,
'
petal_width
'
,
'
species
'
]
...
@@ -24,13 +25,17 @@ def preprocess_data(X, y):
...
@@ -24,13 +25,17 @@ def preprocess_data(X, y):
def
create_model
():
def
create_model
():
model
=
Sequential
()
model
=
Sequential
()
model
.
add
(
Input
(
shape
=
(
4
,)))
model
.
add
(
Input
(
shape
=
(
4
,)))
model
.
add
(
Dense
(
4
,
activation
=
'
relu
'
))
model
.
add
(
Dense
(
8
,
activation
=
'
relu
'
))
model
.
add
(
Dense
(
4
,
activation
=
'
relu
'
))
model
.
add
(
Dense
(
8
,
activation
=
'
relu
'
))
model
.
add
(
Dense
(
3
,
activation
=
'
softmax
'
))
model
.
add
(
Dense
(
3
,
activation
=
'
softmax
'
))
model
.
compile
(
loss
=
'
categorical_crossentropy
'
,
optimizer
=
'
adam
'
,
metrics
=
[
'
accuracy
'
])
model
.
compile
(
loss
=
'
categorical_crossentropy
'
,
optimizer
=
'
adam
'
,
metrics
=
[
'
accuracy
'
])
return
model
return
model
def
cross_validate_model
(
X
,
y
,
n_splits
=
5
,
epochs
=
10
,
batch_size
=
20
):
@tf.function
def
train_model
(
model
,
X_train
,
y_train
,
epochs
,
batch_size
):
model
.
fit
(
X_train
,
y_train
,
epochs
=
epochs
,
batch_size
=
batch_size
,
verbose
=
0
)
def
cross_validate_model
(
X
,
y
,
n_splits
=
5
,
epochs
=
50
,
batch_size
=
10
):
kf
=
KFold
(
n_splits
=
n_splits
,
shuffle
=
True
,
random_state
=
42
)
kf
=
KFold
(
n_splits
=
n_splits
,
shuffle
=
True
,
random_state
=
42
)
accuracies
=
[]
accuracies
=
[]
for
fold
,
(
train_index
,
test_index
)
in
enumerate
(
kf
.
split
(
X
)):
for
fold
,
(
train_index
,
test_index
)
in
enumerate
(
kf
.
split
(
X
)):
...
@@ -38,7 +43,7 @@ def cross_validate_model(X, y, n_splits=5, epochs=10, batch_size=20):
...
@@ -38,7 +43,7 @@ def cross_validate_model(X, y, n_splits=5, epochs=10, batch_size=20):
X_train
,
X_test
=
X
[
train_index
],
X
[
test_index
]
X_train
,
X_test
=
X
[
train_index
],
X
[
test_index
]
y_train
,
y_test
=
y
[
train_index
],
y
[
test_index
]
y_train
,
y_test
=
y
[
train_index
],
y
[
test_index
]
model
=
create_model
()
model
=
create_model
()
model
.
fit
(
X_train
,
y_train
,
epochs
=
epochs
,
batch_size
=
batch_size
,
verbose
=
0
)
train_model
(
model
,
X_train
,
y_train
,
epochs
,
batch_size
)
y_pred
=
model
.
predict
(
X_test
)
y_pred
=
model
.
predict
(
X_test
)
y_pred_classes
=
y_pred
.
argmax
(
axis
=
1
)
y_pred_classes
=
y_pred
.
argmax
(
axis
=
1
)
y_test_classes
=
y_test
.
argmax
(
axis
=
1
)
y_test_classes
=
y_test
.
argmax
(
axis
=
1
)
...
...
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