{"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-26T18:55:07.041607Z","iopub.execute_input":"2026-05-26T18:55:07.041948Z","iopub.status.idle":"2026-05-26T18:55:09.518292Z","shell.execute_reply.started":"2026-05-26T18:55:07.041905Z","shell.execute_reply":"2026-05-26T18:55:09.516892Z"}},"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-26T19:29:09.929659Z","iopub.execute_input":"2026-05-26T19:29:09.930062Z","iopub.status.idle":"2026-05-26T19:29:12.212562Z","shell.execute_reply.started":"2026-05-26T19:29:09.929999Z","shell.execute_reply":"2026-05-26T19:29:12.211516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:29:27.399438Z","iopub.execute_input":"2026-05-26T19:29:27.399826Z","iopub.status.idle":"2026-05-26T19:29:27.415767Z","shell.execute_reply.started":"2026-05-26T19:29:27.399793Z","shell.execute_reply":"2026-05-26T19:29:27.414757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:29:28.601900Z","iopub.execute_input":"2026-05-26T19:29:28.602281Z","iopub.status.idle":"2026-05-26T19:29:28.607117Z","shell.execute_reply.started":"2026-05-26T19:29:28.602248Z","shell.execute_reply":"2026-05-26T19:29:28.606264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[18306,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:29:29.906060Z","iopub.execute_input":"2026-05-26T19:29:29.906441Z","iopub.status.idle":"2026-05-26T19:29:30.035469Z","shell.execute_reply.started":"2026-05-26T19:29:29.906402Z","shell.execute_reply":"2026-05-26T19:29:30.034573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:29:34.180335Z","iopub.execute_input":"2026-05-26T19:29:34.180701Z","iopub.status.idle":"2026-05-26T19:29:34.332894Z","shell.execute_reply.started":"2026-05-26T19:29:34.180669Z","shell.execute_reply":"2026-05-26T19:29:34.331907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=df.iloc[ : ,1:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:29:57.778236Z","iopub.execute_input":"2026-05-26T19:29:57.778592Z","iopub.status.idle":"2026-05-26T19:29:57.783731Z","shell.execute_reply.started":"2026-05-26T19:29:57.778560Z","shell.execute_reply":"2026-05-26T19:29:57.782751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y=df.iloc[ : , 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:29:59.107713Z","iopub.execute_input":"2026-05-26T19:29:59.108600Z","iopub.status.idle":"2026-05-26T19:29:59.113391Z","shell.execute_reply.started":"2026-05-26T19:29:59.108557Z","shell.execute_reply":"2026-05-26T19:29:59.112478Z"}},"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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:30:03.786630Z","iopub.execute_input":"2026-05-26T19:30:03.786988Z","iopub.status.idle":"2026-05-26T19:30:04.007710Z","shell.execute_reply.started":"2026-05-26T19:30:03.786956Z","shell.execute_reply":"2026-05-26T19:30:04.006627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:08:44.825978Z","iopub.execute_input":"2026-05-26T19:08:44.826385Z","iopub.status.idle":"2026-05-26T19:08:44.832590Z","shell.execute_reply.started":"2026-05-26T19:08:44.826346Z","shell.execute_reply":"2026-05-26T19:08:44.831531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:11:24.207706Z","iopub.execute_input":"2026-05-26T19:11:24.208124Z","iopub.status.idle":"2026-05-26T19:11:24.486970Z","shell.execute_reply.started":"2026-05-26T19:11:24.208090Z","shell.execute_reply":"2026-05-26T19:11:24.486118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:12:00.647976Z","iopub.execute_input":"2026-05-26T19:12:00.648593Z","iopub.status.idle":"2026-05-26T19:12:00.653524Z","shell.execute_reply.started":"2026-05-26T19:12:00.648556Z","shell.execute_reply":"2026-05-26T19:12:00.652631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,Y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:12:41.549418Z","iopub.execute_input":"2026-05-26T19:12:41.549745Z","iopub.status.idle":"2026-05-26T19:12:41.706929Z","shell.execute_reply.started":"2026-05-26T19:12:41.549715Z","shell.execute_reply":"2026-05-26T19:12:41.706123Z"}},"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-05-26T19:15:27.971255Z","iopub.execute_input":"2026-05-26T19:15:27.971676Z","iopub.status.idle":"2026-05-26T19:15:37.250984Z","shell.execute_reply.started":"2026-05-26T19:15:27.971643Z","shell.execute_reply":"2026-05-26T19:15:37.250126Z"}},"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-05-26T19:17:58.152651Z","iopub.execute_input":"2026-05-26T19:17:58.153074Z","iopub.status.idle":"2026-05-26T19:17:58.162003Z","shell.execute_reply.started":"2026-05-26T19:17:58.153020Z","shell.execute_reply":"2026-05-26T19:17:58.161116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:20:29.036163Z","iopub.execute_input":"2026-05-26T19:20:29.037145Z","iopub.status.idle":"2026-05-26T19:20:29.041802Z","shell.execute_reply.started":"2026-05-26T19:20:29.037050Z","shell.execute_reply":"2026-05-26T19:20:29.040778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf= scaler.fit_transform(X_train)\nX_test_trf = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:30:35.110255Z","iopub.execute_input":"2026-05-26T19:30:35.110593Z","iopub.status.idle":"2026-05-26T19:30:35.582639Z","shell.execute_reply.started":"2026-05-26T19:30:35.110565Z","shell.execute_reply":"2026-05-26T19:30:35.581579Z"}},"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-05-26T19:36:19.637679Z","iopub.execute_input":"2026-05-26T19:36:19.638024Z","iopub.status.idle":"2026-05-26T19:36:19.642736Z","shell.execute_reply.started":"2026-05-26T19:36:19.637991Z","shell.execute_reply":"2026-05-26T19:36:19.641784Z"}},"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-26T19:36:20.487717Z","iopub.execute_input":"2026-05-26T19:36:20.488084Z","iopub.status.idle":"2026-05-26T19:36:21.345411Z","shell.execute_reply.started":"2026-05-26T19:36:20.488051Z","shell.execute_reply":"2026-05-26T19:36:21.344655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:36:21.464112Z","iopub.execute_input":"2026-05-26T19:36:21.464453Z","iopub.status.idle":"2026-05-26T19:36:21.470287Z","shell.execute_reply.started":"2026-05-26T19:36:21.464422Z","shell.execute_reply":"2026-05-26T19:36:21.469377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:36:22.750861Z","iopub.execute_input":"2026-05-26T19:36:22.751222Z","iopub.status.idle":"2026-05-26T19:36:22.755828Z","shell.execute_reply.started":"2026-05-26T19:36:22.751189Z","shell.execute_reply":"2026-05-26T19:36:22.754735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf, Y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:36:26.587871Z","iopub.execute_input":"2026-05-26T19:36:26.588259Z","iopub.status.idle":"2026-05-26T19:36:26.601700Z","shell.execute_reply.started":"2026-05-26T19:36:26.588225Z","shell.execute_reply":"2026-05-26T19:36:26.601018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_pred_trf=knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:36:28.149641Z","iopub.execute_input":"2026-05-26T19:36:28.149966Z","iopub.status.idle":"2026-05-26T19:36:30.370040Z","shell.execute_reply.started":"2026-05-26T19:36:28.149936Z","shell.execute_reply":"2026-05-26T19:36:30.369020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(Y_test,Y_pred_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:36:33.126845Z","iopub.execute_input":"2026-05-26T19:36:33.127202Z","iopub.status.idle":"2026-05-26T19:36:33.134726Z","shell.execute_reply.started":"2026-05-26T19:36:33.127169Z","shell.execute_reply":"2026-05-26T19:36:33.133813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for 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    knn.fit(X_train_trf, Y_train)\n    knn.fit(X_train_trf, Y_train)\n    Y_pred_trf=knn.predict(X_test_trf)\n    print(accuracy_score(Y_test,Y_pred_trf))\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T19:40:52.959764Z","iopub.execute_input":"2026-05-26T19:40:52.960120Z","iopub.status.idle":"2026-05-26T20:25:10.585782Z","shell.execute_reply.started":"2026-05-26T19:40:52.960089Z","shell.execute_reply":"2026-05-26T20:25:10.584459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Transformation 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-26T20:28:43.629181Z","iopub.execute_input":"2026-05-26T20:28:43.629579Z","iopub.status.idle":"2026-05-26T20:28:44.682535Z","shell.execute_reply.started":"2026-05-26T20:28:43.629548Z","shell.execute_reply":"2026-05-26T20:28:44.681706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T20:29:06.895027Z","iopub.execute_input":"2026-05-26T20:29:06.895424Z","iopub.status.idle":"2026-05-26T20:29:06.901700Z","shell.execute_reply.started":"2026-05-26T20:29:06.895388Z","shell.execute_reply":"2026-05-26T20:29:06.900806Z"}},"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_discrete_sequence=px.colors.qualitative.G10\n              )\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T20:38:25.075248Z","iopub.execute_input":"2026-05-26T20:38:25.075586Z","iopub.status.idle":"2026-05-26T20:38:25.145799Z","shell.execute_reply.started":"2026-05-26T20:38:25.075556Z","shell.execute_reply":"2026-05-26T20:38:25.143398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Transformation to a 3D coordinate system\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-26T20:38:20.280154Z","iopub.execute_input":"2026-05-26T20:38:20.280552Z","iopub.status.idle":"2026-05-26T20:38:21.345393Z","shell.execute_reply.started":"2026-05-26T20:38:20.280522Z","shell.execute_reply":"2026-05-26T20:38:21.344347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\n\nY_train_trf = Y_train.astype(str)\n\nfig = px.scatter_3d(\n    x=X_train_trf[:, 0],\n    y=X_train_trf[:, 1],\n    z=X_train_trf[:, 2],\n    color=Y_train_trf\n)\n\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20)\n)\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T20:44:24.944777Z","iopub.execute_input":"2026-05-26T20:44:24.945319Z","iopub.status.idle":"2026-05-26T20:44:25.133470Z","shell.execute_reply.started":"2026-05-26T20:44:24.945282Z","shell.execute_reply":"2026-05-26T20:44:25.132087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T21:04:04.582399Z","iopub.execute_input":"2026-05-26T21:04:04.583011Z","iopub.status.idle":"2026-05-26T21:04:04.588725Z","shell.execute_reply.started":"2026-05-26T21:04:04.582979Z","shell.execute_reply":"2026-05-26T21:04:04.587767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape\n#Eigen Vector","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T21:06:16.342868Z","iopub.execute_input":"2026-05-26T21:06:16.343219Z","iopub.status.idle":"2026-05-26T21:06:16.349048Z","shell.execute_reply.started":"2026-05-26T21:06:16.343186Z","shell.execute_reply":"2026-05-26T21:06:16.348235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T21:10:46.862423Z","iopub.execute_input":"2026-05-26T21:10:46.863146Z","iopub.status.idle":"2026-05-26T21:10:46.869318Z","shell.execute_reply.started":"2026-05-26T21:10:46.863098Z","shell.execute_reply":"2026-05-26T21:10:46.868349Z"}},"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-05-26T21:12:05.836592Z","iopub.execute_input":"2026-05-26T21:12:05.836954Z","iopub.status.idle":"2026-05-26T21:12:08.249728Z","shell.execute_reply.started":"2026-05-26T21:12:05.836923Z","shell.execute_reply":"2026-05-26T21:12:08.248948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T21:12:50.657097Z","iopub.execute_input":"2026-05-26T21:12:50.657452Z","iopub.status.idle":"2026-05-26T21:12:50.663721Z","shell.execute_reply.started":"2026-05-26T21:12:50.657420Z","shell.execute_reply":"2026-05-26T21:12:50.662778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-26T21:13:26.582242Z","iopub.execute_input":"2026-05-26T21:13:26.583181Z","iopub.status.idle":"2026-05-26T21:13:26.595933Z","shell.execute_reply.started":"2026-05-26T21:13:26.583092Z","shell.execute_reply":"2026-05-26T21:13:26.594944Z"}},"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-26T21:16:34.780885Z","iopub.execute_input":"2026-05-26T21:16:34.781249Z","iopub.status.idle":"2026-05-26T21:16:34.923154Z","shell.execute_reply.started":"2026-05-26T21:16:34.781215Z","shell.execute_reply":"2026-05-26T21:16:34.922320Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Thank You","metadata":{}}]}