diff --git a/M1/Statistical Learning/TP2_KNN.ipynb b/M1/Statistical Learning/TP2_KNN.ipynb index aea18fc..946715b 100644 --- a/M1/Statistical Learning/TP2_KNN.ipynb +++ b/M1/Statistical Learning/TP2_KNN.ipynb @@ -62,8 +62,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:35:48.092357Z", - "start_time": "2025-02-05T09:35:48.052472Z" + "end_time": "2025-02-06T12:09:35.663428Z", + "start_time": "2025-02-06T12:09:35.519995Z" } }, "source": "import numpy as np", @@ -73,8 +73,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T10:14:46.744770Z", - "start_time": "2025-02-05T10:14:46.738364Z" + "end_time": "2025-02-06T12:09:37.572928Z", + "start_time": "2025-02-06T12:09:37.564513Z" } }, "cell_type": "code", @@ -117,7 +117,7 @@ ] } ], - "execution_count": 122 + "execution_count": 2 }, { "metadata": {}, @@ -167,8 +167,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T10:14:48.085649Z", - "start_time": "2025-02-05T10:14:48.082460Z" + "end_time": "2025-02-06T12:09:42.269248Z", + "start_time": "2025-02-06T12:09:38.868809Z" } }, "source": [ @@ -179,7 +179,7 @@ "y = iris.target" ], "outputs": [], - "execution_count": 123 + "execution_count": 3 }, { "cell_type": "markdown", @@ -192,8 +192,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:08.647762Z", - "start_time": "2025-02-05T11:26:08.645423Z" + "end_time": "2025-02-06T12:09:42.277413Z", + "start_time": "2025-02-06T12:09:42.274981Z" } }, "source": [ @@ -211,7 +211,7 @@ ] } ], - "execution_count": 161 + "execution_count": 4 }, { "cell_type": "markdown", @@ -224,8 +224,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:09.360612Z", - "start_time": "2025-02-05T11:26:09.357969Z" + "end_time": "2025-02-06T12:09:42.553805Z", + "start_time": "2025-02-06T12:09:42.292116Z" } }, "source": [ @@ -234,7 +234,7 @@ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)" ], "outputs": [], - "execution_count": 162 + "execution_count": 5 }, { "cell_type": "markdown", @@ -249,8 +249,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:10.135924Z", - "start_time": "2025-02-05T11:26:10.133376Z" + "end_time": "2025-02-06T12:09:42.568515Z", + "start_time": "2025-02-06T12:09:42.564885Z" } }, "source": [ @@ -272,7 +272,7 @@ ] } ], - "execution_count": 163 + "execution_count": 6 }, { "cell_type": "markdown", @@ -294,8 +294,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:10.847708Z", - "start_time": "2025-02-05T11:26:10.845674Z" + "end_time": "2025-02-06T12:09:42.583044Z", + "start_time": "2025-02-06T12:09:42.580874Z" } }, "source": [ @@ -303,7 +303,7 @@ " return np.linalg.norm(sample1 - sample2, axis=1) ** 2" ], "outputs": [], - "execution_count": 164 + "execution_count": 7 }, { "cell_type": "markdown", @@ -329,8 +329,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:11.319414Z", - "start_time": "2025-02-05T11:26:11.315241Z" + "end_time": "2025-02-06T12:09:42.607360Z", + "start_time": "2025-02-06T12:09:42.604540Z" } }, "source": [ @@ -365,14 +365,14 @@ ] } ], - "execution_count": 165 + "execution_count": 8 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:11.549555Z", - "start_time": "2025-02-05T11:26:11.546889Z" + "end_time": "2025-02-06T12:09:43.571841Z", + "start_time": "2025-02-06T12:09:43.569070Z" } }, "source": [ @@ -385,7 +385,7 @@ " return Counter(y_train[nearest_neighbors]).most_common(1)[0][0]" ], "outputs": [], - "execution_count": 166 + "execution_count": 9 }, { "cell_type": "markdown", @@ -415,8 +415,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:12.511511Z", - "start_time": "2025-02-05T11:26:12.508445Z" + "end_time": "2025-02-06T12:09:49.298170Z", + "start_time": "2025-02-06T12:09:49.292521Z" } }, "source": [ @@ -431,19 +431,19 @@ " [3, 4]])" ] }, - "execution_count": 167, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 167 + "execution_count": 10 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:12.784146Z", - "start_time": "2025-02-05T11:26:12.780845Z" + "end_time": "2025-02-06T12:09:49.680539Z", + "start_time": "2025-02-06T12:09:49.676064Z" } }, "source": [ @@ -456,19 +456,19 @@ "np.int64(10)" ] }, - "execution_count": 168, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 168 + "execution_count": 11 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:13.022841Z", - "start_time": "2025-02-05T11:26:13.020426Z" + "end_time": "2025-02-06T12:09:49.895044Z", + "start_time": "2025-02-06T12:09:49.889807Z" } }, "source": [ @@ -481,19 +481,19 @@ "array([3, 7])" ] }, - "execution_count": 169, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 169 + "execution_count": 12 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:13.231874Z", - "start_time": "2025-02-05T11:26:13.229410Z" + "end_time": "2025-02-06T12:09:50.047326Z", + "start_time": "2025-02-06T12:09:50.045038Z" } }, "source": [ @@ -506,12 +506,12 @@ "array([4, 6])" ] }, - "execution_count": 170, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 170 + "execution_count": 13 }, { "cell_type": "markdown", @@ -525,8 +525,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:13.632871Z", - "start_time": "2025-02-05T11:26:13.629716Z" + "end_time": "2025-02-06T12:09:51.499493Z", + "start_time": "2025-02-06T12:09:51.496079Z" } }, "source": [ @@ -544,19 +544,19 @@ " [6, 7]]])" ] }, - "execution_count": 171, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 171 + "execution_count": 14 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:13.817580Z", - "start_time": "2025-02-05T11:26:13.814914Z" + "end_time": "2025-02-06T12:09:51.964030Z", + "start_time": "2025-02-06T12:09:51.959665Z" } }, "source": "b.sum(axis=0), b.sum(axis=1), b.sum(axis=2), b.sum()", @@ -573,12 +573,12 @@ " np.int64(28))" ] }, - "execution_count": 172, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 172 + "execution_count": 15 }, { "cell_type": "markdown", @@ -601,17 +601,16 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:14.342154Z", - "start_time": "2025-02-05T11:26:14.340114Z" + "end_time": "2025-02-06T12:09:54.092285Z", + "start_time": "2025-02-06T12:09:54.089316Z" } }, "source": [ - "#Answer for Optional question 1\n", "def euc_dis_mat(sample1, sample2):\n", " return np.linalg.norm(sample1 - sample2, axis=1) ** 2" ], "outputs": [], - "execution_count": 173 + "execution_count": 16 }, { "cell_type": "markdown", @@ -636,20 +635,30 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:14.657801Z", - "start_time": "2025-02-05T11:26:14.655785Z" + "end_time": "2025-02-06T12:10:31.335918Z", + "start_time": "2025-02-06T12:10:31.332196Z" } }, "source": [ - "# Answer for Optional question 2\n", "def knn_class_2(X_train, y_train, x_new, K):\n", " X_new = np.tile(x_new, (len(X_train), 1))\n", " distances = euc_dis_mat(X_train, X_new)\n", " k_neighbors = np.argsort(distances)[:K]\n", " return Counter(y_train[k_neighbors]).most_common()[0][0]" ], - "outputs": [], - "execution_count": 174 + "outputs": [ + { + "data": { + "text/plain": [ + "np.int64(1)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 18 }, { "cell_type": "markdown",