mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-27 06:54:18 +01:00
Refactor code for improved readability and consistency across notebooks
- Standardized spacing around operators and function arguments in TP7_Kmeans.ipynb and neural_network.ipynb. - Enhanced the formatting of model building and training code in neural_network.ipynb for better clarity. - Updated the pyproject.toml to remove a specific TensorFlow version and added linting configuration for Ruff. - Improved comments and organization in the code to facilitate easier understanding and maintenance.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np \n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
@@ -178,16 +178,21 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def build_model():\n",
|
||||
" model = tf.keras.models.Sequential([\n",
|
||||
" tf.keras.layers.Dense(16, activation='relu', input_shape=(X.shape[1],), kernel_regularizer=tf.keras.regularizers.l2(0.01)),\n",
|
||||
" tf.keras.layers.Dense(8, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)),\n",
|
||||
" tf.keras.layers.Dense(1, activation='sigmoid')\n",
|
||||
" ])\n",
|
||||
" model.compile(\n",
|
||||
" optimizer='adam',\n",
|
||||
" loss='binary_crossentropy',\n",
|
||||
" metrics=['accuracy']\n",
|
||||
" model = tf.keras.models.Sequential(\n",
|
||||
" [\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 16,\n",
|
||||
" activation=\"relu\",\n",
|
||||
" input_shape=(X.shape[1],),\n",
|
||||
" kernel_regularizer=tf.keras.regularizers.l2(0.01),\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 8, activation=\"relu\", kernel_regularizer=tf.keras.regularizers.l2(0.01)\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(1, activation=\"sigmoid\"),\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
" model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n",
|
||||
" return model"
|
||||
]
|
||||
},
|
||||
@@ -291,10 +296,7 @@
|
||||
"histories = []\n",
|
||||
"\n",
|
||||
"early_stopping = EarlyStopping(\n",
|
||||
" monitor='val_loss',\n",
|
||||
" patience=10,\n",
|
||||
" restore_best_weights=True,\n",
|
||||
" verbose=1\n",
|
||||
" monitor=\"val_loss\", patience=10, restore_best_weights=True, verbose=1\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"for fold, (train_idx, val_idx) in enumerate(skf.split(X, y), 1):\n",
|
||||
@@ -305,29 +307,28 @@
|
||||
" scaler = StandardScaler()\n",
|
||||
" X_train_scaled = scaler.fit_transform(X_train)\n",
|
||||
" X_val_scaled = scaler.transform(X_val)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" model = build_model()\n",
|
||||
"\n",
|
||||
" model.compile(\n",
|
||||
" optimizer='adam',\n",
|
||||
" loss='binary_crossentropy',\n",
|
||||
" metrics=[\"f1_score\"]\n",
|
||||
" )\n",
|
||||
" model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"f1_score\"])\n",
|
||||
"\n",
|
||||
" # EarlyStopping\n",
|
||||
" callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\n",
|
||||
" callback = tf.keras.callbacks.EarlyStopping(\n",
|
||||
" monitor=\"val_loss\", patience=10, restore_best_weights=True\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Entraînement\n",
|
||||
" history = model.fit(\n",
|
||||
" X_train_scaled, y_train,\n",
|
||||
" X_train_scaled,\n",
|
||||
" y_train,\n",
|
||||
" epochs=50,\n",
|
||||
" batch_size=8,\n",
|
||||
" validation_data=(X_val_scaled, y_val),\n",
|
||||
" callbacks=[callback],\n",
|
||||
" verbose=0,\n",
|
||||
" class_weight={0: 1.0, 1: 2.0}\n",
|
||||
" class_weight={0: 1.0, 1: 2.0},\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" histories.append(history.history)\n",
|
||||
"\n",
|
||||
" # Prédiction & F1\n",
|
||||
@@ -360,9 +361,9 @@
|
||||
"axes = axes.flatten() # Flatten to easily iterate\n",
|
||||
"\n",
|
||||
"for i, (hist, ax) in enumerate(zip(histories, axes)):\n",
|
||||
" ax.plot(hist['loss'], label='Train loss', alpha=0.6)\n",
|
||||
" ax.plot(hist['val_loss'], label='Val loss', linestyle='--', alpha=0.6)\n",
|
||||
" ax.set_title(f\"Fold {i+1}\")\n",
|
||||
" ax.plot(hist[\"loss\"], label=\"Train loss\", alpha=0.6)\n",
|
||||
" ax.plot(hist[\"val_loss\"], label=\"Val loss\", linestyle=\"--\", alpha=0.6)\n",
|
||||
" ax.set_title(f\"Fold {i + 1}\")\n",
|
||||
" ax.set_xlabel(\"Epochs\")\n",
|
||||
" if i % 2 == 0:\n",
|
||||
" ax.set_ylabel(\"Binary Crossentropy\")\n",
|
||||
@@ -436,7 +437,9 @@
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.2, random_state=42, stratify=y\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
"X_train_scaled = scaler.fit_transform(X_train)\n",
|
||||
@@ -444,21 +447,21 @@
|
||||
"\n",
|
||||
"model = build_model()\n",
|
||||
"\n",
|
||||
"model.compile(\n",
|
||||
" optimizer='adam',\n",
|
||||
" loss='binary_crossentropy'\n",
|
||||
"model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\")\n",
|
||||
"\n",
|
||||
"callback = tf.keras.callbacks.EarlyStopping(\n",
|
||||
" monitor=\"val_loss\", patience=10, restore_best_weights=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\n",
|
||||
"\n",
|
||||
"history = model.fit(\n",
|
||||
" X_train_scaled, y_train,\n",
|
||||
" X_train_scaled,\n",
|
||||
" y_train,\n",
|
||||
" epochs=50,\n",
|
||||
" batch_size=8,\n",
|
||||
" validation_split=0.2,\n",
|
||||
" callbacks=[callback],\n",
|
||||
" verbose=0,\n",
|
||||
" class_weight={0: 1.0, 1: 2.0}\n",
|
||||
" class_weight={0: 1.0, 1: 2.0},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -486,11 +489,11 @@
|
||||
],
|
||||
"source": [
|
||||
"plt.figure(figsize=(8, 5))\n",
|
||||
"plt.plot(history.history['loss'], label='Loss (train)')\n",
|
||||
"plt.plot(history.history['val_loss'], label='Loss (val)', linestyle='--')\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('Binary Cross-Entropy Loss')\n",
|
||||
"plt.title('Courbe d\\'apprentissage')\n",
|
||||
"plt.plot(history.history[\"loss\"], label=\"Loss (train)\")\n",
|
||||
"plt.plot(history.history[\"val_loss\"], label=\"Loss (val)\", linestyle=\"--\")\n",
|
||||
"plt.xlabel(\"Epochs\")\n",
|
||||
"plt.ylabel(\"Binary Cross-Entropy Loss\")\n",
|
||||
"plt.title(\"Courbe d'apprentissage\")\n",
|
||||
"plt.legend()\n",
|
||||
"plt.grid(True)\n",
|
||||
"plt.tight_layout()\n",
|
||||
|
||||
Reference in New Issue
Block a user