{"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":false,"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-05-12T09:20:11.684249Z","iopub.execute_input":"2026-05-12T09:20:11.684545Z","iopub.status.idle":"2026-05-12T09:20:13.025583Z","shell.execute_reply.started":"2026-05-12T09:20:11.684514Z","shell.execute_reply":"2026-05-12T09:20:13.024587Z"}},"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-12T09:20:13.028355Z","iopub.execute_input":"2026-05-12T09:20:13.028815Z","iopub.status.idle":"2026-05-12T09:20:15.810568Z","shell.execute_reply.started":"2026-05-12T09:20:13.028779Z","shell.execute_reply":"2026-05-12T09:20:15.809472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:15.811914Z","iopub.execute_input":"2026-05-12T09:20:15.812452Z","iopub.status.idle":"2026-05-12T09:20:15.851899Z","shell.execute_reply.started":"2026-05-12T09:20:15.812409Z","shell.execute_reply":"2026-05-12T09:20:15.850836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:15.853026Z","iopub.execute_input":"2026-05-12T09:20:15.853357Z","iopub.status.idle":"2026-05-12T09:20:15.858892Z","shell.execute_reply.started":"2026-05-12T09:20:15.853329Z","shell.execute_reply":"2026-05-12T09:20:15.857591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[130,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:15.860130Z","iopub.execute_input":"2026-05-12T09:20:15.860430Z","iopub.status.idle":"2026-05-12T09:20:16.108504Z","shell.execute_reply.started":"2026-05-12T09:20:15.860403Z","shell.execute_reply":"2026-05-12T09:20:16.107546Z"}},"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-12T09:20:16.109752Z","iopub.execute_input":"2026-05-12T09:20:16.110536Z","iopub.status.idle":"2026-05-12T09:20:16.115919Z","shell.execute_reply.started":"2026-05-12T09:20:16.110489Z","shell.execute_reply":"2026-05-12T09:20:16.114806Z"}},"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-12T09:20:16.120125Z","iopub.execute_input":"2026-05-12T09:20:16.120675Z","iopub.status.idle":"2026-05-12T09:20:17.368354Z","shell.execute_reply.started":"2026-05-12T09:20:16.120630Z","shell.execute_reply":"2026-05-12T09:20:17.367131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:17.369753Z","iopub.execute_input":"2026-05-12T09:20:17.370321Z","iopub.status.idle":"2026-05-12T09:20:17.377184Z","shell.execute_reply.started":"2026-05-12T09:20:17.370276Z","shell.execute_reply":"2026-05-12T09:20:17.375944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:17.378542Z","iopub.execute_input":"2026-05-12T09:20:17.378950Z","iopub.status.idle":"2026-05-12T09:20:17.407072Z","shell.execute_reply.started":"2026-05-12T09:20:17.378907Z","shell.execute_reply":"2026-05-12T09:20:17.406003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:17.408396Z","iopub.execute_input":"2026-05-12T09:20:17.408791Z","iopub.status.idle":"2026-05-12T09:20:17.743318Z","shell.execute_reply.started":"2026-05-12T09:20:17.408751Z","shell.execute_reply":"2026-05-12T09:20:17.742190Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:17.744370Z","iopub.execute_input":"2026-05-12T09:20:17.744806Z","iopub.status.idle":"2026-05-12T09:20:17.751876Z","shell.execute_reply.started":"2026-05-12T09:20:17.744776Z","shell.execute_reply":"2026-05-12T09:20:17.750540Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:17.753280Z","iopub.execute_input":"2026-05-12T09:20:17.753652Z","iopub.status.idle":"2026-05-12T09:20:17.937444Z","shell.execute_reply.started":"2026-05-12T09:20:17.753611Z","shell.execute_reply":"2026-05-12T09:20:17.935973Z"}},"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-12T09:20:17.938645Z","iopub.execute_input":"2026-05-12T09:20:17.939014Z","iopub.status.idle":"2026-05-12T09:20:30.235162Z","shell.execute_reply.started":"2026-05-12T09:20:17.938983Z","shell.execute_reply":"2026-05-12T09:20:30.234030Z"}},"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-12T09:20:30.236432Z","iopub.execute_input":"2026-05-12T09:20:30.236706Z","iopub.status.idle":"2026-05-12T09:20:30.246295Z","shell.execute_reply.started":"2026-05-12T09:20:30.236679Z","shell.execute_reply":"2026-05-12T09:20:30.245294Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\npca=PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:30.247950Z","iopub.execute_input":"2026-05-12T09:20:30.248362Z","iopub.status.idle":"2026-05-12T09:20:30.274574Z","shell.execute_reply.started":"2026-05-12T09:20:30.248321Z","shell.execute_reply":"2026-05-12T09:20:30.273288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:30.275789Z","iopub.execute_input":"2026-05-12T09:20:30.276179Z","iopub.status.idle":"2026-05-12T09:20:30.724701Z","shell.execute_reply.started":"2026-05-12T09:20:30.276140Z","shell.execute_reply":"2026-05-12T09:20:30.723485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test=scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:30.726013Z","iopub.execute_input":"2026-05-12T09:20:30.726410Z","iopub.status.idle":"2026-05-12T09:20:30.772217Z","shell.execute_reply.started":"2026-05-12T09:20:30.726368Z","shell.execute_reply":"2026-05-12T09:20:30.771164Z"}},"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-12T09:20:30.773467Z","iopub.execute_input":"2026-05-12T09:20:30.773854Z","iopub.status.idle":"2026-05-12T09:20:31.744692Z","shell.execute_reply.started":"2026-05-12T09:20:30.773812Z","shell.execute_reply":"2026-05-12T09:20:31.743926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:31.745606Z","iopub.execute_input":"2026-05-12T09:20:31.745931Z","iopub.status.idle":"2026-05-12T09:20:31.764188Z","shell.execute_reply.started":"2026-05-12T09:20:31.745895Z","shell.execute_reply":"2026-05-12T09:20:31.763237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:31.765604Z","iopub.execute_input":"2026-05-12T09:20:31.765989Z","iopub.status.idle":"2026-05-12T09:20:34.622687Z","shell.execute_reply.started":"2026-05-12T09:20:31.765939Z","shell.execute_reply":"2026-05-12T09:20:34.621327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:20:34.624368Z","iopub.execute_input":"2026-05-12T09:20:34.624781Z","iopub.status.idle":"2026-05-12T09:20:34.633184Z","shell.execute_reply.started":"2026-05-12T09:20:34.624708Z","shell.execute_reply":"2026-05-12T09:20:34.632270Z"}},"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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:21:48.076301Z","iopub.execute_input":"2026-05-12T09:21:48.077509Z","iopub.status.idle":"2026-05-12T09:21:48.761997Z","shell.execute_reply.started":"2026-05-12T09:21:48.077466Z","shell.execute_reply":"2026-05-12T09:21:48.761242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:21:49.476184Z","iopub.execute_input":"2026-05-12T09:21:49.476560Z","iopub.status.idle":"2026-05-12T09:21:49.485155Z","shell.execute_reply.started":"2026-05-12T09:21:49.476528Z","shell.execute_reply":"2026-05-12T09:21:49.483901Z"}},"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,\n             )\nfig.update_layout(\n    margin=dict(l=20,r=20,t=20,b=20)\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:21:51.278989Z","iopub.execute_input":"2026-05-12T09:21:51.279412Z","iopub.status.idle":"2026-05-12T09:21:51.537031Z","shell.execute_reply.started":"2026-05-12T09:21:51.279377Z","shell.execute_reply":"2026-05-12T09:21:51.535851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:22:05.452217Z","iopub.execute_input":"2026-05-12T09:22:05.452568Z","iopub.status.idle":"2026-05-12T09:22:05.459900Z","shell.execute_reply.started":"2026-05-12T09:22:05.452537Z","shell.execute_reply":"2026-05-12T09:22:05.458983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:22:38.640794Z","iopub.execute_input":"2026-05-12T09:22:38.641662Z","iopub.status.idle":"2026-05-12T09:22:38.647928Z","shell.execute_reply.started":"2026-05-12T09:22:38.641607Z","shell.execute_reply":"2026-05-12T09:22:38.647077Z"}},"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-12T09:23:26.819012Z","iopub.execute_input":"2026-05-12T09:23:26.819430Z","iopub.status.idle":"2026-05-12T09:23:28.455860Z","shell.execute_reply.started":"2026-05-12T09:23:26.819396Z","shell.execute_reply":"2026-05-12T09:23:28.455116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-12T09:25:10.191547Z","iopub.execute_input":"2026-05-12T09:25:10.192409Z","iopub.status.idle":"2026-05-12T09:25:10.205379Z","shell.execute_reply.started":"2026-05-12T09:25:10.192373Z","shell.execute_reply":"2026-05-12T09:25:10.204163Z"}},"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-12T09:26:07.695806Z","iopub.execute_input":"2026-05-12T09:26:07.696221Z","iopub.status.idle":"2026-05-12T09:26:07.845807Z","shell.execute_reply.started":"2026-05-12T09:26:07.696177Z","shell.execute_reply":"2026-05-12T09:26:07.844614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}