Skip to content
Snippets Groups Projects
Commit 98a46b47 authored by Kevin Bonga's avatar Kevin Bonga
Browse files

version 1.2 du tp2

parent 95966a2a
No related branches found
No related tags found
No related merge requests found
...@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment