From 36ad88bca92ae2592ad536101b271665e43f6f9a Mon Sep 17 00:00:00 2001 From: "gabriel.marinoja" Date: Tue, 23 Sep 2025 14:30:36 +0200 Subject: [PATCH] Initial commit --- PW-2/ex1-numpy/numpy-tutorial-stud.ipynb | 10 +- .../to-loan-or-not-to-loan-stud.ipynb | 118 ++++++++++++------ 2 files changed, 85 insertions(+), 43 deletions(-) diff --git a/PW-2/ex1-numpy/numpy-tutorial-stud.ipynb b/PW-2/ex1-numpy/numpy-tutorial-stud.ipynb index 87b26d8..c28bbe2 100644 --- a/PW-2/ex1-numpy/numpy-tutorial-stud.ipynb +++ b/PW-2/ex1-numpy/numpy-tutorial-stud.ipynb @@ -931,7 +931,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -954,19 +954,21 @@ "\n", "plt.figure(figsize=(15, 5))\n", "\n", + "# RGB format : img[red, green, blue] -> img[:,:,0], img[:,:,1], img[:,:,2]\n", + "\n", "# Red channel\n", "plt.subplot(1, 3, 1)\n", - "plt.imshow(img[:, :, 0], cmap='Reds')\n", + "plt.imshow(img[:,:,0], cmap='Reds')\n", "plt.title('Red Channel')\n", "\n", "# Green channel\n", "plt.subplot(1, 3, 2)\n", - "plt.imshow(img[:, :, 1], cmap='Greens')\n", + "plt.imshow(img[:,:,1], cmap='Greens')\n", "plt.title('Green Channel')\n", "\n", "# Blue channel\n", "plt.subplot(1, 3, 3)\n", - "plt.imshow(img[:, :, 2], cmap='Blues')\n", + "plt.imshow(img[:,:,2], cmap='Blues')\n", "plt.title('Blue Channel')\n", "\n", "plt.show()\n", diff --git a/PW-2/ex2-to-loan-or-not-to-loan/to-loan-or-not-to-loan-stud.ipynb b/PW-2/ex2-to-loan-or-not-to-loan/to-loan-or-not-to-loan-stud.ipynb index 7e4ffe3..453c35b 100644 --- a/PW-2/ex2-to-loan-or-not-to-loan/to-loan-or-not-to-loan-stud.ipynb +++ b/PW-2/ex2-to-loan-or-not-to-loan/to-loan-or-not-to-loan-stud.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "26758936", "metadata": {}, "outputs": [], @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "id": "a23f62b5", "metadata": {}, "outputs": [], @@ -81,7 +81,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "id": "f4bec500", "metadata": {}, "outputs": [ @@ -186,7 +186,7 @@ "4 Y " ] }, - "execution_count": 3, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -228,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "id": "2c56efa5", "metadata": {}, "outputs": [], @@ -246,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "id": "dc5f9cda", "metadata": {}, "outputs": [], @@ -264,7 +264,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "id": "83beacfb", "metadata": {}, "outputs": [], @@ -283,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "id": "9c567bb7", "metadata": {}, "outputs": [], @@ -301,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 12, "id": "c0db7c1f", "metadata": {}, "outputs": [], @@ -319,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 13, "id": "b05be2cc", "metadata": {}, "outputs": [], @@ -345,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "30919672", "metadata": {}, "outputs": [], @@ -356,36 +356,20 @@ " \"\"\"\n", " Initialize the class.\n", " \"\"\"\n", - " pass\n", + " self.classes_ = None\n", " \n", " def fit(self, X, y):\n", " \"\"\"\n", " Fit the dummy classifier.\n", - " \n", - " Parameters\n", - " ----------\n", - " X : Numpy array or Pandas DataFrame of shape (n_samples, n_features)\n", - " Training data.\n", - " y : Numpy array or Pandas DataFrame of shape (n_samples,)\n", - " Target values.\n", " \"\"\"\n", - " pass\n", + " self.classes_ = np.unique(y)\n", " \n", " def predict(self, X):\n", " \"\"\"\n", " Predict the class labels for the provided data.\n", - "\n", - " Parameters\n", - " ----------\n", - " X : Numpy array or Pandas DataFrame of shape (n_queries, n_features)\n", - " Test samples.\n", - "\n", - " Returns\n", - " -------\n", - " y : Numpy array or Pandas DataFrame of shape (n_queries,)\n", - " Class labels for each data sample.\n", " \"\"\"\n", - " pass" + " n_samples = len(X)\n", + " return np.random.choice(self.classes_, size=n_samples)\n" ] }, { @@ -398,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 15, "id": "184f3905", "metadata": {}, "outputs": [], @@ -441,7 +425,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 16, "id": "759e924e", "metadata": {}, "outputs": [], @@ -457,7 +441,10 @@ " n_neighbors : int, default=3\n", " Number of neighbors to use by default.\n", " \"\"\"\n", - " pass\n", + "\n", + " self.n_neighbors = n_neighbors\n", + " self.X_train = None\n", + " self.y_train = None\n", " \n", " def fit(self, X, y):\n", " \"\"\"\n", @@ -470,7 +457,9 @@ " y : Numpy array or Pandas DataFrame of shape (n_samples,)\n", " Target values.\n", " \"\"\"\n", - " pass\n", + "\n", + " self.X_train = np.array(X)\n", + " self.y_train = np.array(y)\n", " \n", " @staticmethod\n", " def _euclidian_distance(a, b):\n", @@ -484,7 +473,23 @@ " b : Numpy array or Pandas DataFrame\n", " Second operand.\n", " \"\"\"\n", - " pass\n", + "\n", + " return np.sqrt(np.sum((a - b) ** 2, axis=1))\n", + " \n", + " @staticmethod\n", + " def _manhattan_distance(a, b):\n", + " \"\"\"\n", + " Utility function to compute the Manhattan distance.\n", + " \n", + " Parameters\n", + " ----------\n", + " a : Numpy array or Pandas DataFrame\n", + " First operand.\n", + " b : Numpy array or Pandas DataFrame\n", + " Second operand.\n", + " \"\"\"\n", + " \n", + " return np.sum(np.abs(a - b), axis=1)\n", " \n", " def predict(self, X):\n", " \"\"\"\n", @@ -500,7 +505,16 @@ " y : Numpy array or Pandas DataFrame of shape (n_queries,)\n", " Class labels for each data sample.\n", " \"\"\"\n", - " pass" + " X = np.array(X)\n", + " predictions = []\n", + " for x in X:\n", + " distances = self._euclidian_distance(self.X_train, x)\n", + " neighbor_idxs = np.argsort(distances)[:self.n_neighbors]\n", + " neighbor_labels = self.y_train[neighbor_idxs]\n", + " \n", + " counts = np.bincount(neighbor_labels.astype(int))\n", + " predictions.append(np.argmax(counts))\n", + " return np.array(predictions)" ] }, { @@ -511,6 +525,32 @@ "Compute the performance of the system as a function of $k = 1...7$." ] }, + { + "cell_type": "code", + "execution_count": 18, + "id": "4214d221", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "KNN accuracies for k=1 to 7: [None, None, None, None, None, None, None]\n" + ] + } + ], + "source": [ + "knn_accuracies = []\n", + "for k in range(1, 8):\n", + " knn = KNNClassifier(n_neighbors=k)\n", + " knn.fit(X_train, y_train)\n", + " y_pred = knn.predict(X_test)\n", + " acc = accuracy_score(y_test, y_pred)\n", + " knn_accuracies.append(acc)\n", + "\n", + "print(\"KNN accuracies for k=1 to 7:\", knn_accuracies)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -578,7 +618,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "MLvenv", "language": "python", "name": "python3" }, @@ -592,7 +632,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.11" + "version": "3.10.18" } }, "nbformat": 4,