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:
2025-07-01 20:46:08 +02:00
parent e273cf90f7
commit f94ff07cab
34 changed files with 5713 additions and 5047 deletions

View File

@@ -46,23 +46,23 @@
"\n",
"\n",
"np.random.seed(12)\n",
"num_observations=400\n",
"num_observations = 400\n",
"\n",
"center1=[0,0]\n",
"center2=[1,4]\n",
"center3=[-3,2]\n",
"center1 = [0, 0]\n",
"center2 = [1, 4]\n",
"center3 = [-3, 2]\n",
"\n",
"x1=np.random.multivariate_normal(center1,[[1,0],[0,1]], num_observations)\n",
"x2=np.random.multivariate_normal(center2,[[1,0],[0,1]], num_observations)\n",
"x3=np.random.multivariate_normal(center3,[[1,0],[0,1]], num_observations)\n",
"x1 = np.random.multivariate_normal(center1, [[1, 0], [0, 1]], num_observations)\n",
"x2 = np.random.multivariate_normal(center2, [[1, 0], [0, 1]], num_observations)\n",
"x3 = np.random.multivariate_normal(center3, [[1, 0], [0, 1]], num_observations)\n",
"\n",
"X= np.vstack((x1, x2, x3)).astype(np.float32)\n",
"X = np.vstack((x1, x2, x3)).astype(np.float32)\n",
"\n",
"plt.figure(figsize=(8,6))\n",
"plt.plot(X[:,0], X[:,1],\".b\",alpha=0.2)\n",
"plt.plot(center1[0], center1[1], '.', color='red', markersize=10)\n",
"plt.plot(center2[0], center2[1], '.', color='red', markersize=10)\n",
"plt.plot(center3[0], center3[1], '.', color='red', markersize=10)\n",
"plt.figure(figsize=(8, 6))\n",
"plt.plot(X[:, 0], X[:, 1], \".b\", alpha=0.2)\n",
"plt.plot(center1[0], center1[1], \".\", color=\"red\", markersize=10)\n",
"plt.plot(center2[0], center2[1], \".\", color=\"red\", markersize=10)\n",
"plt.plot(center3[0], center3[1], \".\", color=\"red\", markersize=10)\n",
"plt.show()"
]
},
@@ -540,10 +540,12 @@
}
],
"source": [
"plt.figure(figsize=(8,6))\n",
"plt.plot(X[:,0], X[:,1],\".b\",alpha=0.2)\n",
"plt.figure(figsize=(8, 6))\n",
"plt.plot(X[:, 0], X[:, 1], \".b\", alpha=0.2)\n",
"for center in kmeans1.cluster_centers_:\n",
" plt.plot(center[0], center[1], '.', color='red', markersize=10, label='Cluster center')\n",
" plt.plot(\n",
" center[0], center[1], \".\", color=\"red\", markersize=10, label=\"Cluster center\"\n",
" )\n",
"plt.legend()\n",
"plt.show()"
]
@@ -585,11 +587,11 @@
"# Hint: An example for plotting the Voronoi partition\n",
"from scipy.spatial import Voronoi, voronoi_plot_2d\n",
"\n",
"points_generer_voronoi = np.array([[0,0],[1,4],[-3,2]])\n",
"points_generer_voronoi = np.array([[0, 0], [1, 4], [-3, 2]])\n",
"\n",
"vor = Voronoi(points_generer_voronoi)\n",
"\n",
"fig, ax = plt.subplots(1,1,figsize=(4,4)) \n",
"fig, ax = plt.subplots(1, 1, figsize=(4, 4))\n",
"\n",
"fig = voronoi_plot_2d(vor, ax=ax, show_vertices=False)"
]
@@ -614,14 +616,16 @@
"# Answer for Exercise 3\n",
"\n",
"\n",
"fig, ax = plt.subplots(1,1,figsize=(8,6)) \n",
"plt.plot(X[:,0], X[:,1], \".b\", alpha=0.2)\n",
"fig, ax = plt.subplots(1, 1, figsize=(8, 6))\n",
"plt.plot(X[:, 0], X[:, 1], \".b\", alpha=0.2)\n",
"\n",
"vor = Voronoi(kmeans1.cluster_centers_)\n",
"fig = voronoi_plot_2d(vor, ax=ax, show_vertices=False)\n",
"\n",
"for center in kmeans1.cluster_centers_:\n",
" plt.plot(center[0], center[1], '.', color='red', markersize=10, label='Cluster center')\n",
" plt.plot(\n",
" center[0], center[1], \".\", color=\"red\", markersize=10, label=\"Cluster center\"\n",
" )\n",
"plt.legend()\n",
"plt.show()"
]
@@ -1233,10 +1237,10 @@
}
],
"source": [
"print (\"1:\", compress_model.labels_)\n",
"print (\"2:\", compress_model.labels_.shape)\n",
"print (\"3:\", compress_model.cluster_centers_)\n",
"print (\"4:\", compress_model.cluster_centers_.shape)"
"print(\"1:\", compress_model.labels_)\n",
"print(\"2:\", compress_model.labels_.shape)\n",
"print(\"3:\", compress_model.cluster_centers_)\n",
"print(\"4:\", compress_model.cluster_centers_.shape)"
]
},
{
@@ -1275,13 +1279,13 @@
"metadata": {},
"outputs": [],
"source": [
"color_new=np.zeros_like(colors)\n",
"color_new = np.zeros_like(colors)\n",
"\n",
"labels=compress_model.labels_\n",
"centers=compress_model.cluster_centers_\n",
"labels = compress_model.labels_\n",
"centers = compress_model.cluster_centers_\n",
"\n",
"for i in range(len(colors)):\n",
" color_new[i]= centers[labels[i]]"
" color_new[i] = centers[labels[i]]"
]
},
{
@@ -1336,11 +1340,12 @@
],
"source": [
"import matplotlib.image as mpimg\n",
"\n",
"mpimg.imsave(\"assets/zelda_new.png\", zelda_new)\n",
"\n",
"plt.figure(figsize=(8, 6))\n",
"plt.imshow(zelda_new)\n",
"plt.show()\n"
"plt.show()"
]
},
{
@@ -1363,13 +1368,13 @@
"source": [
"import os\n",
"\n",
"size_new=os.path.getsize('assets/zelda_new.png')\n",
"size_old=os.path.getsize('assets/zelda.png')\n",
"size_new = os.path.getsize(\"assets/zelda_new.png\")\n",
"size_old = os.path.getsize(\"assets/zelda.png\")\n",
"\n",
"print (\"The original size is \", size_old, \"bytes.\")\n",
"print (\"The compressed size is \", size_new, \"bytes.\")\n",
"print(\"The original size is \", size_old, \"bytes.\")\n",
"print(\"The compressed size is \", size_new, \"bytes.\")\n",
"\n",
"print (f\"The compression factor is {size_old/size_new : .3f}\")"
"print(f\"The compression factor is {size_old / size_new: .3f}\")"
]
},
{
@@ -1407,8 +1412,8 @@
}
],
"source": [
"partiel=plt.imread(\"assets/partiel.png\")\n",
"plt.figure(figsize = (20,10))\n",
"partiel = plt.imread(\"assets/partiel.png\")\n",
"plt.figure(figsize=(20, 10))\n",
"plt.imshow(partiel)"
]
},
@@ -1426,7 +1431,7 @@
}
],
"source": [
"print (partiel.shape)"
"print(partiel.shape)"
]
},
{
@@ -1472,23 +1477,23 @@
}
],
"source": [
"partiel_new=np.zeros_like(partiel)\n",
"partiel_new = np.zeros_like(partiel)\n",
"\n",
"noir_rgb=np.array([0,0,0])\n",
"blanc_rgb=np.array([1,1,1])\n",
"noir_rgb = np.array([0, 0, 0])\n",
"blanc_rgb = np.array([1, 1, 1])\n",
"\n",
"epsilon = 0.5 # threshold\n",
"\n",
"epsilon=0.5 # threshold\n",
" \n",
"distances = np.linalg.norm(partiel - noir_rgb, axis=2)\n",
"partiel_new = np.zeros_like(partiel)\n",
"partiel_new[distances <= epsilon] = noir_rgb\n",
"partiel_new[distances > epsilon] = blanc_rgb\n",
" \n",
"\n",
"mpimg.imsave(\"assets/partiel_new.png\", partiel_new)\n",
"\n",
"plt.figure(figsize=(20,10))\n",
"plt.figure(figsize=(20, 10))\n",
"plt.imshow(partiel_new)\n",
"plt.show()\n"
"plt.show()"
]
},
{
@@ -1531,16 +1536,20 @@
"mnist = tf.keras.datasets.mnist\n",
"(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
"\n",
"X_train = X_train.reshape(-1, 28*28)\n",
"X_train = X_train.reshape(-1, 28 * 28)\n",
"\n",
"kmeans2 = KMeans(n_clusters=10)\n",
"clusters = kmeans2.fit_predict(X_train)\n",
"\n",
"\n",
"def map_clusters_to_labels(clusters, true_labels):\n",
" return np.array([mode(true_labels[clusters == i], keepdims=True).mode[0] for i in range(10)])\n",
" return np.array(\n",
" [mode(true_labels[clusters == i], keepdims=True).mode[0] for i in range(10)]\n",
" )\n",
"\n",
"\n",
"cluster_to_label = map_clusters_to_labels(clusters, y_train)\n",
"print(\"Cluster to label mapping:\", cluster_to_label)\n"
"print(\"Cluster to label mapping:\", cluster_to_label)"
]
},
{