{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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,"isSourceIdPinned":false}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"3f13c538","cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.neighbors import KNeighborsClassifier\nimport plotly.express as px\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.decomposition import PCA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:48.340452Z","iopub.execute_input":"2026-04-30T15:44:48.341544Z","iopub.status.idle":"2026-04-30T15:44:48.346846Z","shell.execute_reply.started":"2026-04-30T15:44:48.341504Z","shell.execute_reply":"2026-04-30T15:44:48.345604Z"}},"outputs":[],"execution_count":null},{"id":"2733df3b","cell_type":"code","source":"df = pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:48.348891Z","iopub.execute_input":"2026-04-30T15:44:48.349319Z","iopub.status.idle":"2026-04-30T15:44:50.587124Z","shell.execute_reply.started":"2026-04-30T15:44:48.349275Z","shell.execute_reply":"2026-04-30T15:44:50.586307Z"}},"outputs":[],"execution_count":null},{"id":"dd01d80c","cell_type":"markdown","source":"Visualizing the images of digits :-","metadata":{}},{"id":"3d545b7d","cell_type":"code","source":"sample = df.sample()['label']\nprint(f'DIGIT : {sample.values[0]}')\nplt.imshow(df.iloc[sample.index,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:50.588346Z","iopub.execute_input":"2026-04-30T15:44:50.588684Z","iopub.status.idle":"2026-04-30T15:44:50.793549Z","shell.execute_reply.started":"2026-04-30T15:44:50.588649Z","shell.execute_reply":"2026-04-30T15:44:50.792689Z"}},"outputs":[],"execution_count":null},{"id":"14e22321","cell_type":"code","source":"x = df.drop(columns=['label'])\ny = df['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:50.794480Z","iopub.execute_input":"2026-04-30T15:44:50.794910Z","iopub.status.idle":"2026-04-30T15:44:50.880076Z","shell.execute_reply.started":"2026-04-30T15:44:50.794868Z","shell.execute_reply":"2026-04-30T15:44:50.879019Z"}},"outputs":[],"execution_count":null},{"id":"8622a9a2","cell_type":"code","source":"x.shape,y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:50.882506Z","iopub.execute_input":"2026-04-30T15:44:50.883194Z","iopub.status.idle":"2026-04-30T15:44:50.889295Z","shell.execute_reply.started":"2026-04-30T15:44:50.883165Z","shell.execute_reply":"2026-04-30T15:44:50.888525Z"}},"outputs":[],"execution_count":null},{"id":"aa3e2512","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-04-30T15:44:50.890486Z","iopub.execute_input":"2026-04-30T15:44:50.890879Z","iopub.status.idle":"2026-04-30T15:44:51.138810Z","shell.execute_reply.started":"2026-04-30T15:44:50.890852Z","shell.execute_reply":"2026-04-30T15:44:51.138006Z"}},"outputs":[],"execution_count":null},{"id":"5139bde6","cell_type":"markdown","source":"# WITHOUT PCA","metadata":{}},{"id":"d6a03215","cell_type":"code","source":"model = KNeighborsClassifier()\nmodel.fit(x_train,y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:51.139823Z","iopub.execute_input":"2026-04-30T15:44:51.140058Z","iopub.status.idle":"2026-04-30T15:44:51.289068Z","shell.execute_reply.started":"2026-04-30T15:44:51.140035Z","shell.execute_reply":"2026-04-30T15:44:51.288149Z"}},"outputs":[],"execution_count":null},{"id":"c3ae89ce","cell_type":"code","source":"y_pred = model.predict(x_test)\n\naccuracy_score(y_test,y_pred)\n\n# Without PCA we got accuracy score of around 96%","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:44:51.290189Z","iopub.execute_input":"2026-04-30T15:44:51.290690Z","iopub.status.idle":"2026-04-30T15:45:01.731598Z","shell.execute_reply.started":"2026-04-30T15:44:51.290654Z","shell.execute_reply":"2026-04-30T15:45:01.730731Z"}},"outputs":[],"execution_count":null},{"id":"c0307d86","cell_type":"markdown","source":"# WITH PCA","metadata":{}},{"id":"e8247426","cell_type":"code","source":"# Standard scaling :-\n\nscale = StandardScaler()\n\nx_train = scale.fit_transform(x_train)\nx_test = scale.transform(x_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:45:01.732702Z","iopub.execute_input":"2026-04-30T15:45:01.733152Z","iopub.status.idle":"2026-04-30T15:45:02.182340Z","shell.execute_reply.started":"2026-04-30T15:45:01.733104Z","shell.execute_reply":"2026-04-30T15:45:02.181558Z"}},"outputs":[],"execution_count":null},{"id":"738a368d","cell_type":"code","source":"pca = PCA(n_components=200)\nx_train_transform = pca.fit_transform(x_train)\nx_test_transform = pca.transform(x_test)\n\nmodel2 = KNeighborsClassifier()\n\nmodel2.fit(x_train_transform,y_train)\n\ny_pred2 = model2.predict(x_test_transform)\n\naccuracy_score(y_test,y_pred2)\n\n# For n_components=200 i.e, 200 dimensions rather than 784 we are getting 95% accuracy that is pretty good.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:45:02.183627Z","iopub.execute_input":"2026-04-30T15:45:02.184013Z","iopub.status.idle":"2026-04-30T15:45:05.781559Z","shell.execute_reply.started":"2026-04-30T15:45:02.183973Z","shell.execute_reply":"2026-04-30T15:45:05.780618Z"}},"outputs":[],"execution_count":null},{"id":"50a0fd8c","cell_type":"markdown","source":"# PCA IN LOOP (Not very efficient ; just to check )\n# (time consuming)","metadata":{}},{"id":"f4d56fd2","cell_type":"code","source":"import time \nstart = time.time()\nscores = []\n\nfor i in range (1,785,50):\n    pca = PCA(n_components=i)\n    x_train_transform_loop = pca.fit_transform(x_train)\n    x_test_transform_loop = pca.transform(x_test)\n\n    model3 = KNeighborsClassifier()\n\n    model3.fit(x_train_transform_loop,y_train)\n\n    y_pred3 = model3.predict(x_test_transform_loop)\n\n    scores.append(accuracy_score(y_test,y_pred3))\n\nhighest = max(scores)\nn_comp = (scores.index(highest)*50)+1\n\nprint(f'for {n_comp} components , we got highest accuracy of {highest*100}%')\n\nprint(f'Time took : {time.time()-start} seconds')\n\n'''From this loop we  conclude that at 51 components the accuracy is highest around 95.5% which is very good\nconsidering from 784 dimensions to just 51.[VERY LESS COMPUTATIONS]'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:45:05.782852Z","iopub.execute_input":"2026-04-30T15:45:05.783422Z","iopub.status.idle":"2026-04-30T15:46:31.898225Z","shell.execute_reply.started":"2026-04-30T15:45:05.783393Z","shell.execute_reply":"2026-04-30T15:46:31.897308Z"}},"outputs":[],"execution_count":null},{"id":"a662300e","cell_type":"markdown","source":"# EFFICIENT METHOD TO FIND OPTIMAL NO. OF COMPONENTS ( Graph ):-","metadata":{}},{"id":"1fa7562b","cell_type":"code","source":"pca = PCA(n_components=None)\npca.fit_transform(x_train)\npca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:46:31.899556Z","iopub.execute_input":"2026-04-30T15:46:31.899862Z","iopub.status.idle":"2026-04-30T15:46:33.421507Z","shell.execute_reply.started":"2026-04-30T15:46:31.899835Z","shell.execute_reply":"2026-04-30T15:46:33.420258Z"}},"outputs":[],"execution_count":null},{"id":"6e6f3f0e","cell_type":"code","source":"pca.explained_variance_\n# Gives eigen values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:46:33.423256Z","iopub.execute_input":"2026-04-30T15:46:33.424698Z","iopub.status.idle":"2026-04-30T15:46:33.439018Z","shell.execute_reply.started":"2026-04-30T15:46:33.424659Z","shell.execute_reply":"2026-04-30T15:46:33.438262Z"}},"outputs":[],"execution_count":null},{"id":"dc7c9bb9","cell_type":"code","source":"pca.components_\n# gives eigen vectors.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:46:33.442515Z","iopub.execute_input":"2026-04-30T15:46:33.442841Z","iopub.status.idle":"2026-04-30T15:46:33.463268Z","shell.execute_reply.started":"2026-04-30T15:46:33.442809Z","shell.execute_reply":"2026-04-30T15:46:33.462434Z"}},"outputs":[],"execution_count":null},{"id":"70b5fd57","cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))\n\n# We want to have of more than 90% so through graph we can easily see the no. of components req.\n# Coming nearly 220.\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:46:33.464736Z","iopub.execute_input":"2026-04-30T15:46:33.465209Z","iopub.status.idle":"2026-04-30T15:46:33.617810Z","shell.execute_reply.started":"2026-04-30T15:46:33.465175Z","shell.execute_reply":"2026-04-30T15:46:33.616869Z"}},"outputs":[],"execution_count":null},{"id":"6980ad35","cell_type":"markdown","source":"# 2-D PLOTTING :","metadata":{}},{"id":"e7b21721","cell_type":"code","source":"pca = PCA(n_components=2)\nx_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)\ny_train_trf = y_train.astype(str)\n\nfig = px.scatter(x=x_train_trf[:,0],y=x_train_trf[:,1],color=y_train_trf)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:47:12.250167Z","iopub.execute_input":"2026-04-30T15:47:12.250706Z","iopub.status.idle":"2026-04-30T15:47:13.032887Z","shell.execute_reply.started":"2026-04-30T15:47:12.250673Z","shell.execute_reply":"2026-04-30T15:47:13.031786Z"}},"outputs":[],"execution_count":null},{"id":"a5378298","cell_type":"markdown","source":"# 3-D plotting:","metadata":{}},{"id":"e1f16e7b","cell_type":"code","source":"pca = PCA(n_components=3)\nx_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)\ny_train_trf = y_train.astype(str)\n\nfig = px.scatter_3d(x=x_train_trf[:,0],y=x_train_trf[:,1],z=x_train_trf[:,2],color=y_train_trf)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-30T15:47:20.249954Z","iopub.execute_input":"2026-04-30T15:47:20.250964Z","iopub.status.idle":"2026-04-30T15:47:20.995812Z","shell.execute_reply.started":"2026-04-30T15:47:20.250926Z","shell.execute_reply":"2026-04-30T15:47:20.994671Z"}},"outputs":[],"execution_count":null},{"id":"b2499313","cell_type":"markdown","source":"From this plot we can clearly see that in 3-D model cant discriminate between 0,6,9 and 3,5,8 and \n4,7 and 1,2 and 6,8 and 4,9 and many more due to similar structures.","metadata":{}}]}