diff --git a/M1/Statistical Learning/TP2_KNN.ipynb b/M1/Statistical Learning/TP2_KNN.ipynb index 946715b..bfab100 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-06T12:09:35.663428Z", - "start_time": "2025-02-06T12:09:35.519995Z" + "end_time": "2025-02-07T16:32:17.846897Z", + "start_time": "2025-02-07T16:32:17.807782Z" } }, "source": "import numpy as np", @@ -73,8 +73,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:37.572928Z", - "start_time": "2025-02-06T12:09:37.564513Z" + "end_time": "2025-02-07T16:32:17.854652Z", + "start_time": "2025-02-07T16:32:17.851755Z" } }, "cell_type": "code", @@ -167,8 +167,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:42.269248Z", - "start_time": "2025-02-06T12:09:38.868809Z" + "end_time": "2025-02-07T16:32:18.557770Z", + "start_time": "2025-02-07T16:32:17.940064Z" } }, "source": [ @@ -192,8 +192,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:42.277413Z", - "start_time": "2025-02-06T12:09:42.274981Z" + "end_time": "2025-02-07T16:32:18.569068Z", + "start_time": "2025-02-07T16:32:18.566479Z" } }, "source": [ @@ -224,8 +224,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:42.553805Z", - "start_time": "2025-02-06T12:09:42.292116Z" + "end_time": "2025-02-07T16:32:18.644913Z", + "start_time": "2025-02-07T16:32:18.609906Z" } }, "source": [ @@ -249,8 +249,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:42.568515Z", - "start_time": "2025-02-06T12:09:42.564885Z" + "end_time": "2025-02-07T16:32:18.656592Z", + "start_time": "2025-02-07T16:32:18.654009Z" } }, "source": [ @@ -294,8 +294,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:42.583044Z", - "start_time": "2025-02-06T12:09:42.580874Z" + "end_time": "2025-02-07T16:32:18.668040Z", + "start_time": "2025-02-07T16:32:18.666273Z" } }, "source": [ @@ -329,8 +329,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:42.607360Z", - "start_time": "2025-02-06T12:09:42.604540Z" + "end_time": "2025-02-07T16:32:18.698079Z", + "start_time": "2025-02-07T16:32:18.695117Z" } }, "source": [ @@ -371,8 +371,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:43.571841Z", - "start_time": "2025-02-06T12:09:43.569070Z" + "end_time": "2025-02-07T16:32:18.727024Z", + "start_time": "2025-02-07T16:32:18.725108Z" } }, "source": [ @@ -415,8 +415,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:49.298170Z", - "start_time": "2025-02-06T12:09:49.292521Z" + "end_time": "2025-02-07T16:32:18.747162Z", + "start_time": "2025-02-07T16:32:18.743867Z" } }, "source": [ @@ -442,8 +442,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:49.680539Z", - "start_time": "2025-02-06T12:09:49.676064Z" + "end_time": "2025-02-07T16:32:18.803162Z", + "start_time": "2025-02-07T16:32:18.798675Z" } }, "source": [ @@ -467,8 +467,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:49.895044Z", - "start_time": "2025-02-06T12:09:49.889807Z" + "end_time": "2025-02-07T16:32:18.862775Z", + "start_time": "2025-02-07T16:32:18.859640Z" } }, "source": [ @@ -492,8 +492,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:50.047326Z", - "start_time": "2025-02-06T12:09:50.045038Z" + "end_time": "2025-02-07T16:32:18.936458Z", + "start_time": "2025-02-07T16:32:18.932745Z" } }, "source": [ @@ -525,8 +525,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:51.499493Z", - "start_time": "2025-02-06T12:09:51.496079Z" + "end_time": "2025-02-07T16:32:18.973366Z", + "start_time": "2025-02-07T16:32:18.970454Z" } }, "source": [ @@ -555,8 +555,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:51.964030Z", - "start_time": "2025-02-06T12:09:51.959665Z" + "end_time": "2025-02-07T16:32:19.006689Z", + "start_time": "2025-02-07T16:32:19.003910Z" } }, "source": "b.sum(axis=0), b.sum(axis=1), b.sum(axis=2), b.sum()", @@ -601,8 +601,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:09:54.092285Z", - "start_time": "2025-02-06T12:09:54.089316Z" + "end_time": "2025-02-07T16:32:19.039310Z", + "start_time": "2025-02-07T16:32:19.037538Z" } }, "source": [ @@ -635,8 +635,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-06T12:10:31.335918Z", - "start_time": "2025-02-06T12:10:31.332196Z" + "end_time": "2025-02-07T16:32:19.076151Z", + "start_time": "2025-02-07T16:32:19.073174Z" } }, "source": [ @@ -646,19 +646,8 @@ " k_neighbors = np.argsort(distances)[:K]\n", " return Counter(y_train[k_neighbors]).most_common()[0][0]" ], - "outputs": [ - { - "data": { - "text/plain": [ - "np.int64(1)" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "execution_count": 18 + "outputs": [], + "execution_count": 17 }, { "cell_type": "markdown", @@ -678,8 +667,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:15.309422Z", - "start_time": "2025-02-05T11:26:15.304219Z" + "end_time": "2025-02-07T16:32:19.114434Z", + "start_time": "2025-02-07T16:32:19.109526Z" } }, "source": [ @@ -699,7 +688,7 @@ ] } ], - "execution_count": 175 + "execution_count": 18 }, { "cell_type": "markdown", @@ -719,8 +708,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:15.657315Z", - "start_time": "2025-02-05T11:26:15.655386Z" + "end_time": "2025-02-07T16:32:19.215397Z", + "start_time": "2025-02-07T16:32:19.148514Z" } }, "source": [ @@ -729,7 +718,7 @@ "knn_classifier_2 = KNeighborsClassifier(n_neighbors=3)" ], "outputs": [], - "execution_count": 176 + "execution_count": 19 }, { "cell_type": "markdown", @@ -742,8 +731,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:16.224590Z", - "start_time": "2025-02-05T11:26:16.219083Z" + "end_time": "2025-02-07T16:32:19.233667Z", + "start_time": "2025-02-07T16:32:19.227889Z" } }, "source": "knn_classifier_2.fit(X_train, y_train)", @@ -754,7 +743,7 @@ "KNeighborsClassifier(n_neighbors=3)" ], "text/html": [ - "
KNeighborsClassifier(n_neighbors=3)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + "
KNeighborsClassifier(n_neighbors=3)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ] }, - "execution_count": 177, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 177 + "execution_count": 20 }, { "cell_type": "markdown", @@ -1190,8 +1179,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:17.023370Z", - "start_time": "2025-02-05T11:26:17.018037Z" + "end_time": "2025-02-07T16:32:19.260546Z", + "start_time": "2025-02-07T16:32:19.254828Z" } }, "source": "knn_classifier_2.score(X_test, y_test)", @@ -1202,12 +1191,12 @@ "0.98" ] }, - "execution_count": 178, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 178 + "execution_count": 21 }, { "cell_type": "markdown", @@ -1255,8 +1244,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:19.505745Z", - "start_time": "2025-02-05T11:26:19.502276Z" + "end_time": "2025-02-07T16:32:19.559521Z", + "start_time": "2025-02-07T16:32:19.282596Z" } }, "source": [ @@ -1266,14 +1255,14 @@ "from sklearn.neighbors import KNeighborsClassifier" ], "outputs": [], - "execution_count": 179 + "execution_count": 22 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:19.890538Z", - "start_time": "2025-02-05T11:26:19.887279Z" + "end_time": "2025-02-07T16:32:19.575414Z", + "start_time": "2025-02-07T16:32:19.570441Z" } }, "source": [ @@ -1297,14 +1286,14 @@ ] } ], - "execution_count": 180 + "execution_count": 23 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:20.564996Z", - "start_time": "2025-02-05T11:26:20.484918Z" + "end_time": "2025-02-07T16:32:19.693282Z", + "start_time": "2025-02-07T16:32:19.599201Z" } }, "source": [ @@ -1315,10 +1304,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 181, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" }, @@ -1333,7 +1322,7 @@ "output_type": "display_data" } ], - "execution_count": 181 + "execution_count": 24 }, { "cell_type": "markdown", @@ -1346,8 +1335,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:27:09.124075Z", - "start_time": "2025-02-05T11:27:09.119577Z" + "end_time": "2025-02-07T16:32:19.710747Z", + "start_time": "2025-02-07T16:32:19.706881Z" } }, "source": [ @@ -1361,7 +1350,7 @@ " KNNs.append(knn_classifier_k)" ], "outputs": [], - "execution_count": 188 + "execution_count": 25 }, { "cell_type": "markdown", @@ -1374,8 +1363,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T11:26:42.695499Z", - "start_time": "2025-02-05T11:26:41.672308Z" + "end_time": "2025-02-07T16:32:20.615872Z", + "start_time": "2025-02-07T16:32:19.741060Z" } }, "source": [ @@ -1396,17 +1385,6 @@ "plt.show()\n" ], "outputs": [ - { - "ename": "IndexError", - "evalue": "index 2 is out of bounds for axis 0 with size 2", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mIndexError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[187], line 11\u001B[0m\n\u001B[1;32m 8\u001B[0m Z \u001B[38;5;241m=\u001B[39m clf\u001B[38;5;241m.\u001B[39mpredict(np\u001B[38;5;241m.\u001B[39mc_[xx\u001B[38;5;241m.\u001B[39mravel(), yy\u001B[38;5;241m.\u001B[39mravel()])\n\u001B[1;32m 9\u001B[0m Z \u001B[38;5;241m=\u001B[39m Z\u001B[38;5;241m.\u001B[39mreshape(xx\u001B[38;5;241m.\u001B[39mshape)\n\u001B[0;32m---> 11\u001B[0m \u001B[43maxarr\u001B[49m\u001B[43m[\u001B[49m\u001B[43midx\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43midx\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m]\u001B[49m\u001B[38;5;241m.\u001B[39mcontourf(xx, yy, Z, alpha\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m0.4\u001B[39m)\n\u001B[1;32m 12\u001B[0m axarr[idx[\u001B[38;5;241m0\u001B[39m], idx[\u001B[38;5;241m1\u001B[39m]]\u001B[38;5;241m.\u001B[39mscatter(X2[:, \u001B[38;5;241m0\u001B[39m], X2[:, \u001B[38;5;241m1\u001B[39m], c\u001B[38;5;241m=\u001B[39mY2, s\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m20\u001B[39m, edgecolor\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mk\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m 13\u001B[0m axarr[idx[\u001B[38;5;241m0\u001B[39m], idx[\u001B[38;5;241m1\u001B[39m]]\u001B[38;5;241m.\u001B[39mset_title(tt)\n", - "\u001B[0;31mIndexError\u001B[0m: index 2 is out of bounds for axis 0 with size 2" - ] - }, { "data": { "text/plain": [ @@ -1418,7 +1396,7 @@ "output_type": "display_data" } ], - "execution_count": 187 + "execution_count": 26 }, { "cell_type": "markdown", @@ -1459,8 +1437,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:21.055727Z", - "start_time": "2025-02-05T09:48:18.802198Z" + "end_time": "2025-02-07T16:32:23.064353Z", + "start_time": "2025-02-07T16:32:20.623241Z" } }, "source": [ @@ -1469,7 +1447,7 @@ "mnist = fetch_openml('mnist_784')" ], "outputs": [], - "execution_count": 52 + "execution_count": 27 }, { "cell_type": "markdown", @@ -1482,8 +1460,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:22.477736Z", - "start_time": "2025-02-05T09:48:22.465303Z" + "end_time": "2025-02-07T16:32:23.107290Z", + "start_time": "2025-02-07T16:32:23.095904Z" } }, "source": [ @@ -1847,19 +1825,19 @@ "" ] }, - "execution_count": 53, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 53 + "execution_count": 28 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:23.448971Z", - "start_time": "2025-02-05T09:48:23.445653Z" + "end_time": "2025-02-07T16:32:23.292852Z", + "start_time": "2025-02-07T16:32:23.277441Z" } }, "source": [ @@ -1884,31 +1862,31 @@ "Categories (10, object): ['0', '1', '2', '3', ..., '6', '7', '8', '9']" ] }, - "execution_count": 54, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 54 + "execution_count": 29 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:24.144564Z", - "start_time": "2025-02-05T09:48:24.142427Z" + "end_time": "2025-02-07T16:32:23.505490Z", + "start_time": "2025-02-07T16:32:23.500486Z" } }, "source": "X, y = mnist.data, mnist.target", "outputs": [], - "execution_count": 55 + "execution_count": 30 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:24.514332Z", - "start_time": "2025-02-05T09:48:24.511486Z" + "end_time": "2025-02-07T16:32:23.559082Z", + "start_time": "2025-02-07T16:32:23.555630Z" } }, "source": [ @@ -1921,19 +1899,19 @@ "pandas.core.frame.DataFrame" ] }, - "execution_count": 56, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 56 + "execution_count": 31 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:24.886667Z", - "start_time": "2025-02-05T09:48:24.884478Z" + "end_time": "2025-02-07T16:32:23.690898Z", + "start_time": "2025-02-07T16:32:23.686508Z" } }, "source": [ @@ -1946,12 +1924,12 @@ "pandas.core.series.Series" ] }, - "execution_count": 57, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 57 + "execution_count": 32 }, { "cell_type": "markdown", @@ -1964,8 +1942,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:31.763896Z", - "start_time": "2025-02-05T09:48:31.760241Z" + "end_time": "2025-02-07T16:32:23.768402Z", + "start_time": "2025-02-07T16:32:23.759338Z" } }, "source": [ @@ -1979,19 +1957,19 @@ "(numpy.ndarray, numpy.ndarray)" ] }, - "execution_count": 58, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 58 + "execution_count": 33 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:32.298865Z", - "start_time": "2025-02-05T09:48:32.295472Z" + "end_time": "2025-02-07T16:32:23.842258Z", + "start_time": "2025-02-07T16:32:23.831330Z" } }, "source": "X.shape, y.shape", @@ -2002,12 +1980,12 @@ "((70000, 784), (70000,))" ] }, - "execution_count": 59, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 59 + "execution_count": 34 }, { "cell_type": "markdown", @@ -2020,8 +1998,8 @@ "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:34.139776Z", - "start_time": "2025-02-05T09:48:34.085344Z" + "end_time": "2025-02-07T16:32:23.969004Z", + "start_time": "2025-02-07T16:32:23.905090Z" } }, "source": [ @@ -2043,14 +2021,14 @@ "output_type": "display_data" } ], - "execution_count": 60 + "execution_count": 35 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:34.972262Z", - "start_time": "2025-02-05T09:48:34.969534Z" + "end_time": "2025-02-07T16:32:24.460659Z", + "start_time": "2025-02-07T16:32:24.455555Z" } }, "source": [ @@ -2064,26 +2042,26 @@ "'5'" ] }, - "execution_count": 61, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 61 + "execution_count": 36 }, { "cell_type": "code", "metadata": { "ExecuteTime": { - "end_time": "2025-02-05T09:48:36.165952Z", - "start_time": "2025-02-05T09:48:35.753636Z" + "end_time": "2025-02-07T16:32:25.246771Z", + "start_time": "2025-02-07T16:32:24.668327Z" } }, "source": [ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)" ], "outputs": [], - "execution_count": 62 + "execution_count": 37 }, { "cell_type": "markdown", @@ -2097,9 +2075,11 @@ }, { "metadata": { + "jupyter": { + "is_executing": true + }, "ExecuteTime": { - "end_time": "2025-02-05T09:55:57.835820Z", - "start_time": "2025-02-05T09:55:02.002908Z" + "start_time": "2025-02-07T16:32:25.251727Z" } }, "cell_type": "code", @@ -2117,19 +2097,9 @@ "text": [ "Accuracy score: 0.9693506493506493\n" ] - }, - { - "data": { - "text/plain": [ - "'5'" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" } ], - "execution_count": 74 + "execution_count": null }, { "cell_type": "markdown", @@ -2408,13 +2378,6 @@ } ], "execution_count": 93 - }, - { - "metadata": {}, - "cell_type": "code", - "outputs": [], - "execution_count": null, - "source": "" } ], "metadata": {