{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:41.650866Z","iopub.execute_input":"2026-02-21T17:27:41.651343Z","iopub.status.idle":"2026-02-21T17:27:41.660185Z","shell.execute_reply.started":"2026-02-21T17:27:41.651313Z","shell.execute_reply":"2026-02-21T17:27:41.659022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:41.673329Z","iopub.execute_input":"2026-02-21T17:27:41.673717Z","iopub.status.idle":"2026-02-21T17:27:43.885149Z","shell.execute_reply.started":"2026-02-21T17:27:41.673686Z","shell.execute_reply":"2026-02-21T17:27:43.884081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:43.886858Z","iopub.execute_input":"2026-02-21T17:27:43.887275Z","iopub.status.idle":"2026-02-21T17:27:43.893854Z","shell.execute_reply.started":"2026-02-21T17:27:43.887249Z","shell.execute_reply":"2026-02-21T17:27:43.892993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:43.895029Z","iopub.execute_input":"2026-02-21T17:27:43.895361Z","iopub.status.idle":"2026-02-21T17:27:43.920440Z","shell.execute_reply.started":"2026-02-21T17:27:43.895323Z","shell.execute_reply":"2026-02-21T17:27:43.919413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:43.923367Z","iopub.execute_input":"2026-02-21T17:27:43.923819Z","iopub.status.idle":"2026-02-21T17:27:43.935503Z","shell.execute_reply.started":"2026-02-21T17:27:43.923792Z","shell.execute_reply":"2026-02-21T17:27:43.934444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[6835\t, 1:].values.reshape(28, 28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:43.936741Z","iopub.execute_input":"2026-02-21T17:27:43.937069Z","iopub.status.idle":"2026-02-21T17:27:44.086828Z","shell.execute_reply.started":"2026-02-21T17:27:43.937045Z","shell.execute_reply":"2026-02-21T17:27:44.086121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:, 1:]\ny = df.iloc[:, 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:44.088212Z","iopub.execute_input":"2026-02-21T17:27:44.088561Z","iopub.status.idle":"2026-02-21T17:27:44.094682Z","shell.execute_reply.started":"2026-02-21T17:27:44.088527Z","shell.execute_reply":"2026-02-21T17:27:44.093270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=42)\nX_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:44.096034Z","iopub.execute_input":"2026-02-21T17:27:44.096369Z","iopub.status.idle":"2026-02-21T17:27:44.342012Z","shell.execute_reply.started":"2026-02-21T17:27:44.096336Z","shell.execute_reply":"2026-02-21T17:27:44.341088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:44.343431Z","iopub.execute_input":"2026-02-21T17:27:44.343774Z","iopub.status.idle":"2026-02-21T17:27:44.348725Z","shell.execute_reply.started":"2026-02-21T17:27:44.343749Z","shell.execute_reply":"2026-02-21T17:27:44.347742Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:44.349943Z","iopub.execute_input":"2026-02-21T17:27:44.350452Z","iopub.status.idle":"2026-02-21T17:27:44.477965Z","shell.execute_reply.started":"2026-02-21T17:27:44.350424Z","shell.execute_reply":"2026-02-21T17:27:44.476959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:44.480351Z","iopub.execute_input":"2026-02-21T17:27:44.481252Z","iopub.status.idle":"2026-02-21T17:27:54.879162Z","shell.execute_reply.started":"2026-02-21T17:27:44.481225Z","shell.execute_reply":"2026-02-21T17:27:54.878331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:54.880172Z","iopub.execute_input":"2026-02-21T17:27:54.880573Z","iopub.status.idle":"2026-02-21T17:27:54.888914Z","shell.execute_reply.started":"2026-02-21T17:27:54.880518Z","shell.execute_reply":"2026-02-21T17:27:54.887977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\n\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:54.890431Z","iopub.execute_input":"2026-02-21T17:27:54.890978Z","iopub.status.idle":"2026-02-21T17:27:55.383399Z","shell.execute_reply.started":"2026-02-21T17:27:54.890948Z","shell.execute_reply":"2026-02-21T17:27:55.382434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components = 200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:55.385018Z","iopub.execute_input":"2026-02-21T17:27:55.385274Z","iopub.status.idle":"2026-02-21T17:27:55.390108Z","shell.execute_reply.started":"2026-02-21T17:27:55.385252Z","shell.execute_reply":"2026-02-21T17:27:55.388824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:55.391508Z","iopub.execute_input":"2026-02-21T17:27:55.392019Z","iopub.status.idle":"2026-02-21T17:27:56.251643Z","shell.execute_reply.started":"2026-02-21T17:27:55.391976Z","shell.execute_reply":"2026-02-21T17:27:56.250938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:56.252461Z","iopub.execute_input":"2026-02-21T17:27:56.252726Z","iopub.status.idle":"2026-02-21T17:27:56.257916Z","shell.execute_reply.started":"2026-02-21T17:27:56.252699Z","shell.execute_reply":"2026-02-21T17:27:56.257004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:56.259408Z","iopub.execute_input":"2026-02-21T17:27:56.259698Z","iopub.status.idle":"2026-02-21T17:27:56.278324Z","shell.execute_reply.started":"2026-02-21T17:27:56.259669Z","shell.execute_reply":"2026-02-21T17:27:56.277488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:56.281087Z","iopub.execute_input":"2026-02-21T17:27:56.282154Z","iopub.status.idle":"2026-02-21T17:27:56.292135Z","shell.execute_reply.started":"2026-02-21T17:27:56.282120Z","shell.execute_reply":"2026-02-21T17:27:56.291366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:56.294286Z","iopub.execute_input":"2026-02-21T17:27:56.294582Z","iopub.status.idle":"2026-02-21T17:27:56.321081Z","shell.execute_reply.started":"2026-02-21T17:27:56.294555Z","shell.execute_reply":"2026-02-21T17:27:56.320411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"start = time.time()\ny_pred = knn.predict(X_test_trf)\nprint(time.time() - start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:56.322381Z","iopub.execute_input":"2026-02-21T17:27:56.322665Z","iopub.status.idle":"2026-02-21T17:27:58.950326Z","shell.execute_reply.started":"2026-02-21T17:27:56.322637Z","shell.execute_reply":"2026-02-21T17:27:58.949278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:27:58.951538Z","iopub.execute_input":"2026-02-21T17:27:58.952014Z","iopub.status.idle":"2026-02-21T17:27:58.959278Z","shell.execute_reply.started":"2026-02-21T17:27:58.951978Z","shell.execute_reply":"2026-02-21T17:27:58.958022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1, 785):\n    pca = PCA(n_components = i)\n\n    X_train_trf = pca.fit_transform(X_train)\n    X_test_trf = pca.transform(X_test)\n\n    knn = KNeighborsClassifier()\n    knn.fit(X_train_trf, y_train)\n    \n    y_pred = knn.predict(X_test_trf)\n\n    print(accuracy_score(y_test, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T17:32:59.909275Z","iopub.execute_input":"2026-02-21T17:32:59.910540Z","iopub.status.idle":"2026-02-21T18:47:18.034643Z","shell.execute_reply.started":"2026-02-21T17:32:59.910506Z","shell.execute_reply":"2026-02-21T18:47:18.033679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = 2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)\nX_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T18:48:48.558148Z","iopub.execute_input":"2026-02-21T18:48:48.558459Z","iopub.status.idle":"2026-02-21T18:48:49.175782Z","shell.execute_reply.started":"2026-02-21T18:48:48.558434Z","shell.execute_reply":"2026-02-21T18:48:49.175079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter(x = X_train_trf[:, 0],\n                 y = X_train_trf[:, 1],\n                 color = y_train_trf,\n                 color_discrete_sequence=px.colors.qualitative.G10)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T18:55:21.619263Z","iopub.execute_input":"2026-02-21T18:55:21.619789Z","iopub.status.idle":"2026-02-21T18:55:21.718042Z","shell.execute_reply.started":"2026-02-21T18:55:21.619749Z","shell.execute_reply":"2026-02-21T18:55:21.716928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = 3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)\nX_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T18:57:08.774212Z","iopub.execute_input":"2026-02-21T18:57:08.774522Z","iopub.status.idle":"2026-02-21T18:57:09.396849Z","shell.execute_reply.started":"2026-02-21T18:57:08.774498Z","shell.execute_reply":"2026-02-21T18:57:09.396121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df,x = X_train_trf[:, 0],\n                       y = X_train_trf[:, 1],\n                       z = X_train_trf[:, 2],\n                       color = y_train_trf)\nfig.update_layout(margin = dict(l = 20, r = 20, t = 20, b = 20))\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:13:05.559106Z","iopub.execute_input":"2026-02-21T19:13:05.560154Z","iopub.status.idle":"2026-02-21T19:13:05.667731Z","shell.execute_reply.started":"2026-02-21T19:13:05.560119Z","shell.execute_reply":"2026-02-21T19:13:05.666549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_\n# Eign Values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:15:48.332818Z","iopub.execute_input":"2026-02-21T19:15:48.333163Z","iopub.status.idle":"2026-02-21T19:15:48.339819Z","shell.execute_reply.started":"2026-02-21T19:15:48.333136Z","shell.execute_reply":"2026-02-21T19:15:48.338670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape\n# Eign Values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:16:21.452474Z","iopub.execute_input":"2026-02-21T19:16:21.453271Z","iopub.status.idle":"2026-02-21T19:16:21.460305Z","shell.execute_reply.started":"2026-02-21T19:16:21.453236Z","shell.execute_reply":"2026-02-21T19:16:21.458926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:20:09.086008Z","iopub.execute_input":"2026-02-21T19:20:09.086695Z","iopub.status.idle":"2026-02-21T19:20:09.093520Z","shell.execute_reply.started":"2026-02-21T19:20:09.086664Z","shell.execute_reply":"2026-02-21T19:20:09.092427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = None)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:21:44.499725Z","iopub.execute_input":"2026-02-21T19:21:44.500122Z","iopub.status.idle":"2026-02-21T19:21:46.051493Z","shell.execute_reply.started":"2026-02-21T19:21:44.500096Z","shell.execute_reply":"2026-02-21T19:21:46.050752Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:22:03.017476Z","iopub.execute_input":"2026-02-21T19:22:03.017827Z","iopub.status.idle":"2026-02-21T19:22:03.023672Z","shell.execute_reply.started":"2026-02-21T19:22:03.017800Z","shell.execute_reply":"2026-02-21T19:22:03.022857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:23:35.326809Z","iopub.execute_input":"2026-02-21T19:23:35.328070Z","iopub.status.idle":"2026-02-21T19:23:35.338759Z","shell.execute_reply.started":"2026-02-21T19:23:35.328034Z","shell.execute_reply":"2026-02-21T19:23:35.337946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T19:25:50.842605Z","iopub.execute_input":"2026-02-21T19:25:50.843479Z","iopub.status.idle":"2026-02-21T19:25:50.984675Z","shell.execute_reply.started":"2026-02-21T19:25:50.843426Z","shell.execute_reply":"2026-02-21T19:25:50.983375Z"}},"outputs":[],"execution_count":null}]}