{"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":31328,"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:16:28.623433Z","iopub.execute_input":"2026-05-23T06:16:28.623708Z","iopub.status.idle":"2026-05-23T06:16:31.129170Z","shell.execute_reply.started":"2026-05-23T06:16:28.623673Z","shell.execute_reply":"2026-05-23T06:16:31.128193Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:17:17.825257Z","iopub.execute_input":"2026-05-23T06:17:17.825583Z","iopub.status.idle":"2026-05-23T06:17:20.277115Z","shell.execute_reply.started":"2026-05-23T06:17:17.825554Z","shell.execute_reply":"2026-05-23T06:17:20.276378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:20:35.958000Z","iopub.execute_input":"2026-05-23T06:20:35.959107Z","iopub.status.idle":"2026-05-23T06:20:35.972974Z","shell.execute_reply.started":"2026-05-23T06:20:35.959057Z","shell.execute_reply":"2026-05-23T06:20:35.972088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:22:11.467586Z","iopub.execute_input":"2026-05-23T06:22:11.467921Z","iopub.status.idle":"2026-05-23T06:22:11.484308Z","shell.execute_reply.started":"2026-05-23T06:22:11.467891Z","shell.execute_reply":"2026-05-23T06:22:11.483369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:20:24.052996Z","iopub.execute_input":"2026-05-23T06:20:24.053773Z","iopub.status.idle":"2026-05-23T06:20:24.057814Z","shell.execute_reply.started":"2026-05-23T06:20:24.053730Z","shell.execute_reply":"2026-05-23T06:20:24.056924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[3802,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:23:47.985092Z","iopub.execute_input":"2026-05-23T06:23:47.985448Z","iopub.status.idle":"2026-05-23T06:23:48.120333Z","shell.execute_reply.started":"2026-05-23T06:23:47.985415Z","shell.execute_reply":"2026-05-23T06:23:48.119445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:25:36.972774Z","iopub.execute_input":"2026-05-23T06:25:36.973679Z","iopub.status.idle":"2026-05-23T06:25:36.978671Z","shell.execute_reply.started":"2026-05-23T06:25:36.973616Z","shell.execute_reply":"2026-05-23T06:25:36.977576Z"}},"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)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:27:08.836446Z","iopub.execute_input":"2026-05-23T06:27:08.836806Z","iopub.status.idle":"2026-05-23T06:27:10.046228Z","shell.execute_reply.started":"2026-05-23T06:27:08.836774Z","shell.execute_reply":"2026-05-23T06:27:10.045488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:27:21.263715Z","iopub.execute_input":"2026-05-23T06:27:21.264244Z","iopub.status.idle":"2026-05-23T06:27:21.576540Z","shell.execute_reply.started":"2026-05-23T06:27:21.264209Z","shell.execute_reply":"2026-05-23T06:27:21.575595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nknn = KNeighborsClassifier()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:27:32.224580Z","iopub.execute_input":"2026-05-23T06:27:32.225599Z","iopub.status.idle":"2026-05-23T06:27:32.229512Z","shell.execute_reply.started":"2026-05-23T06:27:32.225564Z","shell.execute_reply":"2026-05-23T06:27:32.228504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nknn.fit(X_train,y_train)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:27:40.216297Z","iopub.execute_input":"2026-05-23T06:27:40.216626Z","iopub.status.idle":"2026-05-23T06:27:40.366492Z","shell.execute_reply.started":"2026-05-23T06:27:40.216596Z","shell.execute_reply":"2026-05-23T06:27:40.365665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nimport time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - start)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:27:50.704847Z","iopub.execute_input":"2026-05-23T06:27:50.705187Z","iopub.status.idle":"2026-05-23T06:28:01.849931Z","shell.execute_reply.started":"2026-05-23T06:27:50.705154Z","shell.execute_reply":"2026-05-23T06:28:01.848951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nfrom sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:33:30.000192Z","iopub.execute_input":"2026-05-23T06:33:30.000547Z","iopub.status.idle":"2026-05-23T06:33:30.008280Z","shell.execute_reply.started":"2026-05-23T06:33:30.000515Z","shell.execute_reply":"2026-05-23T06:33:30.007470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:34:18.734848Z","iopub.execute_input":"2026-05-23T06:34:18.735197Z","iopub.status.idle":"2026-05-23T06:34:18.739813Z","shell.execute_reply.started":"2026-05-23T06:34:18.735167Z","shell.execute_reply":"2026-05-23T06:34:18.738735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:35:05.101466Z","iopub.execute_input":"2026-05-23T06:35:05.101832Z","iopub.status.idle":"2026-05-23T06:35:05.440208Z","shell.execute_reply.started":"2026-05-23T06:35:05.101796Z","shell.execute_reply":"2026-05-23T06:35:05.439306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:36:02.163511Z","iopub.execute_input":"2026-05-23T06:36:02.164356Z","iopub.status.idle":"2026-05-23T06:36:02.168726Z","shell.execute_reply.started":"2026-05-23T06:36:02.164311Z","shell.execute_reply":"2026-05-23T06:36:02.167717Z"}},"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-05-23T06:36:16.399447Z","iopub.execute_input":"2026-05-23T06:36:16.400370Z","iopub.status.idle":"2026-05-23T06:36:17.352968Z","shell.execute_reply.started":"2026-05-23T06:36:16.400326Z","shell.execute_reply":"2026-05-23T06:36:17.352245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train_trf.shape\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:36:26.972975Z","iopub.execute_input":"2026-05-23T06:36:26.973708Z","iopub.status.idle":"2026-05-23T06:36:26.978926Z","shell.execute_reply.started":"2026-05-23T06:36:26.973675Z","shell.execute_reply":"2026-05-23T06:36:26.978130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nknn = KNeighborsClassifier()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:37:12.929430Z","iopub.execute_input":"2026-05-23T06:37:12.929789Z","iopub.status.idle":"2026-05-23T06:37:12.934216Z","shell.execute_reply.started":"2026-05-23T06:37:12.929758Z","shell.execute_reply":"2026-05-23T06:37:12.933299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nknn.fit(X_train_trf,y_train)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:37:16.596251Z","iopub.execute_input":"2026-05-23T06:37:16.596587Z","iopub.status.idle":"2026-05-23T06:37:16.610590Z","shell.execute_reply.started":"2026-05-23T06:37:16.596556Z","shell.execute_reply":"2026-05-23T06:37:16.609665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:37:22.194545Z","iopub.execute_input":"2026-05-23T06:37:22.195435Z","iopub.status.idle":"2026-05-23T06:37:24.980462Z","shell.execute_reply.started":"2026-05-23T06:37:22.195393Z","shell.execute_reply":"2026-05-23T06:37:24.979561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\naccuracy_score(y_test,y_pred)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:37:38.650808Z","iopub.execute_input":"2026-05-23T06:37:38.651882Z","iopub.status.idle":"2026-05-23T06:37:38.659103Z","shell.execute_reply.started":"2026-05-23T06:37:38.651839Z","shell.execute_reply":"2026-05-23T06:37:38.658200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max_accuracy=-1\nmax_n=-1\nfor i in range(1,785):\n    pca = PCA(n_components=i)\n    X_train_trf = pca.fit_transform(X_train)\n    X_test_trf = pca.transform(X_test)\n    \n    knn = KNeighborsClassifier()\n    \n    knn.fit(X_train_trf,y_train)\n    \n    y_pred = knn.predict(X_test_trf)\n    acc=accuracy_score(y_test,y_pred)\n    max_accuracy=max(max_accuracy,acc)\n    if max_accuracy==acc:\n        max_n=i\n    print(i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:51:25.795934Z","iopub.execute_input":"2026-05-23T06:51:25.796989Z","iopub.status.idle":"2026-05-23T06:51:37.438569Z","shell.execute_reply.started":"2026-05-23T06:51:25.796881Z","shell.execute_reply":"2026-05-23T06:51:37.437326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming to a 2D coordinate system\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:52:01.008447Z","iopub.execute_input":"2026-05-23T06:52:01.009268Z","iopub.status.idle":"2026-05-23T06:52:02.113417Z","shell.execute_reply.started":"2026-05-23T06:52:01.009227Z","shell.execute_reply":"2026-05-23T06:52:02.112497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:52:17.704603Z","iopub.execute_input":"2026-05-23T06:52:17.705353Z","iopub.status.idle":"2026-05-23T06:52:17.711486Z","shell.execute_reply.started":"2026-05-23T06:52:17.705311Z","shell.execute_reply":"2026-05-23T06:52:17.710657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nimport 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\n                )\nfig.show()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:53:08.292407Z","iopub.execute_input":"2026-05-23T06:53:08.293084Z","iopub.status.idle":"2026-05-23T06:53:08.388114Z","shell.execute_reply.started":"2026-05-23T06:53:08.293050Z","shell.execute_reply":"2026-05-23T06:53:08.387106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming in 3D\npca = PCA(n_components=3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:53:29.255483Z","iopub.execute_input":"2026-05-23T06:53:29.256454Z","iopub.status.idle":"2026-05-23T06:53:30.362776Z","shell.execute_reply.started":"2026-05-23T06:53:29.256401Z","shell.execute_reply":"2026-05-23T06:53:30.361810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nimport plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df, x=X_train_trf[:,0], y=X_train_trf[:,1], z=X_train_trf[:,2],\n              color=y_train_trf)\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20),\n    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:53:38.515274Z","iopub.execute_input":"2026-05-23T06:53:38.515597Z","iopub.status.idle":"2026-05-23T06:53:38.694508Z","shell.execute_reply.started":"2026-05-23T06:53:38.515567Z","shell.execute_reply":"2026-05-23T06:53:38.693320Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:54:46.873104Z","iopub.execute_input":"2026-05-23T06:54:46.873440Z","iopub.status.idle":"2026-05-23T06:54:46.879271Z","shell.execute_reply.started":"2026-05-23T06:54:46.873410Z","shell.execute_reply":"2026-05-23T06:54:46.878478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:54:55.470764Z","iopub.execute_input":"2026-05-23T06:54:55.471109Z","iopub.status.idle":"2026-05-23T06:54:55.479289Z","shell.execute_reply.started":"2026-05-23T06:54:55.471076Z","shell.execute_reply":"2026-05-23T06:54:55.478402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:55:07.651816Z","iopub.execute_input":"2026-05-23T06:55:07.652797Z","iopub.status.idle":"2026-05-23T06:55:07.658729Z","shell.execute_reply.started":"2026-05-23T06:55:07.652681Z","shell.execute_reply":"2026-05-23T06:55:07.657763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\npca = PCA(n_components=None)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:55:17.581272Z","iopub.execute_input":"2026-05-23T06:55:17.581607Z","iopub.status.idle":"2026-05-23T06:55:20.469907Z","shell.execute_reply.started":"2026-05-23T06:55:17.581577Z","shell.execute_reply":"2026-05-23T06:55:20.469092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:55:31.377958Z","iopub.execute_input":"2026-05-23T06:55:31.378362Z","iopub.status.idle":"2026-05-23T06:55:31.384985Z","shell.execute_reply.started":"2026-05-23T06:55:31.378327Z","shell.execute_reply":"2026-05-23T06:55:31.384140Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:55:40.045266Z","iopub.execute_input":"2026-05-23T06:55:40.045607Z","iopub.status.idle":"2026-05-23T06:55:40.051591Z","shell.execute_reply.started":"2026-05-23T06:55:40.045577Z","shell.execute_reply":"2026-05-23T06:55:40.050631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:55:47.718851Z","iopub.execute_input":"2026-05-23T06:55:47.719189Z","iopub.status.idle":"2026-05-23T06:55:47.730738Z","shell.execute_reply.started":"2026-05-23T06:55:47.719157Z","shell.execute_reply":"2026-05-23T06:55:47.729873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-23T06:55:57.151391Z","iopub.execute_input":"2026-05-23T06:55:57.152249Z","iopub.status.idle":"2026-05-23T06:55:57.289024Z","shell.execute_reply.started":"2026-05-23T06:55:57.152209Z","shell.execute_reply":"2026-05-23T06:55:57.288206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}