{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31192,"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-01-09T20:00:37.707451Z","iopub.execute_input":"2026-01-09T20:00:37.708075Z","iopub.status.idle":"2026-01-09T20:00:37.716398Z","shell.execute_reply.started":"2026-01-09T20:00:37.708046Z","shell.execute_reply":"2026-01-09T20:00:37.715189Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Importing the csv file","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:37.718043Z","iopub.execute_input":"2026-01-09T20:00:37.718889Z","iopub.status.idle":"2026-01-09T20:00:40.152067Z","shell.execute_reply.started":"2026-01-09T20:00:37.718864Z","shell.execute_reply":"2026-01-09T20:00:40.151098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.153642Z","iopub.execute_input":"2026-01-09T20:00:40.153999Z","iopub.status.idle":"2026-01-09T20:00:40.175412Z","shell.execute_reply.started":"2026-01-09T20:00:40.153966Z","shell.execute_reply":"2026-01-09T20:00:40.174400Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visualizing the data","metadata":{}},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.177314Z","iopub.execute_input":"2026-01-09T20:00:40.178025Z","iopub.status.idle":"2026-01-09T20:00:40.195136Z","shell.execute_reply.started":"2026-01-09T20:00:40.177998Z","shell.execute_reply":"2026-01-09T20:00:40.194100Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.196362Z","iopub.execute_input":"2026-01-09T20:00:40.196732Z","iopub.status.idle":"2026-01-09T20:00:40.216108Z","shell.execute_reply.started":"2026-01-09T20:00:40.196709Z","shell.execute_reply":"2026-01-09T20:00:40.215083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[33414,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.217123Z","iopub.execute_input":"2026-01-09T20:00:40.217749Z","iopub.status.idle":"2026-01-09T20:00:40.404814Z","shell.execute_reply.started":"2026-01-09T20:00:40.217717Z","shell.execute_reply":"2026-01-09T20:00:40.403962Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train-Test split","metadata":{}},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.405708Z","iopub.execute_input":"2026-01-09T20:00:40.406185Z","iopub.status.idle":"2026-01-09T20:00:40.412399Z","shell.execute_reply.started":"2026-01-09T20:00:40.406157Z","shell.execute_reply":"2026-01-09T20:00:40.411016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.413549Z","iopub.execute_input":"2026-01-09T20:00:40.413896Z","iopub.status.idle":"2026-01-09T20:00:40.434014Z","shell.execute_reply.started":"2026-01-09T20:00:40.413872Z","shell.execute_reply":"2026-01-09T20:00:40.433065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_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-01-09T20:00:40.438102Z","iopub.execute_input":"2026-01-09T20:00:40.438525Z","iopub.status.idle":"2026-01-09T20:00:40.709655Z","shell.execute_reply.started":"2026-01-09T20:00:40.438500Z","shell.execute_reply":"2026-01-09T20:00:40.708785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.711050Z","iopub.execute_input":"2026-01-09T20:00:40.711364Z","iopub.status.idle":"2026-01-09T20:00:40.717996Z","shell.execute_reply.started":"2026-01-09T20:00:40.711340Z","shell.execute_reply":"2026-01-09T20:00:40.716539Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training without applying PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.719344Z","iopub.execute_input":"2026-01-09T20:00:40.719710Z","iopub.status.idle":"2026-01-09T20:00:40.737451Z","shell.execute_reply.started":"2026-01-09T20:00:40.719684Z","shell.execute_reply":"2026-01-09T20:00:40.736389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.738516Z","iopub.execute_input":"2026-01-09T20:00:40.738760Z","iopub.status.idle":"2026-01-09T20:00:40.882292Z","shell.execute_reply.started":"2026-01-09T20:00:40.738735Z","shell.execute_reply":"2026-01-09T20:00:40.881171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(x_test)\nprint(\"Total run time : \",time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:40.883010Z","iopub.execute_input":"2026-01-09T20:00:40.883327Z","iopub.status.idle":"2026-01-09T20:00:54.208048Z","shell.execute_reply.started":"2026-01-09T20:00:40.883305Z","shell.execute_reply":"2026-01-09T20:00:54.206876Z"}},"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-01-09T20:00:54.209518Z","iopub.execute_input":"2026-01-09T20:00:54.209830Z","iopub.status.idle":"2026-01-09T20:00:54.219292Z","shell.execute_reply.started":"2026-01-09T20:00:54.209806Z","shell.execute_reply":"2026-01-09T20:00:54.218273Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training with applying PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:54.220427Z","iopub.execute_input":"2026-01-09T20:00:54.220843Z","iopub.status.idle":"2026-01-09T20:00:54.242184Z","shell.execute_reply.started":"2026-01-09T20:00:54.220810Z","shell.execute_reply":"2026-01-09T20:00:54.241000Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Mean Centering the data","metadata":{}},{"cell_type":"code","source":"sc=StandardScaler()\nx_train=sc.fit_transform(x_train)\nx_test=sc.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:54.243683Z","iopub.execute_input":"2026-01-09T20:00:54.244068Z","iopub.status.idle":"2026-01-09T20:00:54.843912Z","shell.execute_reply.started":"2026-01-09T20:00:54.244034Z","shell.execute_reply":"2026-01-09T20:00:54.842663Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:54.844952Z","iopub.execute_input":"2026-01-09T20:00:54.845404Z","iopub.status.idle":"2026-01-09T20:00:54.850611Z","shell.execute_reply.started":"2026-01-09T20:00:54.845372Z","shell.execute_reply":"2026-01-09T20:00:54.849356Z"}},"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-01-09T20:00:54.851863Z","iopub.execute_input":"2026-01-09T20:00:54.852245Z","iopub.status.idle":"2026-01-09T20:00:59.546326Z","shell.execute_reply.started":"2026-01-09T20:00:54.852151Z","shell.execute_reply":"2026-01-09T20:00:59.545137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:59.547465Z","iopub.execute_input":"2026-01-09T20:00:59.547807Z","iopub.status.idle":"2026-01-09T20:00:59.555664Z","shell.execute_reply.started":"2026-01-09T20:00:59.547776Z","shell.execute_reply":"2026-01-09T20:00:59.554297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:59.556741Z","iopub.execute_input":"2026-01-09T20:00:59.557724Z","iopub.status.idle":"2026-01-09T20:00:59.632387Z","shell.execute_reply.started":"2026-01-09T20:00:59.557686Z","shell.execute_reply":"2026-01-09T20:00:59.631291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(x_test_trf)\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:00:59.633409Z","iopub.execute_input":"2026-01-09T20:00:59.633883Z","iopub.status.idle":"2026-01-09T20:01:02.275806Z","shell.execute_reply.started":"2026-01-09T20:00:59.633852Z","shell.execute_reply":"2026-01-09T20:01:02.274843Z"}},"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    \n#     knn = KNeighborsClassifier()\n    \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-01-09T20:01:02.276860Z","iopub.execute_input":"2026-01-09T20:01:02.277279Z","iopub.status.idle":"2026-01-09T20:01:02.281473Z","shell.execute_reply.started":"2026-01-09T20:01:02.277206Z","shell.execute_reply":"2026-01-09T20:01:02.280429Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Transforming to 2D","metadata":{}},{"cell_type":"code","source":"pca=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-01-09T20:01:02.282562Z","iopub.execute_input":"2026-01-09T20:01:02.282964Z","iopub.status.idle":"2026-01-09T20:01:03.089153Z","shell.execute_reply.started":"2026-01-09T20:01:02.282930Z","shell.execute_reply":"2026-01-09T20:01:03.088414Z"}},"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\n                )\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:01:03.093623Z","iopub.execute_input":"2026-01-09T20:01:03.094518Z","iopub.status.idle":"2026-01-09T20:01:03.192968Z","shell.execute_reply.started":"2026-01-09T20:01:03.094484Z","shell.execute_reply":"2026-01-09T20:01:03.191473Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Transforming to 3D","metadata":{}},{"cell_type":"code","source":"pca=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-01-09T20:01:03.194528Z","iopub.execute_input":"2026-01-09T20:01:03.194935Z","iopub.status.idle":"2026-01-09T20:01:04.056131Z","shell.execute_reply.started":"2026-01-09T20:01:03.194910Z","shell.execute_reply":"2026-01-09T20:01:04.055306Z"}},"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], 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()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:01:04.057075Z","iopub.execute_input":"2026-01-09T20:01:04.057510Z","iopub.status.idle":"2026-01-09T20:01:04.171303Z","shell.execute_reply.started":"2026-01-09T20:01:04.057476Z","shell.execute_reply":"2026-01-09T20:01:04.169877Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Eigen values","metadata":{}},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:01:04.172804Z","iopub.execute_input":"2026-01-09T20:01:04.173606Z","iopub.status.idle":"2026-01-09T20:01:04.180623Z","shell.execute_reply.started":"2026-01-09T20:01:04.173579Z","shell.execute_reply":"2026-01-09T20:01:04.179258Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Eigen vectors","metadata":{}},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T20:01:04.181750Z","iopub.execute_input":"2026-01-09T20:01:04.182140Z","iopub.status.idle":"2026-01-09T20:01:04.203070Z","shell.execute_reply.started":"2026-01-09T20:01:04.182117Z","shell.execute_reply":"2026-01-09T20:01:04.202153Z"}},"outputs":[],"execution_count":null}]}